1 / 41

Control

Control. Outline. PC-relative Address Suggested reading Chap 3.6.3. Example. jle .L2 .L5: movl % edx , % eax sarl $1, % eax subl % eax , % edx Leal (% edx , % edx , 2), % edx testl % edx , % edx jg .L5 9 .L2: 10 movl % edx , % eax.

liona
Download Presentation

Control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Control

  2. Outline • PC-relative Address • Suggested reading • Chap 3.6.3

  3. Example • jle .L2 • .L5: • movl %edx, %eax • sarl $1, %eax • subl %eax, %edx • Leal (%edx, %edx, 2), %edx • testl %edx, %edx • jg .L5 9 .L2: 10 movl %edx, %eax

  4. PC-relative in the Relocatable Object • 8: 7e 0d jle 17<silly+0x17> • a: 89 d0 mov %edx, %eax dest1: 3 c: c1 f8 sar %eax • e: 29 c2 sub %eax, %edx • 10: 8d 14 52 lea (%edx, %edx, 2), %edx 6 13: 85 d2 test %edx, %edx • 15: 7f f3 jg a<silly+0x10> • 17: 89 d0 movl %edx, %eax dest2: d+a = 17 17+f3(-d) =a

  5. PC-relative in the Executable Object • 804839: 7e 0d jle 17<silly+0x17> • 804839e: 89 d0 mov %edx, %eax dest1: 3 80483a0: c1 f8 sar %eax • 80483a2: 29 c2 sub %eax, %edx • 80483a4: 8d 14 52 lea (%edx, %edx, 2), %edx 6 80483a7: 85 d2 test %edx, %edx • 80483a9: 7f f3 jg a<silly+0x10> • 80483ab: 89 d0 movl %edx, %eax dest2: d+804849e = 80483ab 80483ab+f3(-d) = 804839e

  6. Procedure Call

  7. Outline • Procedure call • Stack frame • Calling conventions • Recursive • Suggested reading • Chap 3.7

  8. Execution within Procedure/Function • Data Movement (e.g., movl $-17, (%esp)) • Arithmetic Operations (e.g., incl 8(%eax)) • Logical Operations (e.g., xorl 8(%esp), %eax) • Condition Codes (e.g., cmpl %eax, %edx) • Jump Instructions (e.g., jg .L5) How to execute cross procedures?

  9. Procedure/Function call • Another type of unconditional JUMP • SAME: • Control from one part to another • DIFF: • Return • Passing data (arguments, return values) • Local variable • Registers

  10. Basic Concept • Terminology • Caller • Callee f() call-1 g() h() call-2 • call-1 • Caller: f • Callee: g • call-2 • Caller: g • Callee: h

  11. Basic Concept • Terminology • Caller: g • Callee: f • Control Flow e.g. int g() { return 1 ; } int f() { return g() ; } int main() { g(); return f(); } g() call main() f() g() f() g()

  12. Procedure/Function Implementation • Invoke callee • Return to caller • Passing data • Registers • Local variable

  13. Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller • Passing data • Registers • Local variable

  14. Invoke Callee • Instruction • call label (direct) • call *operand(indirect) • Behavior description (by hardware) • Save return address in the stack • Jump to the entryof callee call = push + jmp • push retaddr • jmpcallee

  15. Execution of call and ret //beginning of function sum • 08048394 <sum>: • 8048394: 55 push %ebp . . . • 80483a4: ret . . . //call to sum from main • 80483dc: e8 b3 ffffff call 8048394<sum> • 80483e1: 83 c4 14 add $0x14, %esp Executing call After call

  16. Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller: ret (new instructions) • Passing data • Registers • Local variable

  17. Return to Caller • Instruction • ret • Behavior description (by hardware) • Pop return address from stack • Jump to return addressin caller ret = pop + jmp • pop retaddr • jmpretaddr

  18. Execution of call and ret //beginning of function sum • 08048394 <sum>: • 8048394: 55 push %ebp . . . • 80483a4: ret . . . //call to sum from main • 80483dc: e8 b3 ffffff call 8048394<sum> • 80483e1: 83 c4 14 add $0x14, %esp executing ret After ret

  19. Procedure/Function Implementation • Invoke callee: call (new instructions) • Return to caller: ret (new instructions) • Passing data: stack, register • Registers • Local variable

  20. %ebp %esp Stack Frame Structure • The portion of stack allocated for a procedure • A stack frame is delimited by • The frame pointer %ebp • The stack pointer %esp • The stack pointer can move when the procedure is executing (dynamic) • The frame pointer is static

  21. %ebp %esp Stack Frame Structure • call: save return address in the stack • ret: pop return address from stack • The end of caller’s stack frame

  22. Frame Chain • Pointers (%ebp/%esp) only delimit topmostframe %ebp %esp

  23. Frame Chain • Pointers (%ebp/%esp) only delimit topmostframe • Frames are chained • 1. call callee %ebp %esp

  24. Frame Chain • Pointers (%ebp/%esp) only delimit topmostframe • Frames are chained • 1. call callee • callee: • 2. push %ebp • 3. mov %esp, %ebp %ebp %esp

  25. Frame Chain • Pointers (%ebp/%esp) only delimit topmostframe • Frames are chained • 1. call callee • callee: • 2. push %ebp • 3. mov %esp, %ebp %ebp %esp

  26. Frame Chain • Pointers (%ebp/%esp) only delimit topmostframe • Frames are chained • 1. call callee • callee: • 2. push %ebp • 3. mov %esp, %ebp %ebp %esp

  27. Frame Chain • Pointers (%ebp/%esp) only delimit topmostframe • Frames are chained • 1. call callee • callee: • 2. push %ebp • 3. mov %esp, %ebp • . . . • n-2. mov %ebp, %esp • n-1. pop %ebp • n. ret %ebp %esp

  28. Frame Chain • Pointers (%ebp/%esp) only delimit topmostframe • Frames are chained • 1. call callee • callee: • 2. push %ebp • 3. mov %esp, %ebp • . . . • n-2. mov %ebp, %esp • n-1. pop %ebp • n. ret %ebp %esp

  29. Frame Chain • Pointers (%ebp/%esp) only delimit topmostframe • Frames are chained • 1. call callee • callee: • 2. push %ebp • 3. mov %esp, %ebp • . . . • n-2. mov %ebp, %esp • n-1. pop %ebp • n. ret %ebp %esp

  30. Frame Chain • Pointers (%ebp/%esp) only delimit topmostframe • Frames are chained • 1. call callee • callee: • 2. push %ebp • 3. mov %esp, %ebp • . . . • n-2. mov %ebp, %esp • n-1. pop %ebp • n. ret %ebp %esp

  31. Frame Chain • Pointers (%ebp/%esp) only delimit topmostframe • Frames are chained %ebp %esp

  32. Restore Caller %ebp • Instruction • leave • Behavior description (by hardware) • Adjust %esp to callee %ebp • Pop caller %ebp from stack leave = mov + pop • mov %ebp, %esp • pop %ebp

  33. Execution of call and ret //beginning of function sum • 08048394 <sum>: • 8048394: 55 push %ebp . . . • 80483a4: ret . . . //call to sum from main • 80483dc: e8 b3 ffffff call 8048394<sum> • 80483e1: 83 c4 14 add $0x14, %esp Executing call After call After ret

  34. Memory Layout User stack

  35. %ebp %esp Passing Data: Arguments • Pushed by Caller • Saved in caller frame • Just upon of return address • From Nth to 1st (from right to left) • Used by Callee • Relative to %ebp • Offset: 4 + 4*i + %ebp

  36. %ebp %esp Passing Data: Arguments push argument N

  37. %ebp %esp Passing Data: Arguments push argument N . . . push argument 1

  38. %ebp %esp Passing Data: Arguments push argument N . . . push argument 1 call callee

  39. %ebp %esp Passing Data: Arguments push argument N . . . push argument 1 call callee push %ebp

  40. %esp / %ebp Passing Data: Arguments push argument N . . . push argument 1 call callee push %ebp mov %esp, %ebp . . .

  41. Passing Data: Return Value • Specific register to keep the return value • %eax is used to pass the result of callee to caller

More Related