Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University.

Slides:



Advertisements
Similar presentations
Machine-Level Programming III: Procedures Feb. 8, 2000 Topics IA32 stack Stack-based languages Stack frames Register saving conventions Creating pointers.
Advertisements

University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
Carnegie Mellon Instructors: Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures : Introduction.
University of Washington Last Time For loops  for loop → while loop → do-while loop → goto version  for loop → while loop → goto “jump to middle” version.
Machine-Level Programming III: Procedures Apr. 17, 2006 Topics IA32 stack discipline Register saving conventions Creating pointers to local variables CS213.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Machine-Level Programming II: Control Flow September 1, 2008 Topics Condition Codes Setting Testing Control Flow If-then-else Varieties of Loops Switch.
Machine-Level Programming III: Procedures Sept. 17, 2007 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
– 1 – , F’02 ICS05 Instructor: Peter A. Dinda TA: Bin Lin Recitation 4.
Machine-Level Programming III: Procedures Jan 30, 2003
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Introduction to x86 Assembly or “How does someone hack my laptop?”
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Y86 Processor State Program Registers
Carnegie Mellon Introduction to Computer Systems /18-243, spring 2009 Recitation, Jan. 14 th.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Lee CSCE 312 TAMU 1 Based on slides provided by Randy Bryant and Dave O’Hallaron Machine-Level Programming III: Switch Statements and IA32 Procedures Instructor:
Ithaca College Machine-Level Programming IV: IA32 Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2013 * Modified slides.
Machine-Level Programming II: Control Flow Topics Condition codes Conditional branches Loops Switch statements CS 105 “Tour of the Black Holes of Computing”
University of Washington x86 Programming III The Hardware/Software Interface CSE351 Winter 2013.
Machine-Level Programming V: Switch Statements Comp 21000: Introduction to Computer Systems & Assembly Lang Systems book chapter 3* * Modified slides from.
University of Washington Today More on procedures, stack etc. Lab 2 due today!  We hope it was fun! What is a stack?  And how about a stack frame? 1.
Fabián E. Bustamante, Spring 2007 Machine-Level Programming III - Procedures Today IA32 stack discipline Register saving conventions Creating pointers.
Machine-Level Programming: X86-64 Topics Registers Stack Function Calls Local Storage X86-64.ppt CS 105 Tour of Black Holes of Computing.
Machine-Level Programming III: Procedures Topics IA32 stack discipline Register-saving conventions Creating pointers to local variables CS 105 “Tour of.
University of Washington Today Lab 2 due next Monday! Finish-up control flow Switch statements 1.
Machine-Level Programming 3 Control Flow Topics Control Flow Switch Statements Jump Tables.
Carnegie Mellon Machine-Level Programming III: Switch Statements, 1-D Array and IA32 Procedures Lecture, Mar. 7, 2013 These slides are from
Carnegie Mellon 1 Odds and Ends Intro to x86-64 Memory Layout.
Machine-level Programming III: Procedures Topics –IA32 stack discipline –Register saving conventions –Creating pointers to local variables.
1 Procedure Call and Array. 2 Outline Data manipulation Control structure Suggested reading –Chap 3.7, 3.8.
Carnegie Mellon 1 Machine-Level Programming I: Basics Lecture, Feb. 21, 2013 These slides are from website which accompanies the.
F’08 Stack Discipline Aug. 27, 2008 Nathaniel Wesley Filardo Slides stolen from CMU , whence they were stolen from CMU CS 438.
Compiler Construction Code Generation Activation Records
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Carnegie Mellon Instructor: San Skulrattanakulchai Machine-Level Programming.
MACHINE-LEVEL PROGRAMMING III: SWITCH STATEMENTS AND IA32 PROCEDURES.
1 Assembly Language: Function Calls Jennifer Rexford.
IA32 Stack –Region of memory managed with stack discipline –Grows toward lower addresses –Register %esp indicates lowest stack address address of top element.
IA32: Control Flow Topics –Condition Codes Setting Testing –Control Flow If-then-else Varieties of Loops Switch Statements.
Section 5: Procedures & Stacks
A job ad at a game programming company
Reading Condition Codes (Cont.)
C function call conventions and the stack
CSCE 212 Computer Architecture
Machine-Level Programming III: Procedures
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Switch & Procedures & The Stack I CSE 351 Winter 2017
Machine-Level Programming III: Switch Statements and IA32 Procedures
Y86 Processor State Program Registers
Instructors: Majd Sakr and Khaled Harras
Machine-Level Programming 4 Procedures
Machine-Level Programming III: Procedures /18-213/14-513/15-513: Introduction to Computer Systems 7th Lecture, September 18, 2018.
Carnegie Mellon Machine-Level Programming III: Procedures : Introduction to Computer Systems October 22, 2015 Instructor: Rabi Mahapatra Authors:
Condition Codes Single Bit Registers
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine-Level Programming III: Procedures Sept 18, 2001
Today Control flow if/while/do while/for/switch
Machine-Level Programming: Introduction
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Machine-Level Representation of Programs (x86-64)
“Way easier than when we were students”
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Presentation transcript:

Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University

