# Conditional Jumps Jcond destination  Cond refers to a flag condition  Destination should be a local label  Destination must be within –128 to +127 bytes.

## Presentation on theme: "Conditional Jumps Jcond destination  Cond refers to a flag condition  Destination should be a local label  Destination must be within –128 to +127 bytes."— Presentation transcript:

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. CMPal, 0 jzL1;jump to L1 if ZF = 1 2. ANDdl, 1011 0000b jnzL2;jump to L2 if ZF = 0

Jumps Based on Specific Flag Values(10) MnemonicDescriptionFlags/Registers JZif zeroZF = 1 JNZif not zeroZF = 0 JCif carryCF = 1 JNCif not carryCF = 0 JOIf overflowOF = 1 JNOIf not overflowOF = 0 JSif signedSF = 1 JNSif not signedSF = 0 JPif parity (even)PF = 1 JNPif not parity (odd)PF = 0

MnemonicDescriptionFlags/Registers JEif equalZF = 1 JNEif not equalZF = 0 JCXZif CX = 0ZF = 0 JECXZif ECX = 0ZF = 0 Jumps Based on Equality(4)

Jumps Based on Unsigned Comparisons(8)

Jumps Based on Signed Comparisons(8)

Conditional Jump Applications

In Class Problems 1. Will the following code jump to the label named Target? movax, 8109h cmpax, 26h jgTarget jg →signed conditional jump (8109h < 26h) so no jump

In Class Problems 2. Will the following code jump to the label named Target? movax, -30 cmpax, -50 jgTarget jg →signed conditional jump (-30 > -50) so jump is taken

In Class Problems 3. Will the following code jump to the label named Target? movax, -42 cmpax, 26 jaTarget 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. cmpdx, cx jbeL1

In Class Problems 5. Write instructions that jump to label L2 when the signed integer in AX is greater than the integer in CX. cmpax, cx jgL2

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 JZL3 JMPL4

Block-Structured IF Statements If ( op1 == op2) { X = 1; Y = 2; } Moveax, op1 Cmp eax, op2 JeL1 JmpL2 L1: MovX, 1 Mov Y, 2 L2:

IF, Else Statements If ( op1 == op2) { X = 1; Y = 2; } Else { X = 4; Y = 5; } Moveax, op1 Cmp eax, op2 JeL1 JmpL2 L1: MovX, 1 Mov X, 2 JmpL3 L2: Mov, X, 4 movX, 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) { x = 1; } Cmp al, bl jbenext cmpbl, cl jbenext movX, 1 Next:

Logical OR Operator There are several ways to implement expressions If (al > bl) OR (bl > cl) { x = 1; } Cmp al, bl jaL1 cmpbl, cl jbenext L1: movX, 1 Next:

While Loop While (val1 < val2) { val1++; val2--; } mov eax, val1 While: cmpeax, val2 jnlendwhile inceax decval2 jmpwhile Endwhile: movval1, 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 CaseTableBYTE‘1’;lookup value DWORDProcess_1 BYTE‘2’ DWORD Process_2 ….. ‘1’ 00000120‘2’ 00000130‘3’ 00000140 Address of process_2 Lookup value

Download ppt "Conditional Jumps Jcond destination  Cond refers to a flag condition  Destination should be a local label  Destination must be within –128 to +127 bytes."

Similar presentations