Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS/COE 0447 (term 2181) Jarrett Billingsley

Similar presentations


Presentation on theme: "CS/COE 0447 (term 2181) Jarrett Billingsley"— Presentation transcript:

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

2 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 term 2181

3 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 term 2181

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

5 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 term 2181

6 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 term 2181

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

8 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 term 2181

9 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 term 2181

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

11 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 term 2181

12 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 term 2181

13 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 term 2181

14 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 term 2181

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

16 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 term 2181


Download ppt "CS/COE 0447 (term 2181) Jarrett Billingsley"

Similar presentations


Ads by Google