2 Outline Switch statements IA 32 Procedures  Stack Structure  Calling Conventions Seoul National University

3 Switch Statement Example Multiple case labels  Here: 5 & 6 Fall through cases  Here: 2 Missing cases  Here: 4 long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; /* Fall Through */ case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; } return w; } long switch_eg (long x, long y, long z) { long w = 1; switch(x) { case 1: w = y*z; break; case 2: w = y/z; /* Fall Through */ case 3: w += z; break; case 5: case 6: w -= z; break; default: w = 2; } return w; } Seoul National University

4 Jump Table Structure Code Block 0 Targ0: Code Block 1 Targ1: Code Block 2 Targ2: Code Block n–1 Targn-1: Targ0 Targ1 Targ2 Targn-1 jtab: target = JTab[x]; goto *target; target = JTab[x]; goto *target; switch(x) { case val_0: Block 0 case val_1: Block 1 case val_n-1: Block n–1 } switch(x) { case val_0: Block 0 case val_1: Block 1 case val_n-1: Block n–1 } Switch Form Approximate Translation Jump Table Jump Targets Seoul National University

5 Switch Statement Example (IA32) Setup: long switch_eg(long x, long y, long z) { long w = 1; switch(x) {... } return w; } long switch_eg(long x, long y, long z) { long w = 1; switch(x) {... } return w; } switch_eg: pushl%ebp# Setup movl%esp, %ebp# Setup movl8(%ebp), %eax# %eax = x cmpl$6, %eax# Compare x:6 ja.L2 # If unsigned > goto default jmp*.L7(,%eax,4)# Goto *JTab[x] What range of values takes default? Note that w not initialized here Seoul National University

6 Switch Statement Example (IA32) long switch_eg(long x, long y, long z) { long w = 1; switch(x) {... } return w; } long switch_eg(long x, long y, long z) { long w = 1; switch(x) {... } return w; } Indirect jump Jump table.section.rodata.align 4.L7:.long.L2# x = 0.long.L3# x = 1.long.L4# x = 2.long.L5# x = 3.long.L2# x = 4.long.L6# x = 5.long.L6# x = 6.section.rodata.align 4.L7:.long.L2# x = 0.long.L3# x = 1.long.L4# x = 2.long.L5# x = 3.long.L2# x = 4.long.L6# x = 5.long.L6# x = 6 Setup: switch_eg: pushl%ebp# Setup movl%esp, %ebp# Setup movl8(%ebp), %eax# eax = x cmpl$6, %eax# Compare x:6 ja.L2 # If unsigned > goto default jmp*.L7(,%eax,4)# Goto *JTab[x] Seoul National University

