Machine-Level Programming III: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument.

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.
Instructor: Erol Sahin
Machine Programming – Procedures and IA32 Stack CENG334: Introduction to Operating Systems Instructor: Erol Sahin Acknowledgement: Most of the slides are.
X86 Programming CS 740 Sept. 12, 2007 Topics Basics Accessing and Moving Data Arithmetic operations Control Flow Procedures Data Structures.
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 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.
Stack Activation Records Topics IA32 stack discipline Register saving conventions Creating pointers to local variables February 6, 2003 CSCE 212H Computer.
Instructor: Erol Sahin
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.
1 1 Machine-Level Programming IV: x86-64 Procedures, Data Andrew Case Slides adapted from Jinyang Li, Randy Bryant & Dave O’Hallaron.
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.
64-Bit Architectures Topics 64-bit data New registers and instructions Calling conventions CS 105 “Tour of the Black Holes of Computing!”
Machine-Level Programming III: Switch Statements and IA32 Procedures Seoul National University.
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.
Recitation 2 – 2/11/02 Outline Stacks & Procedures Homogenous Data –Arrays –Nested Arrays Mengzhi Wang Office Hours: Thursday.
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.
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.
University of Washington x86 Programming I The Hardware/Software Interface CSE351 Winter 2013.
F’08 Stack Discipline Aug. 27, 2008 Nathaniel Wesley Filardo Slides stolen from CMU , whence they were stolen from CMU CS 438.
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.
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
The Stack & Procedures CSE 351 Spring 2017
Machine-Level Programming III: Procedures
Carnegie Mellon Machine-Level Programming III: Switch Statements and IA32 Procedures / : Introduction to Computer Systems 7th Lecture, Sep.
Machine-Level Programming 1 Introduction
Machine-Level Programming IV: x86-64 Procedures, Data
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
The Stack & Procedures CSE 351 Winter 2018
Machine Level Representation of Programs (IV)
Machine-Level Programming: Introduction
Roadmap C: Java: Assembly language: OS: Machine code: Computer system:
Machine Level Representation of Programs (IV)
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
Machine-Level Representation of Programs (x86-64)
Ithaca College Machine-Level Programming VII: Procedures Comp 21000: Introduction to Computer Systems & Assembly Lang Spring 2017.
“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: Procedures Sept. 15, 2006 IA32 stack discipline Register saving conventions Creating pointers to local variablesx86-64 Argument passing in registers Minimizing stack usage Using stack pointer as only reference class06.ppt “The course that gives CMU its Zip!” , F’06

– 2 – , F’06 IA32 Stack Region of memory managed with stack discipline Grows toward lower addresses Register %esp indicates lowest stack address address of top element Stack Pointer %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom”

– 3 – , F’06 IA32 Stack Pushing Pushing pushl Src Fetch operand at Src Decrement %esp by 4 Write operand at address given by %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” Stack Pointer %esp -4

– 4 – , F’06 IA32 Stack Popping Popping popl Dest Read operand at address given by %esp Increment %esp by 4 Write to Dest Stack Pointer %esp Stack Grows Down Increasing Addresses Stack “Top” Stack “Bottom” +4

– 5 – , F’06 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 value Address of instruction beyond 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

– 6 – , F’06 %esp %eip %esp %eip 0x804854e 0x108 0x10c 0x110 0x104 0x804854e 0x Procedure Call Example 0x108 0x10c 0x x108 call 8048b e:e8 3d call 8048b :50 pushl %eax 0x8048b90 0x104 %eip is program counter

– 7 – , F’06 %esp %eip 0x104 %esp %eip 0x x104 0x108 0x10c 0x110 0x Procedure Return Example 0x108 0x10c 0x ret :c3 ret 0x108 %eip is program counter 0x

– 8 – , F’06 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 – , F’06 Call Chain Example Code Structure yoo(…) { who(); } who(…) { amI(); amI(); } amI(…) { amI(); } yoo who amI Call Chain Procedure amI recursive amI

