University of Washington
Section 5: Procedures & Stacks
óð Stacks in memory and stack operations
óð The stack used to keep track of procedure calls
óð Return addresses and return values
óð Stack-based languages
óð The Linux stack frame
óð Passing arguments on the stack
óð Allocating local variables on the stack
óð Register-saving conventions
óð Procedures and stacks on x64 architecture
x64 Procedures and Stacks
University of Washington
x86-64 Procedure Calling Convention
óð Doubling of registers makes us less dependent on stack
żð Store argument in registers
żð Store temporary variables in registers
óð What do we do if we have too many arguments or too many
temporary variables?
x64 Procedures and Stacks
University of Washington
x86-64 64-bit Registers: Usage Conventions
Return value Argument #5
%rax %r8
Callee saved Argument #6
%rbx %r9
Caller saved
%rcx Argument #4 %r10
Caller Saved
%rdx Argument #3 %r11
Callee saved
%rsi Argument #2 %r12
Argument #1 Callee saved
%rdi %r13
Stack pointer Callee saved
%rsp %r14
Callee saved
%rbp Callee saved %r15
x64 Procedures and Stacks
University of Washington
Revisiting swap, IA32 vs. x86-64 versions
swap: swap (64-bit long ints):
pushl %ebp movq (%rdi), %rdx
Set
movl %esp,%ebp movq (%rsi), %rax
Up
pushl %ebx movq %rax, (%rdi)
movq %rdx, (%rsi)
movl 12(%ebp),%ecx ret
movl 8(%ebp),%edx
movl (%ecx),%eax óð Arguments passed in registers
Body
movl (%edx),%ebx
żð First (xp) in %rdi,
movl %eax,(%edx)
second (yp) in %rsi
movl %ebx,(%ecx)
żð 64-bit pointers
movl -4(%ebp),%ebx
óð No stack operations
movl %ebp,%esp
Finish
required (except ret)
popl %ebp
ret
óð Avoiding stack
żð Can hold all local information
in registers
x64 Procedures and Stacks
University of Washington
X86-64 procedure call highlights
óð Arguments (up to first 6) in registers
żð Faster to get these values from registers than from stack in memory
óð Local variables also in registers (if there is room)
óð callq instruction stores 64-bit return address on stack
żð Address pushed onto stack, decrementing %rsp by 8
óð No frame pointer
żð All references to stack frame made relative to %rsp; eliminates need to
update %ebp/%rbp, which is now available for general-purpose use
óð Functions can access memory up to 128 bytes beyond %rsp:
the red zone
żð Can store some temps on stack without altering %rsp
óð Registers still designated caller-saved or callee-saved
x64 Procedures and Stacks
University of Washington
x86-64 Stack Frames
óð Often (ideally), x86-64 functions need no stack frame at all
żð Just a return address is pushed onto the stack when a function call is
made
óð A function does need a stack frame when it:
żð Has too many local variables to hold in registers
żð Has local variables that are arrays or structs
żð Uses the address-of operator (&) to compute the address of a local
variable
żð Calls another function that takes more than six arguments
żð Needs to save the state of callee-save registers before modifying them
x64 Procedures and Stacks
University of Washington
Example
long int call_proc() call_proc:
{ subq $32,%rsp
long x1 = 1; movq $1,16(%rsp)
int x2 = 2; movl $2,24(%rsp)
short x3 = 3; movw $3,28(%rsp)
char x4 = 4; movb $4,31(%rsp)
proc(x1, &x1, x2, &x2, " " "
x3, &x3, x4, &x4);
return (x1+x2)*(x3-x4);
}
%rsp
Return address to caller of call_proc
NB: Details may vary
depending on compiler.
x64 Procedures and Stacks
University of Washington
Example
long int call_proc() call_proc:
{ subq $32,%rsp
long x1 = 1; movq $1,16(%rsp)
int x2 = 2; movl $2,24(%rsp)
short x3 = 3; movw $3,28(%rsp)
char x4 = 4; movb $4,31(%rsp)
proc(x1, &x1, x2, &x2, " " "
x3, &x3, x4, &x4);
return (x1+x2)*(x3-x4);
}
Return address to caller of call_proc
x4 x3 x2
x1
%rsp
x64 Procedures and Stacks
University of Washington
Example
long int call_proc() call_proc:
{ " " "
long x1 = 1; movq $1,%rdi
int x2 = 2; leaq 16(%rsp),%rsi
short x3 = 3; movl $2,%edx
char x4 = 4; leaq 24(%rsp),%rcx
proc(x1, &x1, x2, &x2, movl $3,%r8d
x3, &x3, x4, &x4); leaq 28(%rsp),%r9
return (x1+x2)*(x3-x4); movl $4,(%rsp)
} leaq 31(%rsp),%rax
movq %rax,8(%rsp)
Return address to caller of call_proc
call proc
" " "
x4 x3 x2
x1
Arguments passed in (in order):
rdi, rsi, rdx, rcx, r8, r9, then stack
Arg 8
%rsp
Arg 7
x64 Procedures and Stacks
University of Washington
Example
long int call_proc() call_proc:
{ " " "
long x1 = 1; movq $1,%rdi
int x2 = 2; leaq 16(%rsp),%rsi
short x3 = 3; movl $2,%edx
char x4 = 4; leaq 24(%rsp),%rcx
proc(x1, &x1, x2, &x2, movl $3,%r8d
x3, &x3, x4, &x4); leaq 28(%rsp),%r9
return (x1+x2)*(x3-x4); movl $4,(%rsp)
} leaq 31(%rsp),%rax
movq %rax,8(%rsp)
Return address to caller of call_proc
call proc
" " "
x4 x3 x2
x1
Arguments passed in (in order):
rdi, rsi, rdx, rcx, r8, r9, then stack
Arg 8
Arg 7
%rsp
Return address to line after call to proc
x64 Procedures and Stacks
University of Washington
Example
long int call_proc() call_proc:
{ " " "
long x1 = 1; movswl 28(%rsp),%eax
int x2 = 2; movsbl 31(%rsp),%edx
short x3 = 3; subl %edx,%eax
char x4 = 4; cltq
proc(x1, &x1, x2, &x2, movslq 24(%rsp),%rdx
x3, &x3, x4, &x4); addq 16(%rsp),%rdx
return (x1+x2)*(x3-x4); imulq %rdx,%rax
} addq $32,%rsp
ret
Return address to caller of call_proc
x4 x3 x2
x1
Arg 8
%rsp
Arg 7
x64 Procedures and Stacks
University of Washington
Example
long int call_proc() call_proc:
{ " " "
long x1 = 1; movswl 28(%rsp),%eax
int x2 = 2; movsbl 31(%rsp),%edx
short x3 = 3; subl %edx,%eax
char x4 = 4; cltq
proc(x1, &x1, x2, &x2, movslq 24(%rsp),%rdx
x3, &x3, x4, &x4); addq 16(%rsp),%rdx
return (x1+x2)*(x3-x4); imulq %rdx,%rax
} addq $32,%rsp
ret
Return address to caller of call_proc
%rsp
x64 Procedures and Stacks
University of Washington
x86-64 Procedure Summary
óð Heavy use of registers (faster than using stack in memory)
żð Parameter passing
żð More temporaries since more registers
óð Minimal use of stack
żð Sometimes none
żð When needed, allocate/deallocate entire frame at once
żð No more frame pointer: address relative to stack pointer
óð More room for compiler optimizations
żð Prefer to store data in registers rather than memory
żð Minimize modifications to stack pointer
x64 Procedures and Stacks
Wyszukiwarka
Podobne podstrony:
06 x86 64 Procedures and Stacks02 x86 vs x86 6402 Procedure?lls and Returns06 Memory Related Perils and Pitfalls06 Memory Related Perils and Pitfalls2008 01 Music Makers Tuning Up with the 64 Studio and Jad Audio Linux Distros02 x86 vs x86 64SHSpec 06 6402C25 What Auditing Is and What It Isn t06?TECT AND FILTERING OF HARMONICS01 Stacks in Memory and Stack Operationsduties and proceduresSHSpec 025 6107C05 Q and A Period Procedures in Auditing06 User Guide for Artlantis Studio and Artlantis Render Export Add onswięcej podobnych podstron