1 / 36

Chapter 5 Branching and Looping

Chapter 5 Branching and Looping. 5.1 Unconditional Jumps. jmp Instruction. Like a goto in a high-level language Format: jmp StatementLabel The next statement executed will be the one at StatementLabel:. Program Design: Calculate 1+2+3+…. number := 0; sum := 0; forever loop

kerryn
Download Presentation

Chapter 5 Branching and Looping

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. Chapter 5 Branching and Looping

  2. 5.1 Unconditional Jumps

  3. jmp Instruction • Like a goto in a high-level language • Format: jmp StatementLabel • The next statement executed will be the one at StatementLabel:

  4. Program Design: Calculate 1+2+3+… number := 0; sum := 0; forever loop add 1 to number; add number to sum; end loop;

  5. Program Code: Calculate 1+2+3+… ; program to find sum 1+2+...+n for n=1, 2, ... .586 .MODEL FLAT .STACK 4096 .DATA .CODE main PROC mov ebx,0 ; number := 0 mov eax,0 ; sum := 0 forever: inc ebx ; add 1 to number add eax, ebx ; add number to sum jmp forever ; repeat main ENDP END

  6. Program Stopped at Breakpoint

  7. jmp Encoding • Relative short encodes a single byte signed displacement telling how far forward or backward to jump for the next instruction to execute – the assembler uses this format if possible • Relative near encodes a signed doubleword displacement – this allows a forward or backward jump essentially anywhere in memory • Indirect forms that encode the address of the destination in a register or memory are not often used

  8. 5.2 Conditional Jumps, Compare Instructions and if Structures

  9. Conditional Jump Instructions • Format: j--targetStatement • The last part of the mnemonic identifies the condition under which the jump is to be executed • If the condition holds, then the jump takes place and the next statement executed is at targetStatement: • Otherwise, the next instruction (the one following the conditional jump) is executed • Used to implement if structures, other selection structures, and loop structures in 80x86 assembly language

  10. Conditional Jumps and Flags • Most “conditions” considered by the conditional jump instructions are settings of flags in the EFLAGS register. • Examplejz endWhilemeans to jump to the statement with label endWhile if the zero flag ZF is set to 1 • Conditional jump instructions don’t modify flags; they react to previously set flag values

  11. cmp Instructions • Most common way to set flags for conditional jumps • Format: cmp operand1, operand2 • Flags are set the same as for the subtraction operation operand1 – operand2 • Operands are not changed

  12. Conditional Jumps To Use After Signed Operand Comparison mnemonic jumps if jg jump if greater SF=OF and ZF=0 jnle jump if not less or equal jge jump if greater or equal SF=OF jnl jump if not less jl jump if less SFOF jnge jump if not above or equal jle jump if less or equal SFOF or ZF=1 jng jump if not greater

  13. Conditional Jumps To Use After Unsigned Operand Comparison mnemonic jumps if ja jump if above CF=0 and ZF=0 jnbe jump if not below or equal jae jump if above or equal CF=0 jnb jump if not below jb jump if below CF=1 jnae jump if not above or equal jbe jump if below or equal CF=1 or ZF=1 jna jump if not above

  14. Some Other Conditional Jumps mnemonic jumps if je jump if equal ZF=1 jz jump if zero jne jump if not equal ZF=0 jnz jump if not zero js jump if sign (negative) SF=1 jc jump if carry CF=1 jo jump if overflow OF=1

  15. Example Usage cmp eax, nbr jle smaller • The jump will occur if the value in EAX is less than or equal than the value in nbr, where both are interpreted as signed numbers

  16. Code cmp ebx, 10 jnl elseLarge inc smallCount jmp endValueCheck elseLarge: inc largeCount endValueCheck: Assumptions value in EBX smallCount and largeCount in memory if Example 1 Design if value < 10 then add 1 to smallCount; else add 1 to largeCount; end if;

  17. Code cmp total, 100 jge addValue cmp ecx, 10 jne endAddCheck addValue: mov ebx, value add total, ebx endAddCheck: Assumptions total and value in memory count in ECX if Example 2 Design if (total  100) or (count = 10) then add value to total; end if;

  18. 5.3 Implementing Loop Structures

  19. while Loops • while pseudocode design while continuation condition loop ... { body of loop } end while; • Typical while implementation while1: . ; code to check Boolean expression . . body: . ; loop body . . jmp while1 ; go check condition again endWhile1:

  20. Continuation Condition • A Boolean expression • Checked before the loop body is executed • Whenever it is true the loop body is executed and then the continuation condition is checked again • When it is false execution continues with the statement following the loop. • It may take several 80x86 statements to evaluate and check a continuation condition

  21. Code whileSum: cmp sum, 1000 jnl endWhileSum add sum, ecx inc ecx jmp whileSum endWhileSum: Assumptions sum in memory count in ECX while Example Design while (sum < 1000) loop add count to sum; add 1 to count; end while;

  22. for Loops • Counter-controlled loop • for pseudocode design for index := initialValue to finalValue loop ... { body of loop } end for; • Loop body executed once for each value of the loop index in the given range

  23. for Implementation • Convert for loop to equivalent while loop index := initialValue; while index≤finalValue loop ... { body of loop } add 1 to index; end while; • Implement while loop in 80x86 code • Section 5.4 shows another implementation

  24. until Loops • until pseudocode design repeat ... { body of loop } until termination condition; • Termination condition checked after the loop body is executed • If true execution continues with the statement following the until loop • If false the loop body is executed again

  25. Code repeatLoop: add sum, ecx add sum, ecx inc ecx cmp sum, 1000 jng repeatLoop endUntilLoop: Assumptions sum in memory count in ECX until Example Design repeat add 2*count to sum; add 1 to count; until (sum > 1000);

  26. 5.4 for Loops in Assembly Language

  27. for Loops • Can be implemented by converting into while loops • 80x86 loop instruction designed to implement “backward” counter-controlled loops: for index := count downto 1 loop ... { body of loop } end for;

  28. loop Instruction • format: loop statementLabel • statementLabel is the label of a statement which is a short displacement from the loop instruction • execution • The value in ECX is decremented • If the new value in ECX is zero, then execution continues with the statement following the loop instruction • If the new value in ECX is non-zero, then a jump to the instruction at statementLabel takes place

  29. Code mov eax, 0 mov ecx, 20 forCount: add eax, ecx loop forCount Assumptions sum in EAX count in ECX for Example Design sum := 0 for count := 20 downto 1 loop add count to sum; end for;

  30. Cautions • If ECX is initially 0, then 00000000 will be decremented to FFFFFFFF, then FFFFFFFE, etc., for a total of 4,294,967,296 iterations • The jecxz (“jump if ECX is zero”) instruction can be used to guard a loop implemented with the loop instruction

  31. 5.5 Arrays

  32. Defining an Array • Typically declare a collection of contiguous elements in the data section • Examples • array1 DWORD 25, 47, 15, 50, 32creates an array of 5 doublewords with initial values • array2 DWORD 1000 DUP (?)creates an array of 1000 logically uninitialized doublewords

  33. Sequential Array Access • Put the address of the first element in a register (typically with a lea instruction) • Register indirect addresssing allows the register to “point at” the array element to be used • Add the element size to the register to point at the next element

  34. Example: Using Sequential Access to Add 50 Doublewords in an Array nbrArr DWORD 50 DUP (?) ... mov eax, 0 ; sum := 0 lea esi, nbrArr ; load array address mov ecx, 50 ; number of elements addElt: add eax, [esi] ; add array element add esi, 4 ; point at next element loop addElt ; repeat

  35. Random Array Access • Use indexed addressing • Example nbrArray[4*ecx]where nbrArray references the array, ECX is the index of the element to be accessed and scaling factor 4 gives doubleword element size

  36. Example: Using Random Access to Add 50 Doublewords in an Array nbrArr DWORD 50 DUP (?) ... mov eax, 0 ; sum := 0 mov ecx, 50 ; number of elements mov esi, 0 ; array index addElt: add eax, nbrArr[4*esi] ; add element inc esi ; increment array index loop addElt ; repeat

More Related