Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditionals and Functions

Similar presentations


Presentation on theme: "Conditionals and Functions"— Presentation transcript:

1 Conditionals and Functions
CS/COE 0447 Jarrett Billingsley

2 Class announcements project 1 due date pushed back a week to 10/5
cause I doubt y'all will be prepared for it otherwise (449 people: project 2 now due the week after that) CS447

3 Conditionals: if and if-else (and loops too)
CS447

4 Like mad libs, but for code
I'll use these 'blocks' to represent the basic blocks cause they don’t matter if(some condition) { } else { } block A block B block C CS447

5 A simple if with a simple condition
If there is no else, it's pretty simple. if(s0 == 30) { } block A block B beq s0, 30, blockA j blockB blockA: blockB: block A block B don't name things "blockA" "blockB" etc. this is just an example coming up with unique names for all the control flow labels is kind of a chore CS447

6 An if-else with a simple condition
more blocks now… beq s0, 30, blockA j blockB if(s0 == 30) { } else { block A block B block C blockA: blockB: blockC: block A block B block C j blockC we NEED THIS – the CPU doesn't see/care about your labels!! CS447

7 Do NOT branch to the next instruction
this is a very common mistake a branch to the next instruction does absolutely nothing maybe you try to translate this code: beq s0, 30, blockA blockA: block A if(s0 == 30) { } block A if s0 is 30, it will go to blockA. if s0 is not 30, it will… still go to blockA. - if I see anyone doing this, I WILL scream, loudly labels mean nothing to the CPU. CS447

8 A conditional trick CS447

9 It's a little wordy isn't it
there are a lot of labels and branches beq s0, 30, blockA j blockB if(s0 == 30) { } else { block A block B block C blockA: blockB: blockC: block A block B block C j blockC CS447

10 A little simplification
if you see a conditional branch followed by an unconditional one… invert the condition and get rid of the first label. …you can merge them like so: beq s0, 30, blockA j blockB blockA: j end blockB: end: block A block B bne s0, 30, else j end else: end: block A block B - compilers always do this. cause it's shorter, and shorter means faster. - if you look at the disassembly of compiled code (like in 449…) you have to keep this in mind. CS447

11 Opposite conditionals
what's the opposite of < (less than)? it's not >… it's >= Instruction Opposite beq bne blt bge ble bgt CS447

12 More complex conditions
CS447

13 NO! In this code… if(dog_size < 10 || dog_name() == "Fluffy")
if dog_size is 3, is dog_name() called? NO! this is short circuit evaluation. for || (logical OR), if the first condition is true, the second one is skipped. for && (logical AND), if the first condition is false, the second one is skipped. - we skip the second conditions cause there's no way for the result of the || or && to be true in those cases - and cause it just… makes more sense that way. - this is how we operate in real life: if the dog's size is less than 10, we don't also check its name. CS447

14 In this code… if(dog_size < 10) small(); else if(dog_size < 20) medium(); else if(dog_size < 30) large(); else enormous(); if dog_size == 3, is this condition checked? NO! once a true condition is found, no more conditions are checked. after small(), it comes down here. - when you see "if() {} else if() {}" it's really short for "if() {} else { if() {} }" - in that, it's clear that the second if is never run if the first if does CS447

15 And-and! if(s0 == 30 && s1 > 1) { } block A bne s0, 30, skipA
block A is run if both conditions are true. to think of it another way… it's skipped if either condition is false. if(s0 == 30 && s1 > 1) { } block A bne s0, 30, skipA ble s1, 1, skipA block A skipA: CS447

16 Or-or! if(s0 == 30 || s1 > 1) { } block A beq s0, 30, blockA
we go to block A if either condition is true. if(s0 == 30 || s1 > 1) { } block A beq s0, 30, blockA bgt s1, 1, blockA j skipA blockA: we could simplify this… how? block A skipA: - it would become ble s1, 1, blockA; and we'd remove j skipA and blockA: CS447

17 Functions CS447

18 What's a calling convention? (more 0449 stuff)
it ensures our programs don't trip over their own feet it's how machine-language functions call one another how arguments are passed how return values are returned how control flows into/out of the function what contracts exist between the caller and the callee void fork() { knife(); } void knife() { ... } caller callee CS447

19 The program counter register
a program's instructions are in memory, so they have addresses the PC (program counter) holds the address of the next instruction to run PC time 0x8000 top: 0x8000 lw t0, (s0) 0x8004 add t0, t0, 1 0x8008 sw t0, (s0) 0x800C add s0, s0, 4 0x8004 0x8008 0x800C 0x8010 blt s0, s1, top 0x8010 0x8000 ? 0x8014 btw what pattern do you notice about these addresses? - a conditional branch can set the PC to one of two values – the branch target (its label), or the next instruction - the addresses are multiples of 4 - this means instructions are 4 bytes each, and so they are 4 byte aligned CS447

20 caller callee The flow of control void fork() { knife(); spoon++; }
when the caller calls a function, where do we go? when the callee's code is finished, where do we go? void fork() { knife(); spoon++; } void knife() { spork++; spatula--; } caller callee - it doesn't matter if you use a return or not, when the callee finishes, it goes back to the line after the call in the caller. CS447

21 MIPS ISA: jal ra PC 0x8004 0x8000 li a0, 10 0x8004 jal func 0x8008
we call functions with jal: jump and link PC 0x8004 0x8000 li a0, 10 0x8004 jal func 0x8008 li v0, 10 ... this is what jal does: it jumps to a new location, and makes a link back to the old one in the ra (return address) register what address should go into PC next? PC 0x8C30 when func returns, where will we go? ra 0x8008 func: 0x8C30 li v0, 4 ... and this is ALL it does. CS447

22 The return address register
you can think of ra like a bookmark. you bookmark your position, and then read the other book… you're reading, and it references another book. see TAPL pp 407 408 ra and when you're done, you go back to the bookmark. - so if the second book refers you to a third, and you move the bookmark to the second book… - well now you lost your place in the first book. BUT YOU ONLY HAVE ONE BOOKMARK. CS447

23 MIPS ISA: jr ra PC 0x8C38 0x8000 li a0, 10 0x8004 jal func 0x8008
we return from functions with jr: jump to address in register now we're at the end of func. ra still has the proper return address PC 0x8C38 0x8000 li a0, 10 0x8004 jal func 0x8008 li v0, 10 ... ra 0x8008 jr ra copies ra into pc. PC 0x8008 and this is ALL it does. func: 0x8C30 li v0, 4 0x8C34 syscall 0x8C38 jr ra - jr can be used with any register, not just ra - this is how things like virtual methods work – we look up an address to jump to in a table, load it into a register, and jump to that CS447

24 BUT YOU ONLY HAVE ONE BOOKMARK.
let's track PC and ra as we run this code. PC ra 0x8000 jal fork 0x8004 li v0, 10 ... 0x8020 jal spoon 0x8024 jr ra 0x8040 jr ra 0x8000 0x0000 After jal fork: 0x8020 0x8004 fork: After jal spoon: 0x8040 0x8024 After jr ra: 0x8024 0x8024 After jr ra: 0x8024 0x8024 UHHHHHHHH spoon: After jr ra: 0x8024 0x8024 After jr ra: 0x8024 0x8024 After jr ra: 0x8024 0x8024 CS447 After jr ra: 0x8024 0x8024

25 What's the deal? (animated)
there's only one return address register. ra main's AR we lost our way back to the first book. and now we're stuck in an infinite loop. where do we put things when we don't have room in the registers? - we put things in MEMORYYYYYYYYYYYYYY - we'll get to that next tiiiiiiime CS447


Download ppt "Conditionals and Functions"

Similar presentations


Ads by Google