Lecture 9: Memory Layout
3.12-3.13 of textbook (CS:APP)

Sample code used in lecture to illustrate
arrays and pointers, structs, and unions

All code compiled with -m32 for x86 stack conventions
The assembly instruction
  leave
is equivalent to
  movl %ebp, %esp
  popl %ebp

add.c -- add to integers
stack.c -- show example of problems with returning pointer into current stack frame
overflow.c -- illustrating a buffer overflow attack
	      typing in more 12 or more characters will lead to an
              illegal instruction or a segmentation fault

The compiler in -O2 or -O1 mode optimizes stack.c to not move
anything into the stack frame at all.  With -O0, it will
write the values into the stack frame.  See the difference
by executing ./stack and ./stack0.

Compiled for x86 (-m32) except for union.c
--------------------------------------------------
gcc -O2 -o union union.c

gcc -O2 -m32 -S add.c

gcc -O2 -m32 -S stack.c
gcc -O2 -m32 -o stack stack.c

gcc -O0 -m32 -S -o stack0.s stack.c
gcc -O0 -m32 -o stack0 stack.c

gcc -O2 -m32 -S overflow.c
gcc -O2 -m32 -o overflow overflow.c
--------------------------------------------------
