call stack
play

Call Stack Stack Bottom Memory region managed with stack discipline - PowerPoint PPT Presentation

Call Stack Stack Bottom Memory region managed with stack discipline Procedures and the Call Stack higher %rsp holds lowest stack address addresses (address of "top" element) Topics stack grows Procedures toward lower


  1. Call Stack Stack “Bottom” Memory region managed with stack discipline Procedures and the Call Stack higher %rsp holds lowest stack address addresses (address of "top" element) Topics stack grows • Procedures toward lower addresses • Call stack Stack Pointer: %rsp • Procedure/stack instructions • Calling conventions • Register-saving conventions Stack “Top” 9 Stack frames support procedure calls. Procedure Control Flow Instructions Contents Local variables Procedure call: callq label Caller Frame Function arguments (after first 6) 1. Push return address on stack Return information 2. Jump to label Temporary space Frame for Return address: Address of instruction after call . Example: current Management 400544: callq 400550 <mult2> procedure Space allocated when procedure is entered %rsp 400549: movq %rax,(%rbx) Stack Pointer “Setup” code Space deallocated before return Stack “Top” “Finish” code Procedure return: retq 1. Why not just give every procedure a permanent Pop return address from stack chunk of memory to hold its local variables, etc? 2. Jump to address 26 28

  2. Call Example (step 1) Return Example (step 1) • • • • 0x130 0x130 0000000000400540 <multstore>: 0000000000400540 <multstore>: • • 0x128 0x128 • • • • 0x120 0x120 400544: callq 400550 <mult2> 400544: callq 400550 <mult2> 0x118 0x400549 400549: mov %rax,(%rbx) 400549: mov %rax,(%rbx) • • %rsp 0x120 %rsp 0x118 • • %rip 0x400544 %rip 0x400557 0000000000400550 <mult2>: 0000000000400550 <mult2>: 400550: mov %rdi,%rax 400550: mov %rdi,%rax • • • • 400557: retq 400557: retq 29 31 Procedure Data Flow Stack Frame Remaining arguments passed First 6 arguments passed on stack (in memory) in registers High … Addresses Arg 1 %r d i Diane’s • • • Caller Silk Frame %r s i Dress Extra Arguments Arg n %r d x Costs to callee $8 9 %r c x • • • Return Address %r 8 Arg 8 Arg 6 %r 9 Saved Registers Arg 7 + Return value Callee Low Local Variables Addresses Frame %rax Only allocate stack space when needed Extra Arguments Stack pointer %rsp for next call 35

  3. Example: increment Procedure Call Example (initial state) Initial Stack Structure long increment(long* p, long val) { long call_incr() { long x = *p; long v1 = 240; long y = x + val; long v2 = increment(&v1, 61); • • • *p = y; return v1+v2; %rsp return x; } Return addr <main+8> } call_incr: subq $16, %rsp %rdi increment: movq $240, 8(%rsp) Register Use(s) movq (%rdi), %rax movl $61, %esi Argument p %rdi addq %rax, %rsi leaq 8(%rsp), %rdi movq %rsi, (%rdi) %rsi Argument val , y %rsi call increment ret addq 8(%rsp), %rax x , Return value %rax addq $16, %rsp %rax ret 37 38 Procedure Call Example (step 6) Procedure Call Example (step 4) Stack Structure Stack Structure long call_incr() { long increment(long* p, long val) { long call_incr() { long v1 = 240; long x = *p; long v2 = increment( &v1, 61 ) ; long v1 = 240; long y = x + val; return v1+v2; • • • long v2 = increment(&v1, 61); • • • *p = y; } return v1+v2 ; return x; %rsp %rsp } Return addr <main+8> Return addr <main+8> } call_incr: v1 in call_incr à v1 in call_incr à subq $16, %rsp 301 301 movq $240, 8(%rsp) Unused Unused movl $61, %esi Return addr <call_incr+?> call_incr: leaq 8(%rsp), %rdi call increment subq $16, %rsp %rdi %rdi addq 8(%rsp), %rax movq $240, 8(%rsp) addq $16, %rsp &v1 &v1 movl $61, %esi ret leaq 8(%rsp), %rdi %rsi %rsi call increment increment: Update %rax : v1+v2 addq 8(%rsp), %rax 301 301 movq (%rdi), %rax # x = *p addq $16, %rsp addq %rax, %rsi # y = x+61 %rax %rax ret movq %rsi, (%rdi) # *p = y 240 541 ret 42 44

  4. A Puzzle Register Saving Conventions C function body: Write the C function calls who : yoo *p = d; header, types, and Caller Callee return x - c; order of parameters. assembly: Will register contents still be there after a procedure call? movsbl %dl,%edx yoo: who: movl %edx,(%rsi) • • • • • • movq $12345, %rbx addq %rdi, %rbx movswl %di,%edi call who • • • ? subl %edi,%ecx addq %rbx, %rax ret • • • movl %ecx,%eax ret Conventions: Caller Save Callee Save movsbl = move sign-extending a byte to a long (4-byte) movswl = move sign-extending a word (2-byte) to a long (4-byte) 49 Callee-Saved Example x86-64 64-bit Register Conventions Begin/End Stack Structure long call_incr2(long x) { Return value – Caller saved Argument #5 – Caller saved %rax %r8 long v1 = 240; . . . long v2 = increment(&v1, 61); Callee saved Argument #6 – Caller saved %rbx %r9 return x+v2; } Rtn address %rsp Argument #4 – Caller saved Caller saved %rcx %r10 call_incr2: Caller Saved %rdx Argument #3 – Caller saved %r11 pushq %rbx Stack Structure in call_incr2 subq $16, %rsp %rsi Argument #2 – Caller saved %r12 Callee saved movq %rdi, %rbx movq $240, 8(%rsp) . . . Argument #1 – Caller saved Callee saved movl $61, %esi %rdi %r13 leaq 8(%rsp), %rdi Rtn address call increment %rsp Stack pointer %r14 Callee saved addq %rbx , %rax Saved %rbx addq $16, %rsp Callee saved Callee saved %rsp+8 %rbp %r15 240 popq %rbx Unused %rsp ret 51 52

  5. x86-64 stack storage example Recursive Function (1) pcount_r: movl $0, %eax long int call_proc() call_proc: /* Recursive popcount */ testq %rdi, %rdi { subq $32,%rsp long pcount_r(unsigned long x) { je .L6 long x1 = 1; movq $1,16(%rsp) # x1 if (x == 0) { pushq %rbx int x2 = 2; movl $2,24(%rsp) # x2 return 0; movq %rdi, %rbx short x3 = 3; movw $3,28(%rsp) # x3 } else { andl $1, %ebx char x4 = 4; movb $4,31(%rsp) # x4 return (x & 1) shrq %rdi proc(x1, &x1, x2, &x2, • • • + pcount_r(x >> 1); call pcount_r x3, &x3, x4, &x4); } addq %rbx, %rax return (x1+x2)*(x3-x4); } popq %rbx } .L6: rep; ret Return address to caller of call_proc ← %rsp 54 61 Procedure Summary call, ret, push, pop Stack discipline fits procedure call / return.* … If P calls Q: Q (and calls by Q) returns before P Caller Frame Conventions support arbitrary function calls. Extra Arguments Register-save conventions. to callee Stack frame saves extra args or local variables. Return Address Result returned in %rax %rax %r8 Return value – Caller saved Argument #5 – Caller saved Saved Registers %rbx Callee saved %r9 Argument #6 – Caller saved + %rcx %r10 Argument #4 – Caller saved Caller saved Callee Local Variables Frame %rdx %r11 Argument #3 – Caller saved Caller Saved %rsi %r12 Argument #2 – Caller saved Callee saved %rdi Argument #1 – Caller saved %r13 Callee saved Extra Arguments Stack pointer %rsp %r14 Stack pointer Callee saved for next call %rsp %rbp %r15 Callee saved Callee saved 128-byte red zone 66 *Take 251 to learn about languages where it doesn't.

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