Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Similar presentations


Presentation on theme: "Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures."— Presentation transcript:

1 Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures

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

3 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

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

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

6 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

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

8 Comp Sci 251 -- Control structures 8 If-else -- first attempt 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: Unconditional Jump

9 Comp Sci 251 -- Control structures 9 If-else -- improved version fall through to then clause 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:

10 Comp Sci 251 -- Control structures 10 If-else -- code the else clause first 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: blt $t0, $t1, then

11 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

12 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++

13 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

14 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

15 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

16 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 if2 then1 then2else2 else1

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

18 Comp Sci 251 -- Control structures 18 Pre-test loop example 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:

19 Comp Sci 251 -- Control structures 19 Post-test loop exercise 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

20 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?

21 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

22 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

23 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:

24 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


Download ppt "Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures."

Similar presentations


Ads by Google