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

mem-layoutv2.png

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
  • 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

call-graph.png

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 (when P calls Q, 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 (when P calls Q, Q is free to modify these registers, so P must save any that it needs)

reg-conventions.png

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 then layout regs to get assembly and register views
  • focus cmd to have arrows go through command history
  • winheight 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 call call_inrc even before sub $0x10,%rsp—why?
  • Print vs eXamine
    • 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), not 0b1011

status-week4.png

  • 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