– 10 – , F’06 Stack Pointer %esp yoo who proc Frame Pointer %ebp Stack “Top” Stack Frames Contents Local variables Return information Temporary spaceManagement Space allocated when enter procedure “Set-up” code Deallocated when return “Finish” codePointers Stack pointer %esp indicates stack top Frame pointer %ebp indicates start of current frame amI

– 11 – , F’06 Stack Pointer %esp yoo Frame Pointer %ebp Stack Operation yoo Call Chain yoo(…) { who(); }

– 12 – , F’06 Stack Pointer %esp yoo who Frame Pointer %ebp Stack Operation yoo who Call Chain who(…) { amI(); amI(); }

– 13 – , F’06 Stack Pointer %esp yoo who amI Frame Pointer %ebp Stack Operation yoo who amI Call Chain amI(…) { amI(); }

– 14 – , F’06 Stack Pointer %esp yoo who amI Frame Pointer %ebp Stack Operation yoo who amI Call Chain amI(…) { amI(); } amI

– 15 – , F’06 Stack Pointer %esp yoo who amI Frame Pointer %ebp Stack Operation yoo who amI Call Chain amI(…) { amI(); } amI

– 16 – , F’06 Stack Pointer %esp yoo who amI Frame Pointer %ebp Stack Operation yoo who amI Call Chain amI(…) { amI(); } amI

– 17 – , F’06 Stack Pointer %esp yoo who amI Frame Pointer %ebp Stack Operation yoo who amI Call Chain amI(…) { amI(); } amI

– 18 – , F’06 Stack Pointer %esp yoo who Frame Pointer %ebp Stack Operation yoo who Call Chain who(…) { amI(); amI(); } amI

– 19 – , F’06 Stack Pointer %esp yoo who amI Frame Pointer %ebp Stack Operation yoo who Call Chain amI(…) { } amI

– 20 – , F’06 Stack Pointer %esp yoo who Frame Pointer %ebp Stack Operation yoo who Call Chain who(…) { amI(); amI(); } amI

– 21 – , F’06 yoo(…) { who(); } Stack Pointer %esp yoo Frame Pointer %ebp Stack Operation yoo who Call Chain amI

– 22 – , F’06 IA32/Linux Stack Frame Current Stack Frame (“Top” to Bottom) Parameters for function about to call “Argument build” 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 Stack Pointer ( %esp ) Frame Pointer ( %ebp ) Return Addr Saved Registers + Local Variables Argument Build Old %ebp Arguments Caller Frame

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

– 24 – , F’06 Revisiting swap void swap(int *xp, int *yp) { int t0 = *xp; int t1 = *yp; *xp = t1; *yp = t0; } 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 Body Set Up Finish

– 25 – , F’06 swap Setup #1 swap: pushl %ebp movl %esp,%ebp pushl %ebx Resulting Stack &zip2 &zip1 Rtn adr %esp Entering Stack %ebp yp xp Rtn adr Old %ebp %ebp %esp

– 26 – , F’06 swap Setup #2 swap: pushl %ebp movl %esp,%ebp pushl %ebx yp xp Rtn adr Old %ebp %ebp Resulting Stack &zip2 &zip1 Rtn adr %esp Entering Stack %ebp %esp

– 27 – , F’06 swap Setup #3 swap: pushl %ebp movl %esp,%ebp pushl %ebx yp xp Rtn adr Old %ebp %ebp Resulting Stack &zip2 &zip1 Rtn adr %esp Entering Stack %ebp Old %ebx %esp

– 28 – , F’06 Effect of swap Setup yp xp Rtn adr Old %ebp %ebp Offset (relative to %ebp ) Resulting Stack &zip2 &zip1 Rtn adr %esp Entering Stack %ebp Old %ebx %esp movl 12(%ebp),%ecx # get yp movl 8(%ebp),%edx # get xp... Body

– 29 – , F’06 swap Finish #1 movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr Old %ebp %ebp Offset swap ’s Stack Old %ebx %esp-4 Observation Saved & restored register %ebx yp xp Rtn adr Old %ebp %ebp Offset Old %ebx %esp-4

– 30 – , F’06 swap Finish #2 movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr Old %ebp %ebp Offset swap ’s Stack Old %ebx %esp-4 yp xp Rtn adr Old %ebp %ebp Offset swap ’s Stack %esp