7 Assembly Setup Explanation Table Structure  Each target requires 4 bytes  Base address at.L7 Jumping  Direct: jmp.L2  Jump target is denoted by label.L2  Indirect: jmp *.L7(,%eax,4)  Start of jump table:.L7  Must scale by factor of 4 (labels have 32-bits = 4 Bytes on IA32)  Fetch target from effective Address.L7 + eax*4  Only for 0 ≤ x ≤ 6 Jump table.section.rodata.align 4.L7:.long.L2# x = 0.long.L3# x = 1.long.L4# x = 2.long.L5# x = 3.long.L2# x = 4.long.L6# x = 5.long.L6# x = 6.section.rodata.align 4.L7:.long.L2# x = 0.long.L3# x = 1.long.L4# x = 2.long.L5# x = 3.long.L2# x = 4.long.L6# x = 5.long.L6# x = 6 Seoul National University

8.section.rodata.align 4.L7:.long.L2# x = 0.long.L3# x = 1.long.L4# x = 2.long.L5# x = 3.long.L2# x = 4.long.L6# x = 5.long.L6# x = 6.section.rodata.align 4.L7:.long.L2# x = 0.long.L3# x = 1.long.L4# x = 2.long.L5# x = 3.long.L2# x = 4.long.L6# x = 5.long.L6# x = 6 Jump Table Jump table switch(x) { case 1: //.L3 w = y*z; break; case 2: //.L4 w = y/z; /* Fall Through */ case 3: //.L5 w += z; break; case 5: case 6: //.L6 w -= z; break; default: //.L2 w = 2; } switch(x) { case 1: //.L3 w = y*z; break; case 2: //.L4 w = y/z; /* Fall Through */ case 3: //.L5 w += z; break; case 5: case 6: //.L6 w -= z; break; default: //.L2 w = 2; } Seoul National University

9 Handling Fall-Through long w = 1;... switch(x) {... case 2: w = y/z; /* Fall Through */ case 3: w += z; break;... } long w = 1;... switch(x) {... case 2: w = y/z; /* Fall Through */ case 3: w += z; break;... } case 3: w = 1; goto merge; case 3: w = 1; goto merge; case 2: w = y/z; case 2: w = y/z; merge: w += z; merge: w += z; Seoul National University

10 Code Blocks (Partial).L2:# Default movl$2, %eax# w = 2 jmp.L8# Goto done.L5:# x == 3 movl$1, %eax# w = 1 jmp.L9# Goto merge.L3:# x == 1 movl16(%ebp), %eax # z imull12(%ebp), %eax # w = y*z jmp.L8# Goto done.L2:# Default movl$2, %eax# w = 2 jmp.L8# Goto done.L5:# x == 3 movl$1, %eax# w = 1 jmp.L9# Goto merge.L3:# x == 1 movl16(%ebp), %eax # z imull12(%ebp), %eax # w = y*z jmp.L8# Goto done switch(x) { case 1: //.L3 w = y*z; break;... case 3: //.L5 w += z; break;... default: //.L2 w = 2; } switch(x) { case 1: //.L3 w = y*z; break;... case 3: //.L5 w += z; break;... default: //.L2 w = 2; } Seoul National University

11 Code Blocks (Rest).L4:# x == 2 movl12(%ebp), %edx movl%edx, %eax sarl$31, %edx idivl16(%ebp)# w = y/z.L9:# merge: addl16(%ebp), %eax # w += z jmp.L8# goto done.L6:# x == 5, 6 movl$1, %eax # w = 1 subl16(%ebp), %eax # w = 1-z.L4:# x == 2 movl12(%ebp), %edx movl%edx, %eax sarl$31, %edx idivl16(%ebp)# w = y/z.L9:# merge: addl16(%ebp), %eax # w += z jmp.L8# goto done.L6:# x == 5, 6 movl$1, %eax # w = 1 subl16(%ebp), %eax # w = 1-z switch(x) {... case 2: //.L4 w = y/z; /* Fall Through */ merge: //.L9 w += z; break; case 5: case 6: //.L6 w -= z; break; } switch(x) {... case 2: //.L4 w = y/z; /* Fall Through */ merge: //.L9 w += z; break; case 5: case 6: //.L6 w -= z; break; } Seoul National University

