Conditional Jumps J Lots of different conditional jump instructions cond

Download Report

Transcript Conditional Jumps J Lots of different conditional jump instructions cond

Conditional Jumps

Jcond destination
 Cond
refers to a flag condition
 Destination should be a local label
 Destination must be within –128 to +127 bytes
from the current location

Lots of different conditional jump instructions
 Four
types:
Specific flag comparisons
 Equality between operands, or the value of ECX
 unsigned comparisons
 signed comparisons

Conditional Structures

Two steps are needed to execute

An operation such as CMP, AND, or SUB


To modify the flags
A conditional jump instruction tests the flags
and causes a branch to a new address.
1.
2.
CMP
jz
al, 0
L1
AND
jnz
dl, 1011 0000b
L2
;jump to L2 if ZF = 0
;jump to L1 if ZF = 1
Jumps Based on Specific Flag Values(10)
Mnemonic
Description
Flags/Registers
JZ
if zero
ZF = 1
JNZ
if not zero
ZF = 0
JC
if carry
CF = 1
JNC
if not carry
CF = 0
JO
If overflow
OF = 1
JNO
If not overflow
OF = 0
JS
if signed
SF = 1
JNS
if not signed
SF = 0
JP
if parity (even)
PF = 1
JNP
if not parity (odd)
PF = 0
Jumps Based on Equality(4)
Mnemonic
Description
Flags/Registers
JE
if equal
ZF = 1
JNE
if not equal
ZF = 0
JCXZ
if CX = 0
ZF = 0
JECXZ
if ECX = 0
ZF = 0
Jumps Based on Unsigned Comparisons(8)
Mnemonic
JA
JNBE
JAE
JNB
JB
JNAE
JBE
JNA
Description
Jump if above (if op1 > op 2)
Jump if NOT below or equal (same as JA)
Jump if above or equal (if op 1 >= op2)
Jump if NOT below (same as JAE)
Jump if Below (if op1 < op2)
Jump if NOT above or equal (same as JB)
Jump if below or equal (if op1 <= op2)
Jump if NOT above (same as JBE)
Flags
CF = 0 and ZF = 0
CF = 0
CF = 1
CF = 1 or ZF = 1
Jumps Based on Signed Comparisons(8)
Mnemonic
JG
JNLE
JGE
JNL
JL
Description
Flags
Jump if greater (if op1 > op 2)
Jump if NOT less than or equal (same as
JG)
Jump if greater than or equal (if op 1 >=
op2)
Jump if NOT less (same as JGE)
SF = OF and ZF = 0
SF = OF
Jump if less (if op1 < op2)
JNGE
Jump if NOT greater than or equal (same
as JL)
JLE
Jump if less than or equal (if op1 <= op2)
JNG
Jump if NOT greater (same as JLE)
SF ≠ OF
ZF = 1 or SF ≠ OF
Conditional Jump Applications
In Class Problems
1.
Will the following code jump to the label
named Target?
mov ax, 8109h
cmp ax, 26h
jg
Target
jg → signed conditional jump
(8109h < 26h) so no jump
In Class Problems
2.
Will the following code jump to the label
named Target?
mov ax, -30
cmp ax, -50
jg
Target
jg → signed conditional jump
(-30 > -50) so jump is taken
In Class Problems
3.
Will the following code jump to the label named
Target?
mov ax, -42
cmp ax, 26
ja
Target
ja → unsigned conditional jump
-42 = D6h,
26 = 1Ah
D6 > 1A so jump is taken
In Class Problems
4.
Write instructions that jump to label L1
when the unsigned integer in DX is less
than or equal to the integer in CX.
cmp
jbe
dx, cx
L1
In Class Problems
5.
Write instructions that jump to label L2
when the signed integer in AX is greater
than the integer in CX.
cmp
jg
ax, cx
L2
In Class Problems
6.
Write instructions that clear bits 0 and 1
in AL. If the destination operand is equal
to zero, jump to label L3. Otherwise,
jump to label L4.
AND AL, 0FCh
JZ
L3
JMP L4
Block-Structured IF Statements
If ( op1 == op2)
{
Mov eax, op1
Cmp eax, op2
Je
L1
Jmp L2
X = 1;
Y = 2;
}
L1:
Mov X, 1
Mov Y, 2
L2:
IF, Else Statements
If ( op1 == op2)
{
X = 1;
Y = 2;
}
Else
{
X = 4;
Y = 5;
}
Mov
Cmp
Je
Jmp
eax, op1
eax, op2
L1
L2
Mov
Mov
Jmp
X, 1
X, 2
L3
L1:
L2:
Mov, X, 4
mov X, 5
L3:
Logical AND Operator
Finding alternate ways to implement expressions may reduce the
amount of code needed. Using JBE instead of JA:
If (al > bl) AND (bl > cl)
Cmp
jbe
cmp
jbe
mov
{
x = 1;
}
Next:
al, bl
next
bl, cl
next
X, 1
Logical OR Operator
There are several ways to implement expressions
If (al > bl) OR (bl > cl)
Cmp
ja
cmp
jbe
{
x = 1;
}
al, bl
L1
bl, cl
next
L1:
mov X, 1
Next:
While Loop
While (val1 < val2)
{
val1++;
val2--;
}
mov
eax, val1
cmp
jnl
inc
dec
jmp
Endwhile:
mov
eax, val2
endwhile
eax
val2
while
While:
val1, eax
Table-Driven Selections
A way of using a table lookup to replace a
multiway selection structure.
 Create a table containing lookup values
and the offsets of labels or procedures,
and use a loop to search the table.


Link to ProcTble.asm
Part of a Table
.data
CaseTable
BYTE
DWORD
BYTE
DWORD
…..
‘1’ ;lookup value
Process_1
‘2’
Process_2
Lookup value
‘1’
00000120 ‘2’
00000130 ‘3’
00000140
Address of process_2