increasing
play

Increasing Addresses Grows Stack Down Stack Pointer: %rsp Stack - PDF document

Procedure Call Registers %rax %r8 %eax %r8d Return Arg 5 %rbx %r9 %ebx %r9d Arg 6 %rcx %r10 %ecx %r10d Arg 4 %rdx %r11 %edx %r11d Arg 3 %rsi %r12 %esi %r12d Arg 2 %rdi %r13 %edi %r13d Arg 1 %rsp %r14 %esp %r14d


  1. Procedure Call Registers %rax %r8 %eax %r8d Return Arg 5 %rbx %r9 %ebx %r9d Arg 6 %rcx %r10 %ecx %r10d Arg 4 %rdx %r11 %edx %r11d Arg 3 %rsi %r12 %esi %r12d Arg 2 %rdi %r13 %edi %r13d Arg 1 %rsp %r14 %esp %r14d Stack ptr %rbp %r15 %ebp %r15d Sean Barker 1 Call Stack Stack “Bo;om” Increasing Addresses Grows Stack Down Stack Pointer: %rsp Stack “Top” Sean Barker 2

  2. Stack Operations Stack “Bo8om” Stack “Bo;om” Increasing Increasing Addresses Addresses Grows Stack Grows Stack Down Stack Pointer: %rsp Down +8 Stack Pointer: %rsp -8 Stack “Top” Stack “Top” pushq Src popq Dest Sean Barker 3 Procedure Call Example (1) • Stack Grows • 0x130 0000000000400540 <foo>: Down • 0x128 • • 0x120 400544: callq 400550 <bar> 400549: mov %rax,(%rbx) • %rsp 0x120 • %rip 0x400544 0000000000400550 <bar>: 400550: mov %rdi,%rax • • 400557: retq Sean Barker 4

  3. Procedure Call Example (2) • Stack Grows • 0x130 0000000000400540 <foo>: Down • 0x128 • • 0x120 400544: callq 400550 <bar> 0x118 0x400549 400549: mov %rax,(%rbx) • %rsp 0x118 • %rip 0x400550 0000000000400550 <bar>: 400550: mov %rdi,%rax • • 400557: retq Sean Barker 5 Procedure Call Example (3) • Stack Grows • 0x130 0000000000400540 <foo>: Down • 0x128 • • 0x120 400544: callq 400550 <bar> 0x118 0x400549 400549: mov %rax,(%rbx) • %rsp 0x118 • %rip 0x400557 0000000000400550 <bar>: 400550: mov %rdi,%rax • • 400557: retq Sean Barker 6

  4. Procedure Call Example (4) • Stack Grows • 0x130 0000000000400540 <foo>: Down • 0x128 • • 0x120 400544: callq 400550 <bar> 400549: mov %rax,(%rbx) • %rsp 0x120 • %rip 0x400549 0000000000400550 <bar>: 400550: mov %rdi,%rax • • 400557: retq Sean Barker 7 Passing Data Registers Stack ¢ First 6 arguments • • • %rdi Arg n %rsi Stack Grows %rdx Down • • • %rcx Arg 8 %r8 Arg 7 %r9 Stack “Top” ¢ Return value %rax Sean Barker 8

  5. Stack Frames Previous Frame Frame Pointer: %rbp (Op:onal) x Frame for proc Stack Pointer: %rsp Stack “Top” Sean Barker 9 Call Chain Example Example Call Chain yoo(…) { yoo • • who(…) who(); who { • • • • • amI(); amI(…) amI amI } • • • { amI(); • amI • • • • } amI(); • amI • } Procedure amI() is recursive Sean Barker 10

  6. Stack Frame Allocation (1) Stack yoo yoo(…) %rbp { yoo yoo • who %rsp • who(); amI amI • • } amI amI Sean Barker 11 Stack Frame Allocation (2) Stack yoo(…) yoo who(…) { yoo yoo { • who • • • • %rbp amI(); who(); who • • • • amI amI %rsp amI(); • • • • } amI } amI Sean Barker 12

  7. Stack Frame Allocation (3) Stack yoo(…) yoo who(…) { yoo yoo { • amI(…) who • • • • { amI(); who(); • who • • • • amI amI • amI(); • amI(); %rbp • • • } • amI amI } • %rsp } amI Recurse! Sean Barker 13 Stack Frame Allocation (4) Stack yoo(…) yoo who(…) { yoo yoo { • amI(…) who • • • • { amI(); who(); amI(…) • who • • • • amI amI { • amI(); • • amI(); • • • } • • amI amI } amI(); • • } %rbp • amI amI } %rsp Recurse! Sean Barker 14

  8. Stack Frame Allocation (5) Stack yoo(…) yoo who(…) { yoo yoo { • amI(…) who • • • • { amI(); who(); amI(…) • who • • • • amI amI { • amI(); amI(…) • • amI(); • • • { } • • amI amI } • amI(); • • • } amI(); • amI amI • } • %rbp } amI Base %rsp Case Sean Barker 15 Stack Frame Allocation (6) Stack yoo(…) yoo who(…) { yoo yoo { • amI(…) who • • • • { amI(); who(); amI(…) • who • • • • amI amI { • amI(); • • amI(); • • • } • • amI amI } amI(); • • } %rbp • amI amI } %rsp Sean Barker 16

  9. Stack Frame Allocation (7) Stack yoo(…) yoo who(…) { yoo yoo { • amI(…) who • • • • { amI(); who(); • who • • • • amI amI • amI(); • amI(); %rbp • • • } • amI amI } • %rsp } amI Sean Barker 17 Stack Frame Allocation (8) Stack yoo(…) yoo who(…) { yoo yoo { • who • • • • %rbp amI(); who(); who • • • • amI amI %rsp amI(); • • • • } amI } amI Sean Barker 18

  10. Stack Frame Allocation (9) Stack yoo(…) yoo who(…) { yoo yoo { • amI(…) who • • • • { amI(); who(); • who • • • • amI amI • amI(); • amI(); %rbp • • • } • amI amI } • %rsp } amI Base Case Sean Barker 19 Stack Frame Allocation (10) Stack yoo(…) yoo who(…) { yoo yoo { • who • • • • %rbp amI(); who(); who • • • • amI amI %rsp amI(); • • • • } amI } amI Sean Barker 20

  11. Stack Frame Allocation (11) Stack yoo %rbp yoo(…) yoo { yoo who %rsp • • who(); amI amI • • amI } amI Sean Barker 21 Stack Frame Components Caller Frame Arguments 7+ Return Addr Frame pointer %rbp Old %rbp (Op<onal) Saved Registers + Local Variables Argument Stack pointer Build (Op<onal) %rsp Sean Barker 22

  12. Stack Example: incr long incr(long *p, long val) { long x = *p; long y = x + val; *p = y; return x; } incr: Register Use(s) movq (%rdi), %rax Argument p %rdi addq %rax, %rsi Argument val , y movq %rsi, (%rdi) %rsi ret x , Return value %rax Sean Barker 23 Using the Stack (1) Ini$al Stack Structure long call_incr() { long v1 = 15213; . . . long v2 = incr(&v1, 3000); return v1+v2; } Rtn address %rsp call_incr: subq $16, %rsp Resul$ng Stack Structure movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi . . . call incr addq 8(%rsp), %rax Rtn address addq $16, %rsp %rsp+8 15213 ret Unused %rsp Sean Barker 24

  13. Using the Stack (2) Stack Structure long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); . . . return v1+v2; } Rtn address %rsp+8 15213 Unused %rsp call_incr: subq $16, %rsp Register Use(s) movq $15213, 8(%rsp) movl $3000, %esi %rdi &v1 leaq 8(%rsp), %rdi %rsi 3000 call incr addq 8(%rsp), %rax addq $16, %rsp ret Sean Barker 25 Using the Stack (3) Stack Structure long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); . . . return v1+v2; } Rtn address %rsp+8 18213 Unused %rsp call_incr: subq $16, %rsp Register Use(s) movq $15213, 8(%rsp) movl $3000, %esi %rdi &v1 leaq 8(%rsp), %rdi %rsi 3000 call incr addq 8(%rsp), %rax addq $16, %rsp incr: ret movq (%rdi), %rax addq %rax, %rsi movq %rsi, (%rdi) ret Sean Barker 26

  14. Using the Stack (4) Stack Structure long call_incr() { . . . long v1 = 15213; long v2 = incr(&v1, 3000); return v1+v2; Rtn address } %rsp+8 18213 Unused %rsp call_incr: Register Use(s) subq $16, %rsp Return value %rax movq $15213, 8(%rsp) movl $3000, %esi Updated Stack Structure leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax . . . addq $16, %rsp ret Rtn address %rsp Sean Barker 27 Using the Stack (5) Updated Stack Structure long call_incr() { long v1 = 15213; long v2 = incr(&v1, 3000); . . . return v1+v2; } Rtn address %rsp call_incr: Register Use(s) subq $16, %rsp Return value %rax movq $15213, 8(%rsp) movl $3000, %esi Final Stack Structure leaq 8(%rsp), %rdi call incr addq 8(%rsp), %rax . . . addq $16, %rsp %rsp ret Sean Barker 28

  15. Stack Frame Components Caller Frame Arguments 7+ Return Addr Frame pointer %rbp Old %rbp (Op<onal) Saved Registers + Local Variables Argument Stack pointer Build (Op<onal) %rsp Sean Barker 29 Register Conventions Return value %rax (caller-saved) %rbx %rdi %r12 %rsi Callee-saved %r13 %rdx Arguments Temporaries (caller-saved) %r14 %rcx %r15 %r8 %rbp %r9 Special %rsp %r10 Caller-saved temporaries %r11 Sean Barker 30

  16. Callee-Saved Example (1) Ini$al Stack Structure long call_incr2(long x) { long v1 = 15213; . . . long v2 = incr(&v1, 3000); return x+v2; } Rtn address %rsp call_incr2: pushq %rbx Resul$ng Stack Structure subq $16, %rsp movq %rdi, %rbx movq $15213, 8(%rsp) . . . movl $3000, %esi leaq 8(%rsp), %rdi Rtn address call incr addq %rbx, %rax Saved %rbx addq $16, %rsp %rsp+8 15213 popq %rbx Unused %rsp ret Sean Barker 31 Callee-Saved Example (2) Resul3ng Stack Structure long call_incr2(long x) { . . . long v1 = 15213; long v2 = incr(&v1, 3000); return x+v2; Rtn address } Saved %rbx %rsp+8 15213 call_incr2: Unused %rsp pushq %rbx subq $16, %rsp movq %rdi, %rbx Pre-return Stack Structure movq $15213, 8(%rsp) movl $3000, %esi leaq 8(%rsp), %rdi . . . call incr addq %rbx, %rax addq $16, %rsp Rtn address %rsp popq %rbx ret Sean Barker 32

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