Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions and the Stack

Similar presentations


Presentation on theme: "Functions and the Stack"— Presentation transcript:

1 Functions and the Stack
CS/COE 0447 (term 2181) Jarrett Billingsley

2 Class announcements Recitations are now optional, and labs will be posted Fridays. They'll be due the following Thursday at 11:59 PM. Lab 2 will be posted tomorrow. It'll give you practice with arrays, loops, conditionals, and functions. It can also be the basis for your project if you haven't started! 9/12/2017 CS/COE term 2181

3 ABIs and calling conventions
9/12/2017 CS/COE term 2181

4 What's an ABI? An ABI, or Application Binary Interface, is:
a set of conventions… for a given architecture… and operating system… that machine-language programs must follow… in order to work properly with the OS and with one another. whew. The ABI encompasses many things, but one of the most important parts is the calling convention. 9/12/2017 CS/COE term 2181

5 …what's a calling convention? (more 0449 stuff)
It's how machine-language functions call one another. It specifies: How arguments are passed How return values are returned How execution flows into/out of the function What contracts exist between the caller and the callee void fork() { knife(); } void knife() { ... } caller callee 9/12/2017 CS/COE term 2181

6 The program counter register
A program's instructions are in memory, so they have addresses. The Program Counter register (PC) holds the address of the next instruction to execute. PC 0x8000 top: 0x8000 lw t0, (s0) 0x8004 addi t0, t0, 1 0x8008 sw t0, (s0) 0x800C addi s0, s0, 4 0x8004 0x8008 0x800C 0x8010 blt s0, s1, top 0x8010 0x8000 ? 0x8014 9/12/2017 CS/COE term 2181

7 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 9/12/2017 CS/COE term 2181

8 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 ... 9/12/2017 CS/COE term 2181

9 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 func: 0x8C30 li v0, 4 0x8C34 syscall 0x8C38 jr ra 9/12/2017 CS/COE term 2181

10 Don't step on each others' toes
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 spoon: After jr ra: 0x8024 0x8024 UH OH. After jr ra: 0x8024 0x8024 After jr ra: 0x8024 0x8024 9/12/2017 CS/COE term 2181 After jr ra: 0x8024 0x8024

11 The Stack 9/12/2017 CS/COE term 2181

12 One busy desk There's a tiny desk that three people have to share. Person 1 is working at the desk. It's covered in their stuff. Person 2 interrupts them and needs to do some important work. What does person 2 do? Throw all that stuff in the trash? They put it somewhere else. P1 P1 Trash P1 P1 9/12/2017 CS/COE term 2181

13 One busy desk P2 P2 P1 Now person 2 is interrupted by person 3.
When person 3 is done, person 2 will come back. Where do we put person 2's stuff? On top of the stack of stuff. The desk is the registers. The people are functions. The stack of stuff is… the stack. P2 P2 P2 P2 P1 P1 9/12/2017 CS/COE term 2181

14 What's the stack? Stack Memory
It's an area of memory provided to your program by the OS. When your program starts, it's already there. The stack is used to keep track of information related to function calls. It holds stuff like: Copies of registers that we want to change, like ra! The return address to the caller. Local variables that can't fit in registers. How do we access the stack? Through the stack pointer (sp) register. This register is initialized for you by the OS too. Memory Stack 9/12/2017 CS/COE term 2181

15 The stack pointer (animated)
Let's say sp starts at the address 0xF000. We want to push something on the stack. The first thing we'll do is move sp to the next available slot. Clearly, that's the previous address. We subtract 4 from sp. Then, we can store something in the memory that sp points to. 0xEFFC 0x 0xC0DEBEEF sp 0xF000 0x 0xF004 0xF008 ... 9/12/2017 CS/COE term 2181

16 Doing that in MIPS (animated)
Say ra = 0xC0DEBEEF. First we have to move the stack pointer down (up?): subi sp, sp, 4 Then, we store ra at the address that sp holds. sw ra, (sp) Job done! Now the value in ra is saved on the stack, and we can get it back later. 0xEFFC 0x 0xC0DEBEEF sp 0xF000 0x 0xF004 0xF008 ... 9/12/2017 CS/COE term 2181

17 Going the other direction (animated)
Now we wanna pop the value off the stack and put it back in ra. We do the same things, but in reverse. Which means first: lw ra, (sp) Then, we move the stack pointer… up? down? whatever addi sp, sp, 4 Now we got back the old value of ra! And sp is where it was when we started! ra 0xC0DEBEEF 0xABAD1DEA sp 0xEFFC 0xC0DEBEEF 0xF000 0x 0xF004 0xF008 ... 9/12/2017 CS/COE term 2181

18 Toes = protected ra PC 0x8000 jal fork 0x8020 subi sp, sp, 4 0x8024
sw ra, (sp) 0x8028 jal spoon 0x802C lw ra, (sp) 0x8030 addi sp, sp, 4 0x8034 jr ra 0x8040 0x8000 0x0000 fork: After jal fork: 0x8020 0x8004 Then we push ra on the stack! After jal spoon: 0x8040 0x802C After spoon jr ra: 0x802C 0x802C Then we pop ra off the stack! Before fork jr ra: 0x8034 0x8004 spoon: After fork jr ra: 0x8004 0x8004 9/12/2017 CS/COE term 2181

19 Shortening the pushes and pops
The push and pop operations always look and work the same. Since you'll be using them in most functions, let's shorten them. In MIPS asm, we can make a macro. This is not a function. It's a way of doing a "smart copy and paste." .macro push %reg subi sp, sp, 4 sw %reg, (sp) .end_macro .macro pop %reg lw %reg, (sp) addi sp, sp, 4 .end_macro 9/12/2017 CS/COE term 2181

20 Writing a simple function
Writing a function is now really easy: 1. Give it a name (label). spoon: push ra 2. Save ra to the stack. your code goes here 3. Do whatever. 4. Load ra from the stack. pop ra jr ra 5. Return! 9/12/2017 CS/COE term 2181

21 In the next installment
Arguments! Return values! Local variables! Why we call the "saved" registers that! and SO MUCH MORE (well not really) 9/12/2017 CS/COE term 2181


Download ppt "Functions and the Stack"

Similar presentations


Ads by Google