12 Switch Code (Finish) Noteworthy Features  Jump table avoids sequencing through cases  Constant time, rather than linear  Use jump table to handle holes and duplicate tags  Use program sequencing to handle fall-through  Don’t initialize w = 1 unless really need it return w;.L8:# done: popl%ebp ret.L8:# done: popl%ebp ret Seoul National University

13 x86-64 Switch Implementation.section.rodata.align 8.L7:.quad.L2# x = 0.quad.L3# x = 1.quad.L4# x = 2.quad.L5# x = 3.quad.L2# x = 4.quad.L6# X = 5.quad.L6# x = 6.section.rodata.align 8.L7:.quad.L2# x = 0.quad.L3# x = 1.quad.L4# x = 2.quad.L5# x = 3.quad.L2# x = 4.quad.L6# X = 5.quad.L6# x = 6 Jump Table Same general idea, adapted to 64-bit code Table entries 64 bits (pointers) Cases use revised code.L3: movq%rdx, %rax imulq%rsi, %rax ret.L3: movq%rdx, %rax imulq%rsi, %rax ret switch(x) { case 1: //.L3 w = y*z; break;... } switch(x) { case 1: //.L3 w = y*z; break;... } Seoul National University

14 Summary C Control  if-then-else  do-while  while, for  switch Assembler Control  Conditional jump  Conditional move  Indirect jump  Compiler generates code sequence to implement more complex control Standard Techniques  Loops converted to do-while form  Large switch statements use jump tables  Sparse switch statements may use decision trees Seoul National University

15 Outline Switch statements IA 32 Procedures  Stack Structure  Calling Conventions Seoul National University

16 IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Register %esp contains lowest stack address  address of “top” element Stack Pointer: %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” Seoul National University

17 IA32 Stack: Push pushl Src  Fetch operand at Src  Decrement %esp by 4  Write operand at address given by %esp -4 Stack Grows Down Increasing Addresses Stack “Bottom” Stack Pointer: %esp Stack “Top” Seoul National University

18 Stack Pointer: %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” IA32 Stack: Pop +4 Seoul National University

19 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 the next instruction right after call  Example from disassembly e:e8 3d call 8048b :50 pushl %eax  Return address = 0x Procedure return: ret  Pop address from stack  Jump to address Seoul National University

20 0x x104 %esp %eip %esp %eip 0x8048b90 0x108 0x10c 0x110 0x104 0x804854e 123 Procedure Call Example 0x108 0x10c 0x x108 call 8048b e:e8 3d call 8048b :50 pushl %eax e:e8 3d call 8048b :50 pushl %eax %eip: program counter Seoul National University

21 %esp %eip 0x104 %esp %eip 0x x104 0x108 0x10c 0x110 0x Procedure Return Example 0x108 0x10c 0x ret :c3 ret 0x108 0x %eip: program counter Seoul National University

22 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 Seoul National University

23 Call Chain Example yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } amI(…) { amI(); } amI(…) { amI(); } yoo who amI Example Call Chain amI Procedure amI() is recursive Seoul National University

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

25 Example yoo who amI yoo %ebp %esp Stack yoo yoo(…) { who(); } yoo(…) { who(); } Seoul National University

26 yoo(…) { who(); } yoo(…) { who(); } Example yoo who amI yoo %ebp %esp Stack yoo who who(…) { amI(); amI(); } who(…) { amI(); amI(); } Seoul National University

27 yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } Example yoo who amI yoo %ebp %esp Stack yoo who amI amI(…) { amI(); } amI(…) { amI(); } Seoul National University

28 Example yoo who amI yoo %ebp %esp Stack yoo who amI yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } Seoul National University

29 Example yoo who amI yoo %ebp %esp Stack yoo who amI yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } Seoul National University

30 Example yoo who amI yoo %ebp %esp Stack yoo who amI yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } amI(…) { amI(); } Seoul National University

31 Example yoo who amI yoo %ebp %esp Stack yoo who amI yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } amI(…) { amI(); } amI(…) { amI(); } Seoul National University

32 Example yoo who amI yoo %ebp %esp Stack yoo who yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } Seoul National University

