Assignment 2: Part I: Preparation

This assignment is part I of a two-part assignment that requires you to demonstrate the ability to read x86 assembly language. In this assignment, you are given assembly language files and you are required to reconstruct the original C code that was used to generate the assembly file.

In part I, you will build the skills using some straightforward assembly language files. In part II, you will use those skills to recover a fairly sophisticated program from its assembly code.

Part I consists of a single assembly language file, a2_prep.s that was generated from a file a1_prep.c. You are given a skeleton of a1_prep.c and you must complete it.

Note that this assignment is structured like a puzzle, and as such, there is no step-wise, recipe-like way to solve it. Start early!

In particular: you can try out your hypothesis by examining the assembly language for your program and comparing it to supplied assembly language program [this is only guaranteed to work on CSUG machines].

Recovering local variables.

In a1_prep.c, start with the function fn1. Verify, by examining the assembly code, that this function does not accept any arguments.

Now, look at the assembly code and identify local variables -- in the assembly you've been given, all local variables are on the stack. First identify the number of local variables. Then identify the types/sizes of each variable. There are many ways to do this: recover the stack layout by examining the offsets, check the types of operands supplied to instructions, etc. All the methods should give consistent results.

Once you've recovered the number and type of variables, look for assignments/initializations.

Then, examine the rest of the code to identify expressions by locating the operations and variables that they are composed of.

Finally, figure out the return value and its type.

Recovering arguments and conditionals

The function fn2, unlike fn1, does accept arguments. The code uses the x64 ABI, so use that to figure out the number and types of arguments/parameters. fn2 also uses local variables and return values just like fn1. We will not discuss their recovery here, but you'll have to recover them nevertheless.

Now, recover the conditionals. Recall that assembly language conditionals usually consist of comparisons, unconditional jumps, and conditional jumps. Identify all instructions that perform comparisons and jumps, and use that to guide the reconstruction of the if in the original code.

Note that the autograder will not award points for a solution that contains goto.

Recovering loops

The function fn3 accepts arguments, has local variables, and produces a return value. Use the skills from recovering fn1 and fn2 to reconstruct those.

Additionally, fn3 contains a loop. Just like for conditionals in fn2, identify comparisons and jump instructions. Try to identify the instructions that form the body of loop by tracing the control flow. Recreate the loop. It is difficult to identify whether a loop is a for loop or a while loop for sure. Both are acceptable.

Again, the autograder will not award points for a solution that contains a goto or function calls.

Download

The assignment is available on Blackboard, it is attached to the announcement.