Jump & Loop Instructions

Download Report

Transcript Jump & Loop Instructions

Jumps, Loops and Branching
Unconditional Jumps
• Transfer the control flow of the program to a specified
instruction, other than the next instruction in
sequential execution
• Jumps are performed with labels pointing to the
address of the target instruction
• It is the equivalent of a goto statement
• Goto is good in assembly (no real alternative)!
Unconditional jumps
• Short jump
– JMP SHORT label, where label is within –128/+127 bytes off
the current instruction
• Near jump
– JMP label, where label is within –32,768/+32,767 bytes off
the current instruction
• Far jump
– JMP label, where label is any memory address, in
segment:offset format
• In protected mode near and far jumps are the same
• The assembler will know how far you are jumping
and use the correct instruction
Conditional Jumps
• Jumps performed if a condition evaluates to true
• Conditions are evaluated based on the values of the
bits in the FLAGS register
• Test S, Z, P, C, O
• If condition is true the jump is performed to the
location specified
• If condition is false the code proceeds with the next
instruction in sequence
• Short or near jumps in 80386
Comparisons
• CMP A, B
• Executes A-B without modifying A (non-destructive)
• CMP is usually followed by a conditional jump based
on the outcome of the comparison
CMP AL, 10h
JAE target
; If AL >= 10 jump
• CMP is a non-destructive SUB, it sets the flags
exactly like the SUB instruction
Comparisons
• Unsigned CMP A, B
– If (A<B), C=1
– If (A>B), C=0
– Z tested for equality/inequality
• Signed
CMP A, B
– If (S XOR O == 1) A<B else A>B
– Z tested for equality/inequality
Signed comparisons
• Case 1, Sign=1, Overflow=0
– A-B looks negative
– There is no overflow, so A<B
• Case 2, Sign=0, Overflow=1
– A-B looks positive
– But there is overflow so the sign bit is wrong and A<B
• Case 3, Sign=0, Overflow=0
– A-B looks positive
– No overflow, so A>B
• Case 4, Sign=1, Overflow=1
– A-B looks negative
– But overflow bit is set so the sign flag is wrong therefore A>B
• Fortunately, we don’t have to remember this stuff!
Conditional Jumps
•
•
•
•
•
•
Syntax: JX, where X denotes a test condition
J -> Jump
N -> Not
E -> Equal
A/B -> Above/Below for unsigned arithmetic
G/L -> Greater/Less for signed arithmetic
Conditional jump instructions
• JL, jump if less
– Jumps if A<B, that is if S XOR O == 1
• JA, JNBE (above == not (below or equal))
– Jumps if C=0 & Z=0
• JBE, JNA (below or equal == not above)
– Jumps if Z=1 | C=1
• JAE, JNB, JNC (above or equal == not below==no
carry)
– Jumps if C=0
Conditional jump instructions
• JB, JNA, JC (below == not above == carry set)
– Jumps if C=1
• JE, JZ (equal == result is 0)
– Jumps if Z=1
• JNE, JNZ (not equal == result is not 0)
– Jumps if Z=0
• JNS (sign, no sign)
– Jumps if S=0
• JO
– Jumps if O=1
• JS
– Jumps if S=1
Conditional jump instructions
• JNO
– O=0
• JG, JNLE (greater==not (less or equal))
– S=0, Z=0
• JGE, JNL (greater or equal == not less)
– S=0
• JL, JNGE (less == not (greater or equal))
– S XOR O = 1
• JLE, JNG (less or equal == not greater)
– (S XOR O = 1) | Z=1
• JCXZ (counter register is 0), useful for loops
Loops
• LOOP label
– Combination of CMP and JNZ
• Decrement the CX register and if register has not
become 0 jump to the specified label
• If CX becomes 0 the instruction following the LOOP
instruction is executed
Example
mov
mov
mov
cx, 100
si, BLOCK1
di, BLOCK2
;load count
Again:
lodsw
add
; (load string – word)
; gets Block1 data
; AX = [SI]; SI = SI + 2
AX, [ES:DI]
stosw
loop
ret
;add Block2 data
;store in Block2
; [DI] = AX; DI = DI + 2
Again
;repeat 100 times
If then else in assembly
mov ax, [a]
mov bx, [b]
cmp ax,bx
ja true
; false instructions
jmp done
true:
; true instructions
done”
If (a>b) {
/* true instructions
*/
} else {
/* false instructions
*/
}
Do while in assembly
begin:
; body instructions…
mov ax, [a]
mov bx, [b]
cmp a,b
je begin
do {
/* body instructions
*/
} while (a==b);
While do in assembly
begin:
mov ax, [a]
mov bx, [b]
cmp ax,bx
jne done
; instructions
jmp begin
done:
while (a==b) {
/* instructions */
};
(Simple) For loop
mov cx, 10
begin:
; instructions
loop begin
for (i=10;i>0;i--) {
/* instructions */
}
Examples
;
if
(J <= K) then
;
L := L + 1
;
else L := L - 1
;
J, K, L are signed words
mov
cmp
jnel
inc
jmp
ax, [J]
ax, [K]
DoElse
word [L]
ifDone
dec
word [L]
DoElse:
ifDone:
;
;
;
;
;
while (J >=
J := J
K := K
L := J
end;
WhlLoop:
mov
cmp
jnge
dec
inc
mov
imul
mov
jmp
QuitLoop:
K) do
- 1;
+ 1;
* K;
begin
ax, [J]
ax, [K]
QuitLoop
word [J]
word [K]
ax, [J]
[K]
[L], ax
WhlLoop