1 / 23

Conditional Jump, Conditional Loop Instructions, and Conditional Structures

Conditional Jump, Conditional Loop Instructions, and Conditional Structures. 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.

ismail
Download Presentation

Conditional Jump, Conditional Loop Instructions, and Conditional Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

  3. Equality comparisons • Based on equality between operands, or value of (E)cx. • CMP destination, Source

  4. 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

  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: + 127 > -128

  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,10000000b ; 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. 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. 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 jmp Next 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. begin eax = op1 ebx = op2 ecx = op3 false true L2: inc eax L7: true false L3:ebx = = eax? L1: eax < ebx? op1 =eax L4: L5: X = 2 X = 3 L6: end

  23. 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 • ; }

More Related