Lab 5: Assembly

Lab sessions Mon Feb 13 to Thu Feb 16

Lab written by Julie Zelenski

Learning goals

This lab is designed to give you a chance to:

  1. use objdump and gdb to disassemble and trace assembly code
  2. study the relationship between source code and its assembly translation
  3. reverse-engineer a little assembly back to C

Find an open computer and somebody new to sit with. Share your favorite song or album from last year and/or favorite Grammys performance.

Lab exercises

1. Get started.

Clone the lab starter project using the command

hg clone /afs/ir/class/cs107/repos/lab5/shared lab5

Have our guide to x86-64 basics and this handy one page of x86-64 in your browser for reference during lab. Bring up the online lab checkoff up so you can jot down things as you go. At the end of the lab period, submit the sheet and have the TA check off your work.

2. Deadlisting with objdump

As part of the compilation process, the assembler takes in assembly instructions and encodes them into the binary form understood by the hardware. Disassembly is the reverse process that converts binary-encoded instructions back into human-readable assembly. You wrote a little disassembler in assign4. objdump is a tool that operates on object files (i.e. files containing compiled machine code). It can dig out all sorts of information from the object file, but one of the more common uses is as a disassembler. Let's try it out!

3. GDB commands for live assembly-level debugging

The debugger has great support for working with code at the assembly level. Load the trace program in gdb, use the gdb command start to get program going and stopped in main. From there, try out the gdb commands listed below that allow to poke around at the assembly-level. To learn more about any gdb command, try gdb's built-in help.

In the disassembly as printed by gdb, the hex number in the leftmost column is the address in memory for that instruction and in angle brackets is the offset of that instruction relative to the start of the function. You may notice minor differences in presentation between the disassembled instructions as printed by gdb versus the output from objdump, e.g. use of movq instead of mov, negative signed values may display as large unsigned, and so on.

4. Reading and tracing assembly in GDB

Read over the C code in trace.c. Compile the program and run in gdb. Use the gdb commands from the previous exercise to set breakpoints, disassemble, stepi through the assembly, print registers, and so on to answer the following questions.

In the my_variables function:

In the u_arith and s_arith functions:

In the for_loop, while_loop and dowhile_loop functions:

5. Exploring C compilation to assembly

A fun tool for investigating C to asm is the GCC Explorer, an online "interactive compiler". (Thanks, Josh K, for sharing!) Use the link https://godbolt.org/g/fHoZ7S configured to use the myth's version of GCC (4.8.x) and the compiler flags from the CS107 makefiles. You can enter some C code, tweak it a bit, and immediately observe how those changes are reflected in the assembly. The tool is doing the same tasks you could do on myth using gcc/gdb, but in a quick exploratory context. Here are a few experiments to try:

6. Reverse-engineering

The program babybomb asks for input and uses it to make a call to the function mystery in hopes of getting a successful return value. What kind of input is necessary to win at this game? Let's look into this mystery! Open the mystery.s file to view the assembly and then use gdb stepi through the execution of a call to mystery and observe its execution. Once you understand how it operates, give input to the program that will pass the test and win. There are multiple ways to win -- try to find at least two different ones. You're on your way to tackling binary bomb!

Check off with TA

Before you leave, be sure to submit your checkoff sheet and have your lab TA come by and confirm so you will be properly credited. If you don't finish everything before lab is over, we strongly encourage you to finish the remainder on your own. Double-check your progress with self check.

Contents