240 likes | 475 Views
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:
E N D
Ch. 5 Control Structures Comp Sci 251 -- Control structures
Control structures Assembly language implementation of • Selection (if, if-else, switch) • Iteration (while, do-while, for) Comp Sci 251 -- Control structures
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
if(x < y) x = 0; If (without else clause) if1 < >= X = 0; Comp Sci 251 -- Control structures
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
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
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
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
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
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
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
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
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
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
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
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
Loops • Pre-test: (while) • Post-test: (do-while) • Evaluate loop entry condition • Conditionally execute loop body Comp Sci 251 -- Control structures
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
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
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
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
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
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
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