Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditional Loop Instructions, Conditional Structures

Similar presentations


Presentation on theme: "Conditional Loop Instructions, Conditional Structures"— Presentation transcript:

1 Conditional Loop Instructions, Conditional Structures
Conditional Jump, Conditional Loop Instructions, and Conditional Structures

2 Type of conditional jump instruction
Based on specific flag Based on equality between operands, or value of (E)cx. Based on comparisons of unsigned operands. Based on comparison of signed operands. Mnemonic Description Flags JZ Jump if zero ZF = 1 JNZ Jump if not zreo ZF = 0 JC Jump if carry CF = 1 JNC Jump if not carry CF = 0 JO Jump if overflow OF = 1 JNO Jump if not overflow OF = 0 JS Jump if signed SF = 1 JNS Jump if not signed SF = 0 JP Jump if parity (even) PF = 1 JNP Jump if not parity (odd) PF = 0

3 Jump if destination = source
Equality comparisons Based on equality between operands, or value of (E)cx. CMP destination, Source Mnemonic Description JE Jump if destination = source JNE Jump if not equal JCXZ Jump if CX=0 JECXZ Jump if ECX = 0

4 CMP destination, Source
Unsigned Comparisons Jumps based on comparisons of unsigned integers are useful when comparing unsigned values, such as 7FFh and 8000h, where 7FFh is smaller than 8000h latter. CMP destination, Source Mnemonic Description JA Jump if above (destination > source) JNBE Jump if not below or equal (same as JA) JAE Jump if above or equal (destination >= source) JNB Jump if not below ( same as JAE) JB Jump if below JNAE Jump if not above or equal ( same as JB) JBE Jump if below or equal ( destination <= source) JNA Jump if not above ( same as JBE)

5 Signed Comparison It is used when the numbers you are comparing can be interpreted as signed values. CMP destination, Source Example: mov al,7Fh ; (7Fh or +127) Cmp al,80h ; (80h or -128 ) Ja Isabove ; no: 7Fh not> 80 h Jg isGreater ; yes: > -128 Mnemonic Description JG Jump if Greater (destination > source) JNLE Jump if not less than or equal (same as JG) JGE Jump if Greater than or equal (destination >= source) JNL Jump if not Less than ( same as JGE) JL Jump if less ( destination < source ) JNGE Jump if not Greater or equal ( same as JL) JLE Jump if Less than or equal ( destination <= source) JNG Jump if not greater ( same as JLE)

6 Application: Larger of two integers. The following code compares the unsigned integers in Ax and Bx and moves the larger of the two to DX: mov dx,ax ; assume Ax is larger cmp ax,bx ; if Ax is .= BX then jae L1 ; jump if AX>=BX to L1 mov dx,bx ; else move BX to DX L1:

7 Application: The following instructions compare the unsigned values in the three variables V1, V2, V3 and move the smallest of the three to Eax: .data V1 Dword ? V2 Dword ? V3 Dword ? .code mov eax, V1 ; assumes V1 is smallest cmp eax,V2 ; if eax <= V2 then jbe L1 ; jump to L1 mov eax, V2 ; else move V2 to ax L1: cmp eax,V3 ; if eax <= v3 then jbe L2 ; jump to L2 mov eax, V3 ; else move to eax L2:

8 Scanning array for first non zero value.
Title Scanning an Array .data intArray dword 0,0,0,0,1,20,35,12,66,4,0 noneMsg byte “A non-Zero value was not found”,0 .code Main proc Mov ebx,offset intArray Mov ecx, lenghtof intArray L1: Cmp [ebx],0 Jnz Found Add ebx,4 Loop L1 Jmp Notfound Found: Mov eax,[ebx] Call writeint Jmp Quit Notfound: Mov edx,offset nonMsg Call writestring Quit: Call crlf Exit Main endp End main

9 Conditional Loop instructions.
Loopz and Loope instructions. The LOOPZ instruction permits a loop to continue while the Zero flag is set and the unsigned value of ECX is greater than zero. The destination label must be between -128 byte and +127 from the location of the following instruction. LOOPZ destination LOOPE instruction is equal to LOOPZ because they share the same circuitry, this is the execution logic of loopz and loope: ECX = ECX-1 If ECX>0 and ZF=1 jump to destination

10 LOOPNZ and LOOPNE instructions
The LOOPNZ ( loop if not zero) instruction is the counter part of LOOPZ. The loop continues while the unsigned value of ECX is greater than Zero and the Zero flag is clear. The syntax is: LOOPNZ destination The LOOPNE (loop if not equal) instruction is equivalent to LOOPNZ. They share the same circuitry. This is the execution logic of LOOPNZ and LOOPNE ECX = ECX - 1 If ECX > 0 and ZF = 0, jump to destination Otherwise no jump occurs and control passes to the next instruction

