🔬 Lab
CS 240 Lab 8
Learning Goals & Reflection
CS 240 Lab 8
Learning Goals
Core Goals
Students can:
- Identify and explain core x86 assembly instructions:
- Look up the reference for an unfamiliar assembly instruction and understand what it is saying.
- Explain what the
lea
instruction does. - Explain what the
push
andpop
instructions do.
- Explain the usage of key x86 registers.
- Explain what the
%rsp
register is used for in x86 assembly code. - Explain the usual role of the
%rbp
register. - Explain the difference between caller-saved and callee-saved registers.
- Look up which registers are caller-saved vs. callee-saved.
- Explain what the
- Use
gdb
to examine information about the stack:- Use the
backtrace
orbt
command ingdb
to display information about currently active function calls. - Use the
disas
command to “look up” where a return address goes. - Look up format arguments for the
examine
orx
command. - Use
x
with$rsp
to display specific values that a function has saved on the stack, including its return address. - Use
x
with$rsp
to display a function’s entire stack frame and/or to display multiple stack frames. - Draw a memory diagram showing the boundaries of stack frames, return
addresses, and key saved values, based on output from
gdb
’sx
anddisas
commands.
- Use the
- Explain how the C compiler uses assembly code to produce a “stack
frame” for each function call:
- Identify which instructions are used for saving and restoring both caller-saved and callee-saved registers.
- Explain the purpose of subtracting from or adding to
%rsp
. - Based on a function’s assembly code and a memory dump of the stack,
identify which instructions & memory addresses are used to:
- Store callee-saved registers.
- Store caller-saved registers.
- Store local variables of the function.
- Align the stack pointer before a function call.
- Restore the stack pointer and caller-saved registers before returning from the function.
- Restore callee-saved registers after returning from a function call.
- Explain how functions use the stack to communicate with other
functions:
- Explain why a function can pass a pointer to a local variable into a function it calls, but cannot return such a pointer.
- Explain how function call frames operate when dealing with a recursive function.
- Explain what will happen if a function call returns a pointer to memory allocated on its stack.
- Reverse engineer an executable file to figure out what the code is
doing.
- Given assembly code that uses a loop to check relationships among a sequence of numbers, figure out exactly what the loop checks for, and provide a sequence of numbers that will pass the checks.
Stretch goals
- Use
gdb
to examine information about the stack:- Guess which values in a memory display are return addresses based on their hexadecimal values.
- Explain the advantages and properties of a stack-based compiler
design:
- Explain what limitations a computer that stores the return address in a dedicated register instead of in memory would have.
- Explain the limitations of a language like early Fortran which does not have a stack and which allocates memory per-function instead of per-function-call.
- Explain why even though at a code level, one function may call multiple other functions like a branching tree, we can use a linear stack that grows and shrinks to hold a function call frame for each function call.
- Reverse engineer an executable file to figure out what the code is
doing:
- Given assembly code that uses a recursive function to check some inputs, provide a sequence of inputs will pass that check.
Extra goals
- Explain how the C compiler uses assembly code to produce a “stack
frame” for each function call:
- Identify which registers are caller-saved vs. callee-saved just from looking at the pushes and pops in the assembly code.
Reflection
Fill out the CS 240 Lab 8 Reflection form to complete today’s lab reflection.