33 Example yoo who amI yoo %ebp %esp Stack yoo who amI yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } amI(…) { amI(); } amI(…) { amI(); } Seoul National University

34 Example yoo who amI yoo %ebp %esp Stack yoo who yoo(…) { who(); } yoo(…) { who(); } who(…) { amI(); amI(); } who(…) { amI(); amI(); } Seoul National University

35 Example yoo who amI yoo %ebp %esp Stack yoo yoo(…) { who(); } yoo(…) { who(); } Seoul National University

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

37 Revisiting swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } int course1 = 15213; int course2 = 18243; void call_swap() { swap(&course1, &course2); } int course1 = 15213; int course2 = 18243; void call_swap() { swap(&course1, &course2); } call_swap: subl$8, %esp movl$course2, 4(%esp) movl$course1, (%esp) callswap call_swap: subl$8, %esp movl$course2, 4(%esp) movl$course1, (%esp) callswap &course2 &course1 Rtn adr %esp Resulting Stack Calling swap from call_swap %esp subl call Seoul National University

38 Revisiting swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } swap: pushl%ebp movl%esp, %ebp pushl%ebx movl8(%ebp), %edx movl12(%ebp), %ecx movl(%edx), %ebx movl(%ecx), %eax movl%eax, (%edx) movl%ebx, (%ecx) popl%ebx popl%ebp ret Body Set Up Finish Seoul National University

39 swap Setup #1 swap: pushl %ebp movl %esp,%ebp pushl %ebx Resulting Stack &course2 &course1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp Seoul National University

40 swap Setup #2 swap: pushl %ebp movl %esp,%ebp pushl %ebx Resulting Stack &course2 &course1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp Seoul National University

41 swap Setup #3 swap: pushl %ebp movl %esp,%ebp pushl %ebx Resulting Stack &course2 &course1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp Old %ebx Seoul National University

42 swap Body movl 8(%ebp),%edx # get xp movl 12(%ebp),%ecx # get yp... Resulting Stack &course2 &course1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp Old %ebx Offset relative to %ebp Seoul National University

43 swap Finish Stack Before Finish popl%ebx popl%ebp yp xp Rtn adr Old %ebp %ebp %esp Old %ebx Resulting Stack yp xp Rtn adr %ebp %esp Observation  Saved and restored register %ebx  Not so for %eax, %ecx, %edx Seoul National University

44 Disassembled swap : :55 push %ebp :89 e5 mov %esp,%ebp :53 push %ebx :8b mov 0x8(%ebp),%edx b:8b 4d 0c mov 0xc(%ebp),%ecx e:8b 1a mov (%edx),%ebx :8b 01 mov (%ecx),%eax :89 02 mov %eax,(%edx) :89 19 mov %ebx,(%ecx) :5b pop %ebx :5d pop %ebp :c3 ret 80483b4:movl $0x ,0x4(%esp)# Copy &course bc:movl $0x ,(%esp)# Copy &course c3:call # Call swap 80483c8:leave # Prepare to return 80483c9:ret # Return Calling Code Seoul National University

45 Outline Switch statements IA 32 Procedures  Stack Structure  Calling Conventions Seoul National University

46 Register Saving Conventions When procedure yoo calls who :  yoo is the caller  who is the callee Can register be used for temporary storage?  Contents of register %edx overwritten by who  This could be trouble ➙ something should be done!  Need some coordination yoo: movl $15213, %edx call who addl %edx, %eax ret yoo: movl $15213, %edx call who addl %edx, %eax ret who: movl 8(%ebp), %edx addl $18243, %edx ret who: movl 8(%ebp), %edx addl $18243, %edx ret Seoul National University

47 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 values in its frame before the call  “Callee Save”  Callee saves temporary values in its frame before using Seoul National University

48 IA32/Linux+Windows 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 form of callee save  Restored to original values upon exit from procedure %eax %edx %ecx %ebx %esi %edi %esp %ebp Caller-Save Temporaries Callee-Save Temporaries Special Seoul National University