– 31 – , F’06 swap Finish #3 movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret yp xp Rtn adr %ebp Offset swap ’s Stack yp xp Rtn adr Old %ebp %ebp Offset swap ’s Stack %esp

– 32 – , F’06 swap Finish #4 movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret &zip2 &zip1 %esp Exiting Stack %ebp Observation Saved & restored register %ebx Didn’t do so for %eax, %ecx, or %edx yp xp Rtn adr %ebp Offset swap ’s Stack %esp

– 33 – , F’06 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 yoo: movl $15213, %edx call who addl %edx, %eax ret who: movl 8(%ebp), %edx addl $91125, %edx ret

– 34 – , F’06 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

– 35 – , F’06 IA32/Linux Register Usage Integer Registers Two have special uses %ebp, %esp Three managed as callee-save %ebx, %esi, %edi Old values saved on stack prior to using Three managed as caller-save %eax, %edx, %ecx Do what you please, but expect any callee to do so, as well Register %eax also stores returned value %eax %edx %ecx %ebx %esi %edi %esp %ebp Caller-Save Temporaries Callee-Save Temporaries Special

– 36 – , F’06 int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1); return rval * x; }.globl rfact.type rfact: pushl %ebp movl %esp,%ebp pushl %ebx movl 8(%ebp),%ebx cmpl $1,%ebx jle.L78 leal -1(%ebx),%eax pushl %eax call rfact imull %ebx,%eax jmp.L79.align 4.L78: movl $1,%eax.L79: movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret Recursive Factorial Registers %eax used without first saving %ebx used, but save at beginning & restore at end

– 37 – , F’06 rfact: pushl %ebp movl %esp,%ebp pushl %ebx rfact: pushl %ebp movl %esp,%ebp pushl %ebx Rfact Stack Setup Entering Stack x Rtn adr 4 8 Caller %ebp 0 %esp Old %ebx -4 Callee x Rtn adr Caller %esp %ebp pre %ebp pre %ebx pre %ebp pre %ebx Old %ebp rfact: pushl %ebp movl %esp,%ebp pushl %ebx

– 38 – , F’06 Rfact Body Registers %ebx Stored value of x %eax Temporary value of x-1 Returned value from rfact(x-1) Returned value from this call movl 8(%ebp),%ebx# ebx = x cmpl $1,%ebx# Compare x : 1 jle.L78# If <= goto Term leal -1(%ebx),%eax# eax = x-1 pushl %eax# Push x-1 call rfact# rfact(x-1) imull %ebx,%eax# rval * x jmp.L79# Goto done.L78:# Term: movl $1,%eax# return val = 1.L79:# Done: int rfact(int x) { int rval; if (x <= 1) return 1; rval = rfact(x-1) ; return rval * x; } Recursion

– 39 – , F’06 Rfact Recursion x Rtn adr Old %ebp %ebp Old %ebx pushl %eax %esp x-1 %eax x %ebx x Rtn adr Old %ebp %ebp Old %ebx %esp %eax x %ebx x-1 leal -1(%ebx),%eax x Rtn adr Old %ebp %ebp Old %ebx x-1 %eax x %ebx %esp Rtn adr call rfact

– 40 – , F’06 (x-1)! Rfact Result x Rtn adr Old %ebp %ebp Old %ebx %esp x-1 imull %ebx,%eax x! %eax x %ebx x Rtn adr Old %ebp %ebp Old %ebx %esp x-1 (x-1)! %eax x %ebx Return from Call (x-1)! Assume that rfact(x-1) returns (x-1)! in register %eax

– 41 – , F’06 Rfact Completion movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret x Rtn adr Old %ebp %ebp Old %ebx %esp -4 x! %eax x %ebx x-1 -8 pre %ebp pre %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret x Rtn adr Old %ebp %ebp %esp x! %eax Old %ebx %ebx pre %ebp pre %ebx Old %ebx movl -4(%ebp),%ebx movl %ebp,%esp popl %ebp ret x Rtn adr %ebp %esp x! %eax Old %ebx %ebx pre %ebp pre %ebx

