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
- Log into your Linux account on one of the CS machines.
- Create a directory for your lab09 experiments.
- Create a text or word document for your notes (turn-in DUE at the end of lab!)
- Keep detailed notes of each experiment!
In this directory there are several C files containing very simple control structures. For each control structure, your job is to:
- Compile the C program into assembly (using the -S flag).
- Examine the resulting .s file to understand the resulting assembly code.
- Reference the slides linked above and/or the textbook for a relevant explanation.
- Write notes in your file explaining how the assembly code implements the control strucure.
The suggested order of experimentation is:
- if
- ifelse
- dowhile
- while
- forloop
- switch
- 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:
- Where are the first SIX arguments stored?
- Where are arguments store after six?
- The following mnemonic may be useful: "Diane's Silk Dress Costs $89"
If you have time, you can also examine the object code. To do this:
- Compile the files into object code (.o files) using the -c compiler flag.
- Use objdump or gdb to disassemble the resulting code.
- Compare the disassmbled code to the original .s assembly and the original .c file
- Make notes about your observations.
- 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.