Reputation: 173
So I am having trouble understanding how actually jump works,
let me give you an example, What i want to is to loop through L1 and jumps to L2 if the value of DX is > 5 so i did this, but how the code knows how many lines of code have to be read in L2 ?
MOV DX,0
MOV CX,10
L1:
ADD DX,30H
MOV AH,02H
INT 21H
SUB DX,30H
ADD DX,1
CMP DX,5
JA L2
LOOP L1
after jumping on L2 if i want to perform multiple operations i.e A,B,C how can i terminate the L2 after the third operation ?
L2:
A
B
C
;Go back into the Loop L1 , How can i terminate L2 here ?
Upvotes: 0
Views: 4802
Reputation: 4159
I don't know the specifics of this language but all assembly languages essentially work the same:
If the L2 code is a function, i.e. if you want to return to the function that called L2 (the caller function) after you are done executing L2, you want to get to the first instruction of L2 using a CALL. The last instruction in L2 would then be a return.
If, instead, after L2 you always want to go back to the beginning of L1, then you have to use an unconditional JUMP to the label L1 at the very end of your L2 code.
Typically, in most assembly languages, you only JUMP or CALL to labels: the assembler figures out for you how many positive or negative line offsets this translates into if the JUMP or CALL instructions you use rely on relative offsets as opposed to absolute addresses. If that offset is too large, the assembler will generate an error message at assembly time.
Upvotes: 1
Reputation: 22585
The CPU will read and execute each instruction, one at a time. The control flow will be linear unless it encounters an instruction which modifies the flow (e.g. your jump instruction).
If you want to change the flow but then return back to where you were, you have to use a CALL
instruction instead of a jump, and then you return back when the CPU executes a RET
instruction.
However, if you want to go to L1 label of your example, you can just put a JMP L1
instruction at the end of L2.
Upvotes: 2
Reputation: 5605
My 80x86 assembly memories are a bit old (20 years) , but i suppose that if you do not modify CX , you could call a LOOP L1 from L2: code.
If you're at the end of the LOOP (since LOOP decrements CX) , it won't go back to L1 if your JA to L2 is made at the 10th time.
Upvotes: 2