CS 240 Lab 8

Learning Goals & Reflection

CS 240 Lab 8

Learning Goals

Core Goals

Students can:

  1. Identify and explain core x86 assembly instructions:
    1. Look up the reference for an unfamiliar assembly instruction and understand what it is saying.
    2. Explain what the lea instruction does.
    3. Explain what the push and pop instructions do.
  2. Explain the usage of key x86 registers.
    1. Explain what the %rsp register is used for in x86 assembly code.
    2. Explain the usual role of the %rbp register.
    3. Explain the difference between caller-saved and callee-saved registers.
    4. Look up which registers are caller-saved vs. callee-saved.
  3. Use gdb to examine information about the stack:
    1. Use the backtrace or bt command in gdb to display information about currently active function calls.
    2. Use the disas command to “look up” where a return address goes.
    3. Look up format arguments for the examine or x command.
    4. Use x with $rsp to display specific values that a function has saved on the stack, including its return address.
    5. Use x with $rsp to display a function’s entire stack frame and/or to display multiple stack frames.
    6. Draw a memory diagram showing the boundaries of stack frames, return addresses, and key saved values, based on output from gdb’s x and disas commands.
  4. Explain how the C compiler uses assembly code to produce a “stack frame” for each function call:
    1. Identify which instructions are used for saving and restoring both caller-saved and callee-saved registers.
    2. Explain the purpose of subtracting from or adding to %rsp.
    3. Based on a function’s assembly code and a memory dump of the stack, identify which instructions & memory addresses are used to:
      1. Store callee-saved registers.
      2. Store caller-saved registers.
      3. Store local variables of the function.
      4. Align the stack pointer before a function call.
      5. Restore the stack pointer and caller-saved registers before returning from the function.
      6. Restore callee-saved registers after returning from a function call.
  5. Explain how functions use the stack to communicate with other functions:
    1. Explain why a function can pass a pointer to a local variable into a function it calls, but cannot return such a pointer.
    2. Explain how function call frames operate when dealing with a recursive function.
    3. Explain what will happen if a function call returns a pointer to memory allocated on its stack.
  6. Reverse engineer an executable file to figure out what the code is doing.
    1. 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

  1. Use gdb to examine information about the stack:
    1. Guess which values in a memory display are return addresses based on their hexadecimal values.
  2. Explain the advantages and properties of a stack-based compiler design:
    1. Explain what limitations a computer that stores the return address in a dedicated register instead of in memory would have.
    2. 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.
    3. 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.
  3. Reverse engineer an executable file to figure out what the code is doing:
    1. Given assembly code that uses a recursive function to check some inputs, provide a sequence of inputs will pass that check.

Extra goals

  1. Explain how the C compiler uses assembly code to produce a “stack frame” for each function call:
    1. 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.