CS140 Lab 9 - Machine Level Representation of Programs, Part II

Objective

  • Experiment machine level representations of programs.
  • Use gcc, gdb, and objdump tools
  • Understand control logic at the machine level

Background and Perspective

Perspective:
Machine code instructions can be generally categorized three ways:

  • Load/Store data to/from registers and memory
  • Arithmetic/Logic operations on data
  • Control - Jumps or procedure calls that change the program counter
This lab focuses on understanding the 3rd item: control and procedure calls.

Background:
This material is from Chapter 3 in CS:APP textbook.

You will find the following reference slides useful: This lab picks up where Lab 08 left off. Please refer to the previous lab's instructions as needed.

Required Tasks

Experiment with Control Instructions


In this directory there are several C files containing very simple control structures. For each control structure, your job is to:
  1. Compile the C program into assembly (using the -S flag).
  2. Examine the resulting .s file to understand the resulting assembly code.
  3. Reference the slides linked above and/or the textbook for a relevant explanation.
  4. Write notes in your file explaining how the assembly code implements the control strucure.
The suggested order of experimentation is:
  1. if
  2. ifelse
  3. dowhile
  4. while
  5. forloop
  6. switch
  7. procedure
In the code, I used method calls to getc() and putc() to ensure the code wasn't optimzed out by the compiler. Feel free to edit the code so that it makes sense for you. Some additional questions to note for procedure: If you have time, you can also examine the object code. To do this:
  1. Compile the files into object code (.o files) using the -c compiler flag.
  2. Use objdump or gdb to disassemble the resulting code.
  3. Compare the disassmbled code to the original .s assembly and the original .c file
  4. Make notes about your observations.
  5. Advanced version: add a main method and compile/link an executable before disassembling. What do you notice is different than just disassembling the .o files?

Check out



When you are done with the lab exercises, turn in your notes on Gradescope. This is due at the end of the lab session today (1:15pm). Just turn in what you have done for lab credit even if you did not finish.