1 / 123

Instructor: Erol Sahin

Machine Programming – Procedures and IA32 Stack CENG331: Introduction to Computer Systems 6 th Lecture. Instructor: Erol Sahin. Acknowledgement: Most of the slides are adapted from the ones prepared by R.E. Bryant, D.R. O’Hallaron of Carnegie-Mellon Univ. IA32 Stack. Stack “Bottom”.

Download Presentation

Instructor: Erol Sahin

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. Machine Programming – Procedures and IA32 StackCENG331: Introduction to Computer Systems6th Lecture Instructor: ErolSahin • Acknowledgement: Most of the slides are adapted from the ones prepared • by R.E. Bryant, D.R. O’Hallaron of Carnegie-Mellon Univ.

  2. IA32 Stack Stack “Bottom” • Region of memory managed with stack discipline • Grows toward lower addresses • Register %espcontains lowest stack address= address of “top” element Increasing Addresses Stack Grows Down Stack Pointer: %esp Stack “Top”

  3. IA32 Stack: Push Stack “Bottom” • pushlSrc • Fetch operand at Src • Decrement %espby 4 • Write operand at address given by %esp Increasing Addresses Stack Grows Down -4 Stack Pointer: %esp Stack “Top”

  4. IA32 Stack: Pop Stack “Bottom” • poplDest • Read operand at address %esp • Increment %espby 4 • Write operand to Dest Increasing Addresses Stack Grows Down +4 Stack Pointer: %esp Stack “Top”

  5. Procedure Control Flow • Use stack to support procedure call and return • Procedure call: call label • Push return address on stack • Jump to label • Return address: • Address of instruction beyond call • Example from disassembly 804854e: e8 3d 06 00 00 call 8048b90 <main> 8048553: 50 pushl %eax • Return address = 0x8048553 • Procedure return: ret • Pop address from stack • Jump to address

  6. Procedure Call Example • 804854e: e8 3d 06 00 00 call 8048b90 <main> • 8048553: 50 pushl %eax call 8048b90 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 %esp 0x108 %esp 0x108 0x104 %eip 0x804854e %eip 0x804854e 0x8048b90 %eip: program counter

  7. Procedure Return Example • 8048591: c3 ret ret 0x110 0x110 0x10c 0x10c 0x108 123 0x108 123 0x104 0x8048553 0x8048553 %esp 0x104 %esp 0x104 0x108 %eip 0x8048591 %eip 0x8048591 0x8048553 %eip: program counter

  8. Stack-Based Languages • Languages that support recursion • e.g., C, Pascal, Java • Code must be “Reentrant” • Multiple simultaneous instantiations of single procedure • Need some place to store state of each instantiation • Arguments • Local variables • Return pointer • Stack discipline • State for given procedure needed for limited time • From when called to when return • Callee returns before caller does • Stack allocated in Frames • state for single procedure instantiation

  9. Call Chain Example Example Call Chain • yoo(…) • { • • • • • who(); • • • • • } yoo • who(…) • { • • • • • amI(); • • • • • amI(); • • • • • } who amI amI • amI(…) • { • • • • • amI(); • • • • • } amI amI • Procedure amIis recursive

  10. Stack Frames Previous Frame • Contents • Local variables • Return information • Temporary space • Management • Space allocated when enter procedure • “Set-up” code • Deallocated when return • “Finish” code Frame Pointer: %ebp Frame for proc Stack Pointer: %esp Stack “Top”

  11. Stack Example • yoo(…) • { • • • • • who(); • • • • • } yoo %ebp yoo who %esp amI amI amI amI

  12. Stack Example • who(…) • { • • • • • amI(); • • • • • amI(); • • • • • } yoo yoo who %ebp who amI amI %esp amI amI

  13. Stack Example • amI(…) • { • • • • • amI(); • • • • • } yoo yoo who who amI amI %ebp amI amI %esp amI

  14. Stack Example • amI(…) • { • • • • • amI(); • • • • • } yoo yoo who who amI amI amI amI amI %ebp amI %esp

  15. Stack Example • amI(…) • { • • • • • amI(); • • • • • } yoo yoo who who amI amI amI amI amI amI %ebp amI %esp

  16. Stack Example • amI(…) • { • • • • • amI(); • • • • • } yoo yoo who who amI amI amI amI amI %ebp amI %esp

  17. Stack Example • amI(…) • { • • • • • amI(); • • • • • } yoo yoo who who amI amI %ebp amI amI %esp amI

  18. Stack Example • who(…) • { • • • • • amI(); • • • • • amI(); • • • • • } yoo yoo who %ebp who amI amI %esp amI amI

  19. Stack Example • amI(…) • { • • • • • • • • • • • } yoo yoo who who amI amI %ebp amI amI %esp amI

  20. Stack Example • who(…) • { • • • • • amI(); • • • • • amI(); • • • • • } yoo yoo who %ebp who amI amI %esp amI amI

  21. Stack Example • yoo(…) • { • • • • • who(); • • • • • } yoo %ebp yoo who %esp amI amI amI amI

  22. IA32/Linux Stack Frame • Current Stack Frame (“Top” to Bottom) • “Argument build:”Parameters for function about to call • Local variablesIf can’t keep in registers • Saved register context • Old frame pointer • Caller Stack Frame • Return address • Pushed by call instruction • Arguments for this call Caller Frame Arguments Frame pointer%ebp Return Addr Old %ebp Saved Registers + Local Variables Argument Build Stack pointer %esp

  23. Revisiting swap Calling swap from call_swap int zip1 = 15213; int zip2 = 91125; void call_swap() { swap(&zip1, &zip2); } call_swap: • • • pushl $zip2 # Global Var pushl $zip1 # Global Var call swap • • • • • • Resulting Stack void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } &zip2 &zip1 Rtnadr %esp

  24. Revisiting swap swap: pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp),%edx movl (%ecx),%eax movl (%edx),%ebx movl %eax,(%edx) movl %ebx,(%ecx) movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Set Up void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } Body Finish

  25. swap Setup #1 Entering Stack Resulting Stack %ebp %ebp • • • • • • &zip2 yp &zip1 xp Rtnadr %esp Rtnadr Old %ebp %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx

  26. swap Setup #1 Entering Stack %ebp %ebp • • • • • • &zip2 yp &zip1 xp Rtnadr %esp Rtnadr Old %ebp %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx

  27. swap Setup #1 Entering Stack Resulting Stack %ebp • • • • • • &zip2 yp &zip1 xp Rtnadr %esp Rtnadr Old %ebp %ebp %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx

  28. swap Setup #1 Entering Stack %ebp • • • • • • &zip2 yp &zip1 xp Rtnadr %esp Rtnadr Old %ebp %ebp %esp swap: pushl %ebp movl %esp,%ebp pushl %ebx

  29. swap Setup #1 Entering Stack Resulting Stack %ebp • • • • • • Offset relative to %ebp &zip2 12 yp &zip1 8 xp Rtnadr 4 %esp Rtnadr Old %ebp %ebp Old %ebx %esp movl 12(%ebp),%ecx # get yp movl 8(%ebp),%edx # get xp . . .

  30. swapFinish #1 swap’s Stack Resulting Stack • • • • • • yp yp xp xp Rtnadr Rtnadr Old %ebp Old %ebp %ebp %ebp Old %ebx Old %ebx %esp %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Observation: Saved and restored register %ebx

  31. swapFinish #2 swap’s Stack • • • • • • yp yp xp xp Rtnadr Rtnadr Old %ebp Old %ebp %ebp %ebp Old %ebx Old %ebx %esp %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret

  32. swapFinish #2 swap’s Stack Resulting Stack • • • • • • yp yp xp xp Rtnadr Rtnadr Old %ebp Old %ebp %ebp %ebp %esp Old %ebx %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret

  33. swapFinish #2 swap’s Stack • • • • • • yp yp xp xp Rtnadr Rtnadr Old %ebp Old %ebp %ebp %ebp %esp Old %ebx %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret

  34. swapFinish #3 swap’s Stack Resulting Stack %ebp • • • • • • yp yp xp xp Rtnadr Rtnadr %esp Old %ebp %ebp Old %ebx %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret

  35. swapFinish #4 swap’s Stack %ebp • • • • • • yp yp xp xp Rtnadr Rtnadr %esp Old %ebp %ebp Old %ebx %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret

  36. swapFinish #4 swap’s Stack Resulting Stack %ebp • • • • • • yp yp xp xp %esp Rtnadr Old %ebp %ebp Old %ebx %esp movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret • Observation • Saved & restored register %ebx • Didn’t do so for %eax, %ecx, or %edx

  37. Disassembled swap 080483a4 <swap>: 80483a4: 55 push %ebp 80483a5: 89 e5 mov %esp,%ebp 80483a7: 53 push %ebx 80483a8: 8b 55 08 mov 0x8(%ebp),%edx 80483ab: 8b 4d 0c mov 0xc(%ebp),%ecx 80483ae: 8b 1a mov (%edx),%ebx 80483b0: 8b 01 mov (%ecx),%eax 80483b2: 89 02 mov %eax,(%edx) 80483b4: 89 19 mov %ebx,(%ecx) 80483b6: 5b pop %ebx 80483b7: c9 leave 80483b8: c3 ret Calling Code 8048409: e8 96 ff ff ff call 80483a4 <swap> 804840e: 8b 45 f8 mov 0xfffffff8(%ebp),%eax

  38. Register Saving Conventions • When procedure yoo calls who: • yoois the caller • whois the callee • Can Register be used for temporary storage? • Contents of register %edxoverwritten by who • yoo: • • • • • movl $15213, %edx • call who • addl %edx, %eax • • • • • ret • who: • • • • • movl 8(%ebp), %edx • addl $91125, %edx • • • • • ret

  39. Register Saving Conventions • When procedure yoo calls who: • yoo is the caller • who is the callee • Can register be used for temporary storage? • Conventions • “Caller Save” • Caller saves temporary in its frame before calling • “Callee Save” • Callee saves temporary in its frame before using

  40. IA32/Linux Register Usage • %eax, %edx, %ecx • Caller saves prior to call if values are used later • %eax • also used to return integer value • %ebx, %esi, %edi • Callee saves if wants to use them • %esp, %ebp • special %eax Caller-Save Temporaries %edx %ecx %ebx Callee-Save Temporaries %esi %edi %esp Special %ebp

  41. IA 32 Procedure Summary • The Stack Makes Recursion Work • Private storage for each instance of procedure call • Instantiations don’t clobber each other • Addressing of locals + arguments can be relative to stack positions • Managed by stack discipline • Procedures return in inverse order of calls • IA32 Procedures Combination of Instructions+ Conventions • Call / Ret instructions • Register usage conventions • Caller / Callee save • %ebpand %esp • Stack frame organization conventions Caller Frame Arguments Return Addr %ebp Old %ebp Saved Registers + Local Variables Argument Build %esp

  42. Today • Arrays • One-dimensional • Multi-dimensional (nested) • Multi-level • Structures

  43. Basic Data Types • Integral • Stored & operated on in general (integer) registers • Signed vs. unsigned depends on instructions used Intel GAS Bytes C byte b 1 [unsigned] char word w 2 [unsigned] short double word l 4 [unsigned]int quad word q 8 [unsigned] long int(x86-64) • Floating Point • Stored & operated on in floating point registers Intel GAS Bytes C Single s 4 float Double l 8 double Extended t 10/12/16 long double

  44. Array Allocation • Basic Principle TA[L]; • Array of data type T and length L • Contiguously allocated region of L * sizeof(T) bytes char string[12]; x x + 12 int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20 double a[3]; x x + 8 x + 16 x + 24 char *p[3]; IA32 x x + 4 x + 8 x + 12 x86-64 x x + 8 x + 16 x + 24

  45. 1 5 2 1 3 Array Access • Basic Principle TA[L]; • Array of data type T and length L • Identifier A can be used as a pointer to array element 0: Type T* • Reference Type Value val[4] int3 valint * x val+1int * x + 4 &val[2]int * x + 8 val[5]int?? *(val+1)int5 val + iint * x + 4i int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20 Will disappear Blackboard?

  46. 1 5 2 1 3 Array Access • Basic Principle TA[L]; • Array of data type T and length L • Identifier A can be used as a pointer to array element 0: Type T* • Reference Type Value val[4] int3 valint * x val+1int * x + 4 &val[2]int * x + 8 val[5]int?? *(val+1)int5 val + iint * x + 4i int val[5]; x x + 4 x + 8 x + 12 x + 16 x + 20

  47. 1 9 0 5 4 2 7 2 1 1 3 2 0 3 9 Array Example typedef int zip_dig[5]; zip_dig cmu = { 1, 5, 2, 1, 3 }; zip_dig mit = { 0, 2, 1, 3, 9 }; zip_dig ucb = { 9, 4, 7, 2, 0 }; • Declaration “zip_digcmu” equivalent to “intcmu[5]” • Example arrays were allocated in successive 20 byte blocks • Not guaranteed to happen in general zip_digcmu; zip_digmit; 56 36 16 40 60 20 64 24 44 68 28 48 52 32 72 56 36 76 zip_digucb;

  48. 1 5 2 1 3 Array Accessing Example • Register %edx contains starting address of array • Register %eax contains array index • Desired digit at 4*%eax + %edx • Use memory reference (%edx,%eax,4) zip_digcmu; int get_digit (zip_dig z, int dig) { return z[dig]; } 16 20 24 28 32 36 IA32 # %edx = z # %eax = dig movl (%edx,%eax,4),%eax # z[dig]

  49. 1 9 0 5 4 2 2 7 1 3 2 1 9 0 3 Referencing Examples zip_digcmu; • Reference Address Value Guaranteed? mit[3] 36 + 4* 3 = 48 3 mit[5] 36 + 4* 5 = 56 9 mit[-1] 36 + 4*-1 = 32 3 cmu[15] 16 + 4*15 = 76 ?? zip_digmit; zip_digucb; 56 36 16 40 20 60 24 64 44 68 48 28 72 32 52 56 36 76 Will disappear Blackboard?

  50. 9 1 0 2 4 5 7 2 1 3 1 2 0 9 3 Referencing Examples zip_digcmu; • Reference Address Value Guaranteed? mit[3] 36 + 4* 3 = 48 3 mit[5] 36 + 4* 5 = 56 9 mit[-1] 36 + 4*-1 = 32 3 cmu[15] 16 + 4*15 = 76 ?? • No bound checking • Out of range behavior implementation-dependent • No guaranteed relative allocation of different arrays zip_digmit; zip_digmit; 36 56 16 40 60 20 64 24 44 68 28 48 32 52 72 56 36 76 Yes No No No

More Related