cs356 unit 6
play

CS356 Unit 6 x86 Procedures Basic Stack Frames 6.2 Review of - PowerPoint PPT Presentation

6.1 CS356 Unit 6 x86 Procedures Basic Stack Frames 6.2 Review of Program Counter (IP register) PC/IP is used to fetch an instruction PC/IP contains the address of the next instruction The value in the PC/IP is placed on the address


  1. 6.1 CS356 Unit 6 x86 Procedures Basic Stack Frames

  2. 6.2 Review of Program Counter (IP register) • PC/IP is used to fetch an instruction – PC/IP contains the address of the next instruction – The value in the PC/IP is placed on the address bus and the memory is told to read with a signal on the control bus – PC/IP is incremented – The process is repeated for the next instruction Processor Memory PC = Addr = 0 0 PC/IP Addr 0 inst. 1 op. 1 inst. 2 Data = inst.1 code 2 inst. 3 ALU out in Data 3 inst. 4 1 ADD, SUB, AND, OR 4 inst. 5 in Control = Read 2 … $0-$31 Control F F

  3. 6.3 Procedures (Subroutines) CS:APP 3.7.1 • Procedures (aka subroutines or functions) are reusable sections of code that we can call from some location, execute that procedure, and then return to where we left off C code: int main() { We call the ... procedure to x = 8; calculate the average res = avg(x,4); and when it is printf("%d\n", res); finished it will return } to where we left off A procedure to int avg(int a, int b){ calculate the average return (a+b)/2; of 2 numbers }

  4. 6.4 Procedures • Procedure calls are similar to 'jump' instructions where we go to a new location in the code C code: int main() { ... x = 8; Call “avg” procedure will 1 res = avg(x,4); require us to jump to that printf("%d\n", res); code } int avg(int a, int b){ return (a+b)/2; }

  5. 6.5 Normal Jumps vs. Procedures • Difference between normal jumps and procedure calls is that with procedures we have to return to where we left off • We need to leave a link to the return location before we jump to the procedure … int main() { C code: ... x = 8; res = avg(x,4); printf("%d\n", res); Call “avg” procedure will 1 } require us to jump to After procedure that code completes, return int avg(int a, int b){ 2 to the statement return (a+b)/2; in the main code } where we left off

  6. 6.6 Implementing Procedures • To implement procedures in assembly we need to be able to: – Jump to the procedure code, leaving a "return link" (i.e. return address) to know where to return – Find the return address and go back to that location Assembly: C code: ... 113b callq avg # save a link Call res = avg (x,4); 1140 next inst. # to next instruc. ... Desired return avg : location 1125 addl %edi,%esi Definition 1127 movl %esi,%eax int avg (int a, int b) 1129 shrl $0x1f,%eax { return (a+b)/2; } 112c addl %esi,%eax 112e sarl %eax 1130 retl

  7. 6.7 Return Addresses • When calling a procedure, the address to jump to is ALWAYS the same • The location where a procedure returns will vary – Always the address of the instruction after the 'call' Assembly: 0x40004 is the return address for this call PC 0004 0000 0x40000 call AVG 0x40004 add ... 0x40028 is the return address for this call 0x40024 call AVG PC 0004 0024 0x40028 sub ... 0x40180 AVG: ... ret

  8. 6.8 Return Addresses • A further (very common) Assembly: ... complication is nested procedure call SUB1 calls 1 – One procedure calls another 0x4001A ... 4 • Example: Main routine calls SUB1 SUB1: movl %edi,%eax which calls SUB2 call SUB2 • Must store both return addresses 0x40208 ... but where? 3 ret – Registers? 2 No … very limited number – Memory? Yes … usually enough SUB2: ... memory for deep levels of nesting ret

  9. 6.9 Return Addresses and Stacks • Note: Return addresses will be Assembly: ... accessed in reverse order as call SUB1 they are stored 1 0x4001A ... 4 – 0x40208 is the second RA to be stored but should be the first one SUB1: movl %edi,%eax used to return call SUB2 • A stack structure is appropriate! 0x40208 ... • The system stack will be a place 3 ret 2 where we can store – Return addresses and other saved SUB2: ... register values ret – Local variables of a function – Arguments for procedures

  10. 6.10 System Stack • Stack is a data structure where data is accessed in reverse order as it is stored (a.k.a. LIFO = Last-in First-out) • Use a stack to store the return addresses and other data • System stack defined as growing towards smaller Memory / RAM addresses – Usually starts around ½ to ¾ of the way through the ... 0xfffffffc address space (i.e. for a 32-bit somewhere around 0x7ffff … or 0xbffff … ) • Top of stack is accessed and maintained using %rsp Initial "top" 0x7ffffff8 (stack pointer) register 0000 0000 0x7ffffff4 Stack – %rsp points at top occupied location of the stack 0000 0000 0x7ffffff0 0x7fffffec 0000 0000 Processor 0x7fffffe8 0000 0000 Stack Pointer 0000 0000 0x7fffffe4 rip 0000 0000 0004 001b Always points to 0000 0000 0x7fffffe0 top occupied rsp 0000 0000 7fff fff8 Stack grows element of the towards lower rax 0000 0000 0000 0000 stack addresses 0x0

  11. 6.11 Push Operation and Instruction • Push operation adds data to system stack • Format: pushq %reg – Decrements %rsp by 8 – Writes %reg to memory at address given by %rsp Memory / RAM • Example: pushq %rax ... 0xfffffffc – Equivalent: • subq $8, %rsp Bottom of Stack 0x7ffffff8 • movq %rax, (%rsp) 1111 2222 0x7ffffff4 3333 4444 0x7ffffff0 Processor 0x7fffffec 0000 0000 %rsp before rsp 0000 0000 7fff fff8 0x7fffffe8 0000 0000 - 8 Stack 0000 0000 0x7fffffe4 %rsp after 0000 0000 7fff fff0 0000 0000 0x7fffffe0 pushq %rax rax 1111 2222 3333 4444 rdx 0000 0000 0000 0000 0x0 Note: pushw also available

  12. 6.12 Pop Operation and Instruction • Pop operation removes data from system stack • Format: popq %reg – Reads memory at address given by %rsp and places value into %reg Memory / RAM – Increments %rsp by 8 ... 0xfffffffc • Example: popq %rdx – Equivalent: Bottom of Stack 0x7ffffff8 • movq (%rsp), %rdx 1111 2222 0x7ffffff4 • addq $8, %rsp 3333 4444 0x7ffffff0 Processor 0x7fffffec 0000 0000 %rsp before rsp 0000 0000 7fff fff0 0x7fffffe8 0000 0000 Stack + 8 0000 0000 0x7fffffe4 %rsp after 0000 0000 7fff fff8 popq %rdx 0000 0000 0x7fffffe0 rax 1111 2222 3333 4444 Note: pop does not erase the data on the stack, it simply moves the %rsp. The rdx 1111 2222 3333 4444 next push will overwrite the old value. 0x0 ( popw also available)

  13. 6.13 Jumping to a Procedure CS:APP 3.7.2 • Format: – call label – call *operand [e.g. call (%rax)] • Operations: – Pushes the address of next instruction (i.e. return address (RA) ) onto the stack • Implicitly performs subq $8,%rsp and movq %rip,(%rsp) – Updates the PC to go to the start of the desired procedure [i.e. PC = addr] • addr is the address you want to branch to ( usually specified as a label )

  14. 6.14 Returning From a Procedure • Format: – ret • Operations: – Pops the return address from the stack into %rip [i.e. PC = return-address] – Implicitly performs movq (%rsp), %rip and addq $8, %rsp

  15. 6.15 Procedure Call Sequence 1a • Initial conditions – About to execute the 'call' instruction – Current top of stack is at 0x7ffffff8 Memory / RAM 0000 0000 0x7ffffff8 0000 0000 0x7ffffff4 0000 0000 0x7ffffff0 0000 0000 0x7fffffec Processor ... 0x7fffffe8 0000 0000 call AVG 0000 0000 0004 001b rip Stack movl %eax,(%rbp) 0000 0000 0x7fffffe4 ... rsp 0000 0000 0x7fffffe0 0000 0000 7fff fff8 AVG: ... rax 0000 0000 0000 0000 movl %edi,%eax call AVG 0x4001b ... movl 0x40020 ret rdi 0000 0000 0000 0008 AVG: rsi 0000 0000 0000 0004 movl %edi,%eax 0x40180 ... ret 0x40188

  16. 6.16 Procedure Call Sequence 1b • call Operation (i.e. push return address) & jump – Decrement stack pointer ($rsp) and push RA (0x40020) onto stack (as 64-bit address) Memory / RAM – Update PC to start of procedure (0x40180) 0000 0000 0x7ffffff8 0000 0000 0x7ffffff4 2 0004 0020 0x7ffffff0 0000 0000 0000 0x7fffffec Processor ... 3 0x7fffffe8 0000 0000 call AVG 0000 0000 0004 0180 rip Stack movl %eax,(%rbp) 0000 0000 0x7fffffe4 1 ... rsp 0000 0000 0x7fffffe0 0000 0000 7fff fff8 AVG: - 8 ... movl %edi,%eax 0000 0000 7fff fff0 call AVG 0x4001b ... movl 0x40020 rax 0000 0000 0000 0000 ret AVG: rdi 0000 0000 0000 0008 movl %edi,%eax 0x40180 ... ret 0x40188 rsi 0000 0000 0000 0004

  17. 6.17 Procedure Call Sequence 1c • Execute the code for the procedure • Return value should be in %rax/%eax Memory / RAM 0000 0000 0x7ffffff8 0000 0000 0x7ffffff4 0004 0020 0x7ffffff0 0000 0000 0000 0x7fffffec Processor ... 0x7fffffe8 0000 0000 call AVG 0000 0000 0004 0180 rip Stack movl %eax,(%rbp) 0000 0000 0x7fffffe4 ... rsp 0000 0000 0x7fffffe0 0000 0000 7fff fff0 AVG: ... rax 0000 0000 0000 0006 movl %edi,%eax call AVG 0x4001b ... movl 0x40020 rdi 0000 0000 0000 0008 ret AVG: rsi 0000 0000 0000 0004 movl %edi,%eax 0x40180 ... ret 0x40188

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend