Conditionals and Functions

Slides:



Advertisements
Similar presentations
Branches Two branch instructions:
Advertisements

The University of Adelaide, School of Computer Science
CDA 3100 Recitation Week 8. Question 1.data A:.word 21,3,2,9,100,22,6,15,33,90.text.globl main main: la $a0, A li $a1, 17 li $a2, 10 jal funct li $v0,
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
MIPS ISA-II: Procedure Calls & Program Assembly. (2) Module Outline Review ISA and understand instruction encodings Arithmetic and Logical Instructions.
Chapter 2 — Instructions: Language of the Computer — 1 Branching Far Away If branch target is too far to encode with 16-bit offset, assembler rewrites.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
CS 536 Spring Code generation I Lecture 20.
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
Code Generation CS 480. Can be complex To do a good job of teaching about code generation I could easily spend ten weeks But, don’t have ten weeks, so.
Lecture 5: Procedures. Function call book-keeping in C main() { int i,j,k,m;... i = mult(j,k);... m = mult(i,i);... } /* really dumb mult function */
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Higher Level languages have adopted a standard Referred to as C-style calling Uses the stack.
Lecture 8. MIPS Instructions #3 – Branch Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System Education.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
CS 312 Computer Architecture & Organization
Computer Science 210 Computer Organization
Lecture 5: Procedure Calls
Lecture 6: Assembly Programs
Functions and the Stack
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
MIPS Procedures.
MIPS Coding Continued.
CS/COE 0447 (term 2181) Jarrett Billingsley
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Procedures (Functions)
Conditional Branches What distinguishes a computer from a simple calculator is its ability to make decisions Decisions are made using the if statement,
The Interconnect, Control, and Instruction Decoding
Lecturer PSOE Dan Garcia
Pick up the handout on your way in!!
Instructions - Type and Format
C – Scope, Lifetime, and the Stack
MIPS Procedures.
MIPS Instruction Encoding
Arrays, Control flow, and Loops
MIPS Functions.
MIPS Instruction Encoding
More Functions and the Stack
MIPS Functions.
MIPS Procedures.
Review.
CS/COE 0447 Jarrett Billingsley
CS/COE 0447 Jarrett Billingsley
MIPS Functions.
Lecture 6: Assembly Programs
Systems Architecture I
MIPS Coding Continued.
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
9/27: Lecture Topics Memory Data transfer instructions
© Craig Zilles (adapted from slides by Howard Huang)
MIPS Functions.
CS 286 Computer Architecture & Organization
MIPS function continued
Conditional Control Structure
MIPS Functions.
Presentation transcript:

Conditionals and Functions CS/COE 0447 Jarrett Billingsley

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

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

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

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

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

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

A conditional trick CS447

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

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

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

More complex conditions CS447

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

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

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

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

Functions CS447

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

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

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

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

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

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

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

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