11 TITLE Scanning for a Positive Value (Loopnz.asm)
; Scan an array for the first positive value. If positive value found ESI is left pointing at it. If The ;loop fails to find a positive number, it stops when ecx equal to zero. In this case JNZ jumps to quit ;and ESI points to sentinel value (0) stored immediately after the array. INCLUDE Irvine32.inc .data array SWORD -3,-6,-1,-10,10,30,40,4 sentinel SWORD 0 .code main PROC mov esi,OFFSET array mov ecx,LENGTHOF array next: mov ebx,[esi] ; move element of array to ebx test ebx, b ; test highest bit of ebx pushfd ; push flags on stack add esi, 2 popfd ; pop flags from stack loopnz next ; continue loop jnz quit ; none found sub esi, 2 ; SI points to value quit: call crlf exit main ENDP END main

12 Conditional Structure
Conditional structures are conditional expressions that trigger a choice between different logical branches. Each branch causes a different sequence of instructions to execute

13 Block-Structured IF Statement
In most high level languages an IF statement implies that a boolean expression is followed by two lists of statements. One performed when the expression is true and another performed when the expression is false. If (expression) Statement list 1 Else Statement list 2 The else portion of the statement is optional. The following flowchart shows the two branching path in the conditional if structure, labeled true and false.

14 start Boolean expression TRUE FALSE Statement list 1 Statement list 2 end

15 Compile the following C++ if statement to assembly code
Example Compile the following C++ if statement to assembly code If (op1 == op2) { X = 1; Y = 2; } Answer: mov eax, op1 cmp eax, op2 ; compare EAX to op2 je L1 ; jump if equal to L1 jmp L2 ; otherwise, jump to L2 L1: mov X, 1 mov Y, 2 L2:

16 Compound expressions Logical AND operator
You can implement a boolean expression that uses the logical AND operator in at least two ways. Consider the following compound expression written in Pseudo code. If (al > bl) AND (bl > cl) { X = 1 } For any given compound expression there are at least several ways to implemented it in assembly. We are implementing two ways of the above compound expression.

17 cmp al, bl ;first expression ja L1 jmp Next L1:
If (al > bl) AND (bl > cl) { X = 1 } cmp al, bl ;first expression ja L1 jmp Next L1: cmp bl, cl ; second expression ja L2 L2: ; both are true mov X, 1 ; set X to 1 Next: We will assume that the values are unsigned. The implementation using JA (jump if above) cmp al, bl ; first expression jbe Next ; quit if false cmp bl, cl ; second expression jbe next ; quit if false mov X, 1 ; both are true Next: We can simplify the code if we reverse the JA condition and use JBE instead

18 Logical OR operator When multiple expression occur in a compound expression using the logical OR operator the expression is automatically true as soon as one expression is true Example: If (al > bl) OR (bl > cl) X = 1 In the following implementation, the code branches to L1 if the first expression is true; otherwise it falls through the second CMP instruction. The second expression reverse the > operator and uses JBE instead. cmp al, bl ja L1 cmp bl, cl jbe next L1: mov X, 1 next:

19 While Loops The WHILE structure test a condition first before performing a block of statements. As long as the loop condition remains true, the statement are repeated. while (val1 < val2) { Val1++; Val2--; } When coding this structure in assembly language, it is convenient to reverse the loop condition and jump to endwhile when the condition becomes true

20 Assuming that val1 and val2 are variables
mov eax, val1 ; copy variable to eax while: cmp eax, val2 ; if not (val1 < val2) jnl endwhile ; exit the loop inc eax ; val1++ dec val2 ; val2-- jmp while ; repeat the loop endwhile: mov val1, eax ; save new value for val1

21 Example: IF statement Nested in a Loop
High-level structure languages are particularly good at representing nested control structure. In the following C++ example, an IF statement is nested inside a WHILE loop. While (op1 < op2) { Op1 ++; If (op2 == op3) X = 2; Else X = 3; } To simplify the translation, in the following flowchart, the registers have been substituted for variables (EAX = op1, EBX = op2, and ECX = op3)

22 eax = op1 ebx = op2 ecx = op3 op1 =eax end begin false inc eax X = 2 X = 3 L7: L6: L5: L4: L2: true L3: ebx = = eax? L1: eax < ebx?

23 cmp eax, ebx ; EAX < EBX? jl L2 ; true jmp L7 ; false L2 inc eax
Assembly code mov eax, op1 mov ebx, op2 mov ecx, op3 L1: cmp eax, ebx ; EAX < EBX? jl L2 ; true jmp L7 ; false L2 inc eax L3: cmp ebx, ecx ; EBX = = ECX? je L4 ; true jmp L5 ; false L4: mov X, 2 ; X = 2 jmp L6 L5: mov X, 3 ; X = 3 L6: Jmp L1 ; repeat the loop L7: mov op1, eax ; update op1 ;While (op1 < op2) ;{ ;Op1 ++ ;If (op2 == op3) ;X = 2 ;Else ;X = 3 ; }


Download ppt "Conditional Loop Instructions, Conditional Structures"

Similar presentations


Ads by Google