Addressing Modes & Control Flow
In today's exercise, we ask you to read some assembly language and reverse engineer the C constructs that produce them. You will find the code in cs61-exercises/asm2x.
Skills
- Mapping assembly language addressing modes to data structures
- Identifying patterns in assembly the correspond to C conditional statements and loops
- Using
gdb
to examine programs at the assembly level
Update your Cheat Sheet
If you didn't do so in class, you may want to take a minute to fill out most of the remaining part of your cheat sheet (we believe that this will assist you while you defuse your bomb). You can also finish filling it in later as you sit down to tackle the bomb.
Mapping some Derived Types to Assembly
You will find three files in the directory called 2a.S, 2b.S, 2c.S
.
Each of these is produced from a C file based on the template file
2tmplt.c
., which specifies a function that takes two parameters, the
first of which is a type that you're going to specify after reading the
corresponding assembly and the second of which is an int
. Your job
is to produce three C files, 2a.c, 2b.c, and 2c.c
that generate the
output in the corresponding assembly files.
Structs and Unions
OK, let's try a couple of more challenging cases.
Files 3a.S and 3b.S
contain the assembly produced when a function
prints out the fields of the structure passed as a parameter. Using the
file 3tmplt.c
as a starting point, create two new files
3a.c and 3b.c
that produce the assembly found in the corresponding
.S
files.
I bet you know what's coming next: unions!
File 4a.S
contains the assembly produced when a function prints out
the fields of the union passed as a parameter. Using the file 4tmplt.c
as a starting point, create a new file. 4a.c
, that produce the
assembly found in the .S
file.
Control Flow
Next, let's move on to reading assembly containing control flow instructions. We've created four files that show you the basic C constructs for control flow: if/then else statements, while loops, for loops, and switch statements. We encourage you to take a look at the assembly they produce before moving on to the mystery functions below.
Mystery Functions
Once again, we have four mysteries for your assembly reading pleasure. Each mystery function takes two arguments: a data structure and an (integer) element/key, and returns 1 if the element is in the data structure and 0 if it is not. Your job is to read the assembly and figure out what data structure is being accessed. Enjoy!
Survey
Please complete this short survey.
FAQ
Q1: I typed make
and I get a bunch of errors. What am I doing
wrong?
A1: There are template files that you should use to base your
answers on, but they won't compile. Build the .s
files explicitly,
e.g., make 2a.s
.
Q2: I typed make 2a.s
and it overwrote the 2a.S
file!
A2: You can get the original back by typing git checkout 2a.S
. If
this happens, create files named 2x.c, 2y.c, 2z.c, etc
.