CS 0447 Introduction to Computer Programming Luís Oliveira
Fall 2020
Flow, Conditionals, and Loops
Original slides by: Jarrett Billingsley Modified with bits from: Bruce Childers, David Wilkinson
#6
Flow, Conditionals, and Loops Lus Oliveira Original slides by: - - PowerPoint PPT Presentation
#6 CS 0447 Introduction to Computer Programming Flow, Conditionals, and Loops Lus Oliveira Original slides by: Jarrett Billingsley Modified with bits from: Bruce Childers, David Wilkinson Fall 2020 So far Putting numbers into
CS 0447 Introduction to Computer Programming Luís Oliveira
Fall 2020
Original slides by: Jarrett Billingsley Modified with bits from: Bruce Childers, David Wilkinson
#6
So far…
2
li a0, 3 move a0, t0 lw/sw, lh/lhu/sh, lb/lbu/sb lw t0, x sw t0, x la t1, x lw t0, 0(t1) la t1, x sw t0, 0(t1) la a0, x .data x: .word 4 label Do the same thing Unsigned! These do zero extension
In another perspective
Other operations
add sub mul syscall …
3
CPU
Registers
Memory
lw, lh, lhu, lb, lbu sw, sh, sb move li, la
Datatypes
word half byte asciiz …
called dead code.
4
5
With great power…
– And you'll be sad » This is like 90% of the bugs
6
Getting a little further from familiarity
_main_loop: # clear screen # draw one thing # sleep # draw another thing # etc j _main_loop
7
this is an infinite loop, which is sometimes useful but not too interesting j stands for ”jump" – go somewhere else
Building blocks
8
do_thing
x == w - 1?
third_thing
thinking about this is REAL HELPFUL
Essentially…
9
10
MIPS ISA: conditional branch instructions
11
Instruction Meaning beq a, b, label if(a == b) { goto label } bne a, b, label if(a != b) { goto label }
above, a must be a register, but b can be a register or immediate (by the powers of the pseudo-instruction)
How do these work?
12
Next instruction Other instruction t0==t1
label:
Previous instruction
This is the branch False True
beq t0, t1, label # branch if equal
How do these work?
13
beq t0, t1, label # branch if equal
Next instruction Other instruction t0==t1
label:
Previous instruction
This is the branch False True
How to write asm (again!)
14
WRITE PSEUDOCODE ALWAYS REALLY!!!
Like mad libs, but for code
15
A simple conditional block (if)
16
if(s0 == 30) { } block A block B bne s0, 30, blockB blockA: blockB:
A simple conditional block (if)
17
if(s0 == 30) { } block A block B bne s0, 30, blockB
A simple conditional block (if)
18
if(s0 == 30) { } block A block B blockA: blockB: block A block B bne s0, 30, blockB
An if-else with a simple condition
19
if(s0 == 30) { } else { } block A block B block C blockB: blockC: block A block B block C bne s0, 30, blockB j blockC
we NEED THIS – the CPU doesn't see/care about your labels!!
The other way around
20
if(s0 == 30) { } else { } block A block B block C j blockExit # skip the else blockElse: blockExit: block A block B block C beq s0, 30, blockA j blockElse blockA:
MIPS ISA: conditional branch instructions
21
Instruction Meaning bltz a, label if(a < 0) { goto label } blez a, label if(a <= 0) { goto label } bgtz a, label if(a > 0) { goto label } bgez a, label if(a >= 0) { goto label }
MIPS ISA: set if less than
22
Instruction Meaning slt c, a, b if(a < b) { c = 1 } else { c = 0 }
Set if Less Than: register c will be set to 1 if a<b. Otherwise, register c will be set to 0. Using slt together with bne and beq all conditionals can be implemented! a=b , a≠b, a>b, a≥b, a<b, a≤b
Thanks, De Morgan
MIPS ISA: conditional branch instructions
23
Instruction Meaning blt a, b, label if(a < b) { goto label } ble a, b, label if(a <= b) { goto label } bgt a, b, label if(a > b) { goto label } bge a, b, label if(a >= b) { goto label }
above, a must be a register, but b can be a register or immediate
Examples
24
slt t, b, a bne t, zero, label bgt a, b, label Example 1: branch if a>b slt t, a, b beq t, zero, label bge a, b, label Example 2: branch if a≥b Solution: branch if b<a Solution: branch if !(a<b) # Goto label if a>b # t=1 if b<a # Goto label if t≠0 # Goto label if a≥b # t=1 if a<b # Goto label if t=0
25
In this code… if(dog_size < 10 || dog_name() == "Fluffy")
26
if dog_size is 3, is dog_name() called?
this is short circuit evaluation. for || (logical OR), if the first condition is true, the second one is skipped. (cause there's no way for the result of the OR to be false.) for && (logical AND), if the first condition is false, the second one is skipped.
In this code… if(dog_size < 10) small(); else if(dog_size < 20) medium(); else if(dog_size < 30) large(); else enormous();
27
if dog_size is 3, is this condition checked?
more conditions are checked. after small(), it comes down here.
And-and!
28
if(s0 == 30 && s1 > 1) { } block A bne s0, 30, skipA ble s1, 1, skipA skipA: block A
Or-or!
29
if(s0 == 30 || s1 > 1) { } block A beq s0, 30, blockA ble s1, 1, skipA blockA: skipA: block A
30
Dis-assembling a for-loop
31
for(i=0; i<10; i++) { } // carry on
And??? Then… Finally? What is the first thing a for does?
Initialize: i=0 Check condition: Go back up to the top execute while i<10 Increment: i++ block A block A
Looping in MIPS assembly
32
__________________
which conditional branch?
carry_on:
How do we go up? Let’s start with a recipe
_______________ loop_top: # carry on
What’s the first thing a for does?
__________ li s0, 0 for(i=0; i<10; i++) { } // carry on ____________ addi s0, s0, 1
How do we increment?
j loop_top block A block A
That’s bge, actually
the assembly to skip over code
33
_____________________
We want to leave the loop… when the opposite of i<10 happens!
move a0, ___ carry_on: _______________ loop_top: li v0, 1 syscall s0 # carry on __________ bge s0, 10, carry_on ____________ addi s0, s0, 1 j loop_top li s0, 0
The other way around
34
move a0, ___ carry_on: _______________ loop_top: li v0, 1 syscall s0 # carry on __________ blt s0, 10, loop_code ____________ addi s0, s0, 1 j loop_top li s0, 0 loop_code: b carry_on
While looks the same, no initialization or increment
35
while(s2 < 10) { // stuff!! } // more stuff bge s2, 10, ________ stuff: # stuff!! more_stuff: # more stuff more_stuff j loop_top loop_top: