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

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

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

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

Comp Sci 251 -- Control structures1

Ch. 5 Control Structures

Page 2: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures2

Control structures

Assembly language implementation of

Selection (if, if-else, switch)

Iteration (while, do-while, for)

Page 3: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures3

Comparison

High-level relational operators: < <= == > >= !=

MIPS conditional branch instructions:blt*, ble*, beq, bgt*, bge*, bne

Syntax: bxx Rsrc1, Src2, labelExamples: ble $t1, $t2, foo #branch if $t1 <= $t2

bgt $t1, 8, bar #branch if $t1 > 8

registerconstant

Page 4: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures4

If (without else clause)

if(x < y)

x = 0;if1

X = 0;

<>=

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

Comp Sci 251 -- Control structures5

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

Page 6: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures6

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

Page 7: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures7

Exercise: fill in the blank

if(x != 0)

x++;

lw $t0, x

lw $t0, x

addi $t0, $t0, 1

sw $t0, x

endif:

Page 8: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures8

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

Page 9: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures9

If-else -- improved versionfall 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:

Page 10: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures10

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

Page 11: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures11

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

Page 12: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures12

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

Page 13: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures13

Exercise: generate MIPS code

if(x < y && y < z)

x++;

else y++;

Code then clause first Code else clause first

Page 14: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures14

Another Exercise: generate MIPS code

if(x < y || y < z)

x++;

else y++;

Code then clause first Code else clause first

Page 15: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures15

Nested control structures

Then-clause, else-clause, loop body may contain– If or if-else – Do or do-while

Generate code for nested constructs

Page 16: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures16

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

then1then2 else2

else1

Page 17: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures17

Loops

Pre-test: (while) Post-test: (do-while)

Evaluate loop entry condition Conditionally execute loop body

Page 18: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures18

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:

Page 19: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures19

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

Page 20: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures20

Smarter pre-test loop code

j evalbody:

lw $t0, xaddi $t0, $t0, 1sw $t0, x

eval:lw $t0, xlw $t1, yblt $t0, $t1, body

Why is it smarter?

Page 21: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures21

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

Page 22: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures22

Loop summary

Always code loop body first, entry condition last

Pre-test: jump to entry condition before body Post-test: no jump before body

Page 23: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures23

For statement

Equivalent to Pre-test whilefor (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:

Page 24: Comp Sci 251 -- Control structures 1 Ch. 5 Control Structures.

Comp Sci 251 -- Control structures24

Switch statements

Can be treated as linear nested if-else

Can be implemented with “jump tables”– Sometimes quicker– More complex– Better studied after arrays