1 / 24

Ch. 5 Control Structures

Ch. 5 Control Structures. Control structures. Assembly language implementation of Selection (if, if-else, switch) Iteration (while, do-while, for). Comparison. High-level relational operators: < <= == > >= != MIPS conditional branch instructions:

brasen
Download Presentation

Ch. 5 Control 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. Ch. 5 Control Structures Comp Sci 251 -- Control structures

  2. Control structures Assembly language implementation of • Selection (if, if-else, switch) • Iteration (while, do-while, for) Comp Sci 251 -- Control structures

  3. Comparison • High-level relational operators: < <= == > >= != • MIPS conditional branch instructions: blt*, ble*, beq, bgt*, bge*, bne Syntax: bxx Rsrc1, Src2, label Examples: ble $t1, $t2, foo #branch if $t1 <= $t2 bgt $t1, 8, bar #branch if $t1 > 8 register constant Comp Sci 251 -- Control structures

  4. if(x < y) x = 0; If (without else clause) if1 < >= X = 0; Comp Sci 251 -- Control structures

  5. if(x < y) x = 0; endif: lw $t0, x lw $t1, y bge $t0, $t1, endif li $t0, 0 sw $t0, x endif: If (without else clause) Branch if greater or equal Comp Sci 251 -- Control structures

  6. More conditional branches beqz*, bnez*, bgez, bgtz, blez, bltz Syntax: bxxz Rsrc, label Examples: blez $t1, foo #branch if $t1 <= 0 bgtz $t1, bar #branch if $t1 > 0 * psuedo-instructions Comp Sci 251 -- Control structures

  7. if(x != 0) x++; lw $t0, x lw $t0, x addi $t0, $t0, 1 sw $t0, x endif: Exercise: fill in the blank Comp Sci 251 -- Control structures

  8. if(x < y) x++; else y++; lw $t0, x #1 lw $t1, y #2 blt $t0, $t1, then #3 j else #4 then: lw $t0, x #5 addi $t0, $t0, 1 #6 sw $t0, x #7 j endif #8 else: lw $t1, y #9 addi $t1, $t1, 1 #10 sw $t1, y #11 endif: If-else -- first attempt Unconditional Jump Comp Sci 251 -- Control structures

  9. if(x < y) x++; else y++; lw $t0, x #1 lw $t1, y #2 bge $t0, $t1, else #3 then: lw $t0, x #4 addi $t0, $t0, 1 #5 sw $t0, x #6 j endif #7 else: lw $t1, y #8 addi $t1, $t1, 1 #9 sw $t1, y #10 endif: If-else -- improved versionfall through to then clause Comp Sci 251 -- Control structures

  10. if(x < y) x++; else y++; lw $t0, x lw $t1, y else: lw $t0, $t0, y addi $t0, $t0, 1 sw $t0, y j endif then: lw $t0, x addi $t0, $t0, 1 sw $t0, x endif: If-else -- code the else clause first blt $t0, $t1, then Comp Sci 251 -- Control structures

  11. Boolean operators and short-circuiting • Stop evaluating Boolean expression as soon as possible • AND: stop as soon as an operand is False • OR: stop as soon as an operand is True Comp Sci 251 -- Control structures

  12. Pseudo code statement if(x < y && y < z) x++; else y++; If ( x >= y) goto else xlty: if ( y >= z) goto else then: x = x + 1; goto endif else: y = y + 1; endif: Boolean Table (x < y && y < z) true && true do x++ true && false do y++ false && true do y++ false && false do y++ Comp Sci 251 -- Control structures

  13. Exercise: generate MIPS code if(x < y && y < z) x++; else y++; • Code then clause first • Code else clause first Comp Sci 251 -- Control structures

  14. Another Exercise: generate MIPS code if(x < y || y < z) x++; else y++; • Code then clause first • Code else clause first Comp Sci 251 -- Control structures

  15. Nested control structures • Then-clause, else-clause, loop body may contain • If or if-else • Do or do-while • Generate code for nested constructs Comp Sci 251 -- Control structures

  16. Nested if-else example if1: if(x < 100) then1: print("small"); else1: else { if(x < 200) then2: print("medium"); else2: else print("large"); } if1 else1 if2 then1 then2 else2 Comp Sci 251 -- Control structures

  17. Loops • Pre-test: (while) • Post-test: (do-while) • Evaluate loop entry condition • Conditionally execute loop body Comp Sci 251 -- Control structures

  18. while(x < y) x++; endloop: Top: if ( x >= y) goto endloop; x = x + 1; goto Top: endloop: top: lw $t0, x lw $t1, y bge $t0, $t1 endloop lw $t0, x addi $t0, $t0, 1 sw $t0, x j top endloop: Pre-test loop example Comp Sci 251 -- Control structures

  19. do{ x++; }while(x < y); body:lw $t0, x addi $t0, $t0, 1 sw $t0, x eval: lw $t0, x lw $t1, y blt $t0, $t1, body Post-test loop exercise Comp Sci 251 -- Control structures

  20. Smarter pre-test loop code j eval body: lw $t0, x addi $t0, $t0, 1 sw $t0, x eval: lw $t0, x lw $t1, y blt $t0, $t1, body Why is it smarter? Comp Sci 251 -- Control structures

  21. Initial vs Smarter Pre-Test Loop Code top: Eval: lw $t0, x lw $t1, y bge $t0, $t1, endloop Body: lw $t0, x addi $t0, $t0, 1 sw $t0, x j top endloop: j eval body: lw $t0, x addi $t0, $t0, 1 sw $t0, x eval: lw $t0, x lw $t1, y blt $t0, $t1, body Comp Sci 251 -- Control structures

  22. Loop summary • Always code loop body first, entry condition last • Pre-test: jump to entry condition before body • Post-test: no jump before body Comp Sci 251 -- Control structures

  23. For statement • Equivalent to Pre-test while for (int i = 0; i < 10; i++) { //body;} Int i = 0; while ( i < 10) { // body; i ++; } Endwhile: Int I = 0; Begin: if ( I >= 10) goto Endwhile; { // body i++; goto Begin; } Endwhile: Comp Sci 251 -- Control structures

  24. Switch statements • Can be treated as linear nested if-else • Can be implemented with “jump tables” • Sometimes quicker • More complex • Better studied after arrays Comp Sci 251 -- Control structures

More Related