340 likes | 779 Views
Conditional Jumps. J cond 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
E N D
Conditional Jumps • Jconddestination • Condrefers 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 • There are not any high-level logic structures in the IA-32 Instruction set. • But any logic structure can be implemented using a combination of comparisons and jumps.
Implementing any high-level logic structure • 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. • CMP al, 0 ;sets the zero flag if al = 0 jz L1 ;jump to L1 if ZF = 1 • AND dl, 1011 0000b jnz L2 ;jump to L2 if ZF = 0
In Class Problems • Will the following code jump to the label named Target? mov ax, 8109h cmp ax, 26h jg Target ??? • jg is a signed conditional jump (8109h < 26h) so no jump
In Class Problems • 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 • 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 • Write instructions that jump to label L1 when the unsigned integer in DX is less than or equal to the integer in CX. • cmp dx, cx jbe L1
In Class Problems • Write instructions that jump to label L2 when the signed integer in AX is greater than the integer in CX. • cmp ax, cx jg L2
In Class Problems • 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
Scanning an Array • A common task is to search for specific values in an array. • When the first match is found, it is common to either display its value or return a pointer to its location. • Example • ArryScan.asm looks for the first nonzero value in an array of 16-bit integers. If it finds one, it displays the value; otherwise, it displays a message stating that a value could not be found.
Application: String Encryption • XOR can be used twice to provide simple data encryption. • XOR a message string (plain text) with a string (key) to encrypt. • XOR the encrypted message (cipher text) with a string to decrypt and produce the original string • Symmetric encryption • A process where the same key is used for both encryption and decryption. • Example Encrypt.asm
Conditional Loop Instructions • LOOPZ – Loop if zero flag is set and the unsigned value of ECX is greated that zero. • The destination label must be between –128 and +127 bytes from the location of the next instruction. • LOOPZ destination
Conditional Loop Instructions • LOOPE – loop if equal shares the same circuitry as LOOPZ. • ECX = ECX –1 • If ECX > 0 and ZF =1, jump to destination
Conditional Loop Instructions • LOOPNZ – loop if not zero • LOOPNE – loop if not equal to • ECX = ECX –1 • If ECX > 0 and ZF =0, jump to destination • Example • Loopnz.asm scans each number in an array until a positive number is found (signed number declaration is used).
If ( op1 == op2) { X = 1; Y = 2; } Mov eax, op1 Cmp eax, op2 Je L1 Jmp L2 L1: Mov X, 1 Mov Y, 2 L2: Block-Structured IF Statements
If ( op1 == op2) { X = 1; Y = 2; } Else { X = 4; Y = 5; } Mov eax, op1 Cmp eax, op2 Je L1 Jmp L2 L1: Mov X, 1 Mov X, 2 Jmp L3 L2: Mov, X, 4 mov X, 5 L3: IF, Else Statements
If (al > bl) AND (bl > cl) { x = 1; } Cmp al, bl jbe next cmp bl, cl jbe next mov X, 1 Next: Logical AND OperatorFinding alternate ways to implement expressions may reduce the amount of code needed. Using JBE instead of JA:
If (al > bl) OR (bl > cl) { x = 1; } Cmp al, bl ja L1 cmp bl, cl jbe next L1: mov X, 1 Next: Logical OR OperatorThere are several ways to implement expressions
While (val1 < val2) { val1++; val2--; } mov eax, val1 While: cmp eax, val2 jnl endwhile inc eax dec val2 jmp while Endwhile: mov val1, eax While Loop
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 ‘1’ ;lookup value DWORD Process_1 BYTE ‘2’ DWORD Process_2 ….. Lookup value ‘1’ 00000120 ‘2’ 00000130 ‘3’ 00000140 Address of process_2
Application: Finite State Machines • We studied FSMs in Digital • A specific instance of a more general type of structure called a directed graph (or digraph).
Translation of FSMs to Assembly Language Code • Each state in the diagram is represented in the program by a label. • The following actions are performed at each label: • A call to an input procedure reads the next character from input. • If the state is a terminal state, check to see if the user has pressed the Enter key to end the input • One or more compare instructions check for each possible transition leading away from the state. Each comparison is followed by a conditional jump instruction.