CS/COE 0447 (term 2181) Jarrett Billingsley

Slides:



Advertisements
Similar presentations
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Advertisements

The University of Adelaide, School of Computer Science
Slides revised 3/25/2014 by Patrick Kelley. 2 Procedures Unlike other branching structures (loops, etc.) a Procedure has to return to where it was called.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
10/6: Lecture Topics Procedure call Calling conventions The stack
MIPS Function Continued
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Ch. 8 Functions.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Procedure call frame: Hold values passed to a procedure as arguments
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
MIPS Coding. Exercise – the bubble sort 5/8/2015week04-3.ppt2.
Register Conventions (1/4) °CalleR: the calling function °CalleE: the function being called °When callee returns from executing, the caller needs to know.
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
Lecture 6: Procedures (cont.). Procedures Review Called with a jal instruction, returns with a jr $ra Accepts up to 4 arguments in $a0, $a1, $a2 and $a3.
Intro to Computer Architecture
Lecture 7: MIPS Instruction Set Today’s topic –Procedure call/return –Large constants Reminders –Homework #2 posted, due 9/17/
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.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
MIPS Procedures II, Logical & Shift Ops. Review Functions called with jal, return with jr $ra. The stack is your friend: Use it to save anything you need.
Function Calls in Assembly MIPS R3000 Language (extensive use of stack) Updated 7/11/2013.
Programs – Calling Conventions
Computer Architecture & Operations I
Function Calls in MIPS To call a function: jal func
Computer Science 210 Computer Organization
Computer structure: Procedure Calls
Lecture 5: Procedure Calls
CS/COE 0447 (term 2181) Jarrett Billingsley
MIPS Assembly Language Programming
Pseudo-ops and Bitwise Operations
Functions and the Stack
Function Calls in MIPS To call a function: jal func
© Craig Zilles (adapted from slides by Howard Huang)
Procedures 101: There and Back Again
CSCI206 - Computer Organization & Programming
MIPS Procedures.
Procedures (Functions)
Procedures (Functions)
CSCI206 - Computer Organization & Programming
CSCI206 - Computer Organization & Programming
MIPS Procedures.
MIPS Instructions.
MIPS Procedure Calls CSE 378 – Section 3.
The University of Adelaide, School of Computer Science
MIPS Functions.
More Functions and the Stack
MIPS Functions.
Lecture 5: Procedure Calls
MIPS Procedures.
Review.
10/4: Lecture Topics Overflow and underflow Logical operations
CS/COE 0447 Jarrett Billingsley
MIPS function continued
MIPS Functions.
Lecture 6: Assembly Programs
Procedures and Calling Conventions
Conditionals and Functions
© Craig Zilles (adapted from slides by Howard Huang)
ICS51 Introductory Computer Organization
MIPS Functions.
MIPS function continued
MIPS Functions.
Presentation transcript:

CS/COE 0447 (term 2181) Jarrett Billingsley More Functions CS/COE 0447 (term 2181) Jarrett Billingsley

Class announcements If you're confused about Lab 2… Everyone is! Hopefully today will help with that. When doing the nested loops, work from the outside in. When doing if-elses, do the first if first. You can use Discord on your computers, too… Since only 20 of you have responded so far: Who has exams in other classes on 9/28? 10/3? 10/5? 9/19/2017 CS/COE 0447 term 2181

Which instruction do I use to put a value into… We've seen a lot of data transfer instructions. All of these only change the value of the register on the left. Inst. li la move lw lh[u] lb[u] Use it for… Putting a constant into a register Putting an address into a register Copying from one reg to another Getting a 32-bit value from memory Getting a 16-bit value from memory Getting an 8-bit value from memory The value will stay in the reg you're copying from!! 9/19/2017 CS/COE 0447 term 2181

Saved and Unsaved registers 9/19/2017 CS/COE 0447 term 2181

