CS 208 w20 lecture 12 outline
1 Poll
What gets store in %rax by these instructions?
call next next: popq %rax
2 Another Look at Memory
3 Procedures Continued
3.1 Data Transfer
- up to six integer/pointer arguments passed via register
- in this order:
%rdi
,%rsi
,%rdx
,%rcx
,%r8
,%r9
- handy mnemonic: Diane's silk dress costs $8 9
- in this order:
- arguments 7 through \(n\) on the stack
- what the argument build area is for
- return value in
%rax
3.2 Local Storage on the Stack
- storage on the stack required when
- not enough registers
- code generates a pointer to a local variable (via
&
operator) - local variables that are arrays or structures
3.3 Example
void swap(long *xp, long *yp) { long t = *xp; *xp = *yp; *yp = t; } long f(long x, long y) { swap(&x, &y); return x - y; }
swap(long*, long*): movq (%rdi), %rax movq (%rsi), %rdx movq %rdx, (%rdi) movq %rax, (%rsi) ret f(long, long): subq $16, %rsp # allocate space on the stack by moving the stack pointer (grows DOWN) movq %rdi, 8(%rsp) # move x onto the stack movq %rsi, (%rsp) # move y onto the stack movq %rsp, %rsi # copy the stack address for y to rsi leaq 8(%rsp), %rdi # copy the stack address for x in rdi call swap(long*, long*) movq 8(%rsp), %rax subq (%rsp), %rax addq $16, %rsp # free stack space by moving the stack pointer back up ret
3.4 Poll
How many stack frames will be created? How many will be present on the stack at one time?
3.5 Local Storage in Registers
- want to make sure callee does not overwrite register values the caller plans to use later
- x86-64 has uniform conventions that all procedures must adhere to
%rbx
,%rbp
, and%r12
–%r15
are callee-saved registers (whenP
callsQ
,Q
must preserve these values)- preserving means not writing to that register or pushing its value onto the stack and popping it off to restore before returning (saved registers portion of the stack frame)
- all other registers besides the stack pointer
%rsp
are caller-saved (whenP
callsQ
,Q
is free to modify these registers, soP
must save any that it needs)
3.6 Recursive Procedures
- the stack and register conventions facilitate recursion without needing any extra apparatus
3.7 Example in gdb
- using
-tui
layout asm
thenlayout regs
to get assembly and register viewsfocus cmd
to have arrows go through command historywinheight regs -LINES
to have registers take up less of the screen- note
%rip
changes each step - draw stack as we go
%rsp
changes when we callcall_inrc
even beforesub $0x10,%rsp
—why?Print
vseXamine
- specifying type and width
long increment(long* p, long val) { long x = *p; long y = x + val; *p = y; return x; } long call_incr() { long v1 = 208; long v2 = increment(&v1, 124); return v1+v2; } int main() { call_incr(); }
increment: movq (%rdi), %rax addq %rax, %rsi movq %rsi, (%rdi) ret call_incr: subq $16, %rsp movq $208, 8(%rsp) movl $124, %esi leaq 8(%rsp), %rdi call increment addq 8(%rsp), %rax addq $16, %rsp ret main: movl $0, %eax call call_incr movl $0, %eax ret
3.8 Example of Register Saving
long call_incr2(long x) { long v1 = x; long v2 = increment(&v1, 124); return x + v2; }
call_incr2: pushq %rbx subq $16, %rsp movq %rdi, %rbx movq %rdi, 8(%rsp) movl $124, %esi leaq 8(%rsp), %rdi call increment addq %rbx, %rax addq $16, %rsp popq %rbx ret
4 Quiz; feedback
- Average 26.7, median 28
- did really well overall, especially on matching assembly to C
- Common confusions:
- math in hex (
0xe0 + 0x40 = 0x120
) - meaning of
%rcx
vs(%rcx)
- two's complment negatives ≠ sign and magnitude
- -3 is
0b1101
(-8 + 5), not0b1011
- -3 is
- math in hex (
- lab 1 feedback—lab took much longer than I inteded
- labs intended to be challenging, but not nearly that time consuming
- I have adjusted lab 2 grading scheme to hopefully avoid a repeat
- phases 1–3 16 points each, phases 4–6 worth 4, 3, and 2 points, respectively
- my goal is to minimze pressure to devote endless time to the lab when you have other things that need to take priority
- See annoucement about lab 2 compilation mishap