# Objectives

* Illustrate the x86-64 stack discipline used by the compiler
* Identify how parameters are passed
* Know caller/callee save registers
* Explain how call/ret work to implement procedure

# Reading

* Section 3.7 in the textbook

# Outline

* Note, compile with *-fno-stack-protector*
* gcc -Og -g -S -fno-stack-protector multstore.c
* Start with *multstore* compile and draw on board the stack (all labeled). Be sure to use Fig. 3.2 on page 180 to make sense of register stuff. Use **gdb** to drive the entire thing.

   * gdb -tui a.out (see the [manual](https://ftp.gnu.org/old-gnu/Manuals/gdb/html_chapter/gdb_19.html) for all the details on TUI)
   * **break** main
   * **break** *\*main* + *offset* to break at specific assembly instruction
   * **del** # where # is the breakpoint number
   * **run**
   * **layout asm**
   * **layout regs**
   * **layout src**
   * **layout split** two things displayed
   * **refresh** if the tui stuff gets all wonky and garbled
   * **focus cmd** focuses on the cmd so arrows/scroll work
   * **ni** or **si** to single step instructions where **ni** is step over and **si** is step in
   * **p/f $rsp** to show value of a register or local variable--f is the printf format specifier
   * **x/f $rsp** to get memory at the address

* Repeat for *incr* but have class draw the stack
* Repeat for *rfact*
* Do *practice-3-35.txt* (page 255)

# Notes

* Ignore stack passed parameters and only focus on registers for now
* Review the behavior of call/ret (Draw the stack frame)
* Spend time explaining the role of %rsp (it grows down-subtract to allocate)
* Make clear the role of %rip (holds the address of the next instruction to execute)