– 42 – , F’06 Pointer Code void s_helper (int x, int *accum) { if (x <= 1) return; else { int z = *accum * x; *accum = z; s_helper (x-1,accum); } int sfact(int x) { int val = 1; s_helper(x, &val); return val; } Top-Level Call Recursive Procedure Pass pointer to update location

– 43 – , F’06 Temp. Space %esp Creating & Initializing Pointer int sfact(int x) { int val = 1; s_helper(x, &val); return val; } _sfact: pushl %ebp# Save %ebp movl %esp,%ebp# Set %ebp subl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = x movl $1,-4(%ebp)# val = 1 Using Stack for Local Variable Variable val must be stored on stack Need to create pointer to it Compute pointer as - 4(%ebp) Push on stack as second argument Initial part of sfact x Rtn adr Old %ebp %ebp val = 1 Unused _sfact: pushl %ebp# Save %ebp movl %esp,%ebp# Set %ebp subl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = x movl $1,-4(%ebp)# val = 1 _sfact: pushl %ebp# Save %ebp movl %esp,%ebp# Set %ebp subl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = x movl $1,-4(%ebp)# val = 1 _sfact: pushl %ebp# Save %ebp movl %esp,%ebp# Set %ebp subl $16,%esp# Add 16 bytes movl 8(%ebp),%edx# edx = x movl $1,-4(%ebp)# val = 1

– 44 – , F’06 Passing Pointer int sfact(int x) { int val = 1; s_helper(x, &val); return val; } leal -4(%ebp),%eax# Compute &val pushl %eax# Push on stack pushl %edx# Push x call s_helper# call movl -4(%ebp),%eax# Return val # Finish Calling s_helper from sfact x Rtn adr Old %ebp %ebp val = 1 -4 Unused %esp x &val Stack at time of call leal -4(%ebp),%eax# Compute &val pushl %eax# Push on stack pushl %edx# Push x call s_helper# call movl -4(%ebp),%eax# Return val # Finish leal -4(%ebp),%eax# Compute &val pushl %eax# Push on stack pushl %edx# Push x call s_helper# call movl -4(%ebp),%eax# Return val # Finish val =x!

– 45 – , F’06 Using Pointer movl %ecx,%eax# z = x imull (%edx),%eax# z *= *accum movl %eax,(%edx)# *accum = z void s_helper (int x, int *accum) { int z = *accum * x; *accum = z; } Register %ecx holds x Register %edx holds accum Assume memory initally has value V Use access (%edx) to reference memory %edx (accum) V x x %eax %ecx V*x

– 46 – , F’06 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 Can be 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 %ebp and %esp Stack frame organization conventions

– 47 – , F’06 %rax %rbx %rcx %rdx %rsi %rdi %rsp %rbp x86-64 General Purpose Registers Twice the number of registers Accessible as 8, 16, 32, or 64 bits %eax %ebx %ecx %edx %esi %edi %esp %ebp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %r8d %r9d %r10d %r11d %r12d %r13d %r14d %r15d

– 48 – , F’06 %rax %rbx %rcx %rdx %rsi %rdi %rsp %rbp x86-64 Register Conventions %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 Return Value Callee Saved Argument #4 Argument #3 Argument #2 Argument #1 Stack Pointer Callee Saved Argument #5 Argument #6 Callee Saved Used for linking C: Callee Saved Callee Saved

– 49 – , F’06 x86-64 Registers Arguments passed to functions via registers If more than 6 integral parameters, then pass rest on stack These registers can be used as caller-saved as well All References to Stack Frame via Stack Pointer Eliminates need to update %ebp Other Registers 6+1 callee saved 2 or 3 have special uses

– 50 – , F’06 x86-64 Long Swap Operands passed in registers First ( xp ) in %rdi, second ( yp ) in %rsi 64-bit pointers No stack operations required Avoiding Stack Can hold all local information in registers void swap(long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } swap: movq(%rdi), %rdx movq(%rsi), %rax movq%rax, (%rdi) movq%rdx, (%rsi) ret

– 51 – , F’06 x86-64 Locals in the Red Zone Avoiding Stack Pointer Change Can hold all information within small window beyond stack pointer /* Swap, using local array */ void swap_a(long *xp, long *yp) { volatile long loc[2]; loc[0] = *xp; loc[1] = *yp; *xp = loc[1]; *yp = loc[0]; } swap_a: movq (%rdi), %rax movq %rax, -24(%rsp) movq (%rsi), %rax movq %rax, -16(%rsp) movq -16(%rsp), %rax movq %rax, (%rdi) movq -24(%rsp), %rax movq %rax, (%rsi) ret rtn Ptr unused %rsp −8−8 loc[1] loc[0] −16 −24

– 52 – , F’06 x86-64 NonLeaf without Stack Frame No values held while swap being invoked No callee save registers needed long scount = 0; /* Swap a[i] & a[i+1] */ void swap_ele_se (long a[], int i) { swap(&a[i], &a[i+1]); scount++; } swap_ele_se: movslq %esi,%rsi # Sign extend i leaq (%rdi,%rsi,8), %rdi # &a[i] leaq 8(%rdi), %rsi # &a[i+1] call swap # swap() incq scount(%rip) # scount++; ret

– 53 – , F’06 x86-64 Call using Jump When swap executes ret, it will return from swap_ele Possible since swap is a “tail call” long scount = 0; /* Swap a[i] & a[i+1] */ void swap_ele (long a[], int i) { swap(&a[i], &a[i+1]); } swap_ele: movslq %esi,%rsi # Sign extend i leaq (%rdi,%rsi,8), %rdi # &a[i] leaq 8(%rdi), %rsi # &a[i+1] jmp swap # swap()

– 54 – , F’06 x86-64 Stack Frame Example Keeps values of a and i in callee save registers Must set up stack frame to save these registers long sum = 0; /* Swap a[i] & a[i+1] */ void swap_ele_su (long a[], int i) { swap(&a[i], &a[i+1]); sum += a[i]; } swap_ele_su: movq %rbx, -16(%rsp) movslq %esi,%rbx movq %r12, -8(%rsp) movq %rdi, %r12 leaq (%rdi,%rbx,8), %rdi subq $16, %rsp leaq 8(%rdi), %rsi call swap movq (%r12,%rbx,8), %rax addq %rax, sum(%rip) movq (%rsp), %rbx movq 8(%rsp), %r12 addq $16, %rsp ret

– 55 – , F’06 Understanding x86-64 Stack Frame swap_ele_su: movq %rbx, -16(%rsp) # Save %rbx movslq %esi,%rbx # Extend & save i movq %r12, -8(%rsp) # Save %r12 movq %rdi, %r12 # Save a leaq (%rdi,%rbx,8), %rdi # &a[i] subq $16, %rsp # Allocate stack frame leaq 8(%rdi), %rsi # &a[i+1] call swap # swap() movq (%r12,%rbx,8), %rax # a[i] addq %rax, sum(%rip) # sum += a[i] movq (%rsp), %rbx # Restore %rbx movq 8(%rsp), %r12 # Restore %r12 addq $16, %rsp # Deallocate stack frame ret

– 56 – , F’06 Stack Operations movq %rbx, -16(%rsp) # Save %rbx movq %r12, -8(%rsp) # Save %r12 subq $16, %rsp # Allocate stack frame rtn Ptr %r12 %rsp −8−8 %rbx −16 rtn Ptr %r12 %rsp +8+8 %rbx movq (%rsp), %rbx # Restore %rbx movq 8(%rsp), %r12 # Restore %r12 addq $16, %rsp # Deallocate stack frame

– 57 – , F’06 Interesting Features of Stack Frame Allocate Entire Frame at Once All stack accesses can be relative to %rsp Do by decrementing stack pointer Can delay allocation, since safe to temporarily use red zone Simple Deallocation Increment stack pointer

– 58 – , F’06 x86-64 Procedure Summary Heavy Use of Registers Parameter passing More temporaries Minimal Use of Stack Sometimes none Allocate/deallocate entire block Many Tricky Optimizations What kind of stack frame to use Calling with jump Various allocation techniques