Some toes are steppable, some aren't Maybe you wrote a loop, and bad things happened. li t0, 0 loop_top: move a0, t0 li a1, 5 li a2, COLOR_RED jal Display_SetLED addi t0, t0, 1 blt t0, 4, loop_top loop_end: What is this supposed to do? Let's see what really happens. Now let's change t0 to s0. 9/19/2017 CS/COE 0447 term 2181

Another piece of the calling convention puzzle When you call a function, it's allowed to change some registers. Other registers must be left exactly as they were. Functions are allowed to use these registers, as long as they put them back the way they were before they return. You are allowed to use these registers freely, but when you call another function, you don't know what they'll hold afterwards! Saved Registers s0-s7 sp ra Unsaved Registers v0-v1 a0-a3 t0-t9 9/19/2017 CS/COE 0447 term 2181

Why it broke li t0, 0 loop_top: move a0, t0 li a1, 5 li a2, COLOR_RED If we look at this code again… t0 is our loop counter and everything's fiiiine. li t0, 0 loop_top: move a0, t0 li a1, 5 li a2, COLOR_RED jal Display_SetLED addi t0, t0, 1 blt t0, 4, loop_top loop_end: uh oh. We have no idea what's in t0 now! 9/19/2017 CS/COE 0447 term 2181

Whenever you call a function… After calling a function, you have no idea what's in these registers. ... jal Display_SetLED Unsaved Registers v0-v1 a0-a3 t0-t9 9/19/2017 CS/COE 0447 term 2181

Using s registers properly You can't just use an s register. You have to follow the protocol. If you want to use an s register in your function, save it (push it) at the beginning, and restore it (pop it) at the end. my_func: push ra push s0 pop s0 pop ra jr ra Moving the papers off the desk. The pops happen in reverse order! Putting the papers back. 9/19/2017 CS/COE 0447 term 2181

Passing arguments and returning values 9/19/2017 CS/COE 0447 term 2181

The a and v registers Registers a0-a3 are the argument registers. Registers v0-v1 are the return value registers. This is just a convention. There's nothing special about them! The caller… Fills the argument registers Uses jal to call the function Looks at the return value registers The callee… Looks at the argument registers Does its work Puts something in the return value register(s) Returns 9/19/2017 CS/COE 0447 term 2181

int add_nums(int x, int y) { return x + y; } Input, output We want to write a function like this: int add_nums(int x, int y) { return x + y; } Inside of our add_nums asm function… What register will represent x? What register will represent y? What register will hold the sum that we return? add_nums: add __, __, __ v0 a0 a1 jr ra 9/19/2017 CS/COE 0447 term 2181

Calling that function li a0, 3 v0 = add_nums(3, 8) li a1, 8 print(v0) Now let's call it from main. We want to do: v0 = add_nums(3, 8) print(v0) How do we set 3 and 8 as the arguments? How do we call add_nums? How do we put the result in v0? How do we print an integer? Why do syscalls put the number of the syscall in v0? What do you get when you cross an elephant and a rhino? li a0, 3 li a1, 8 jal add_nums move a0, v0 li v0, 1 syscall 9/19/2017 CS/COE 0447 term 2181

Which registers do I use? 90% of your code will use s and t registers. Which register would you use to… Hold a loop counter variable? Hold a * 4 in the expression (a * 4) + b ? Hold an address of a variable you'll load, and then not use the address again? Hold the argument x in the function call check_block(x, y) ? Hold x + 3 in return x + 3; ? 9/19/2017 CS/COE 0447 term 2181

Function recap 9/19/2017 CS/COE 0447 term 2181

Let's design a function which… Draws a diagonal line of pixels on the display. If we call it like draw_line(10, 10, 5), it will start at x = 10, y = 10, and draw 5 squares in a down-right direction. What registers are the arguments given in? Do we have to save ra? What register will we use for the loop? Do we have to save it? How do we write the loop? What do we call in the loop? 9/19/2017 CS/COE 0447 term 2181