# CS360 Lab #5 -- Assembler

This is not a lab that you hand in. Instead, its goal is to help you reinforce the
lecture notes on assembler, and to study for the exam. For that reason, I have included
the answers with the lab. It is completely fair game for you to consult the
TA's to help you with this material.
Additional study material:

- Question 1 from the 2011 Midterm Exam.
- Questions 3 and 4 from the 2004 Midterm Exam.
- Question 1 from the 2003 Midterm Exam. Maybe I should solicit money from the students
to prevent me from asking a question like this again...

Do not "optimize" the assembler. Give me the simple yet
inefficient assembler that the compiler would return.

## Question 1

Derive the assembler for the following procedure.
int b(int j, int k)
{
int i;
i = j;
while (i > 0) {
j = (i * j) + (k * 3);
i--;
}
return j;
}

The answer is here.

## Question 2

Derive the assembler for the following procedure.
You may assume that NULL is equal to zero.
int f2(int *x)
{
int j;
if (x == NULL) return 37;
j = *x * 2;
return j;
}

The answer is here.

## Question 3

Derive the assembler for the following program.
In terms of grading, this would be worth three times Question 1 or Question 2.
int c(int i, int j)
{
if (i <= 0) return j+1;
return a(j, i-3)+1;
}
int a(int i, int j)
{
if (j <= 0) return i+1;
return c(i-1, j)+c(i-1, j-1);
}
main(int argc, char **argv)
{
int i;
if (argc != 3) { exit(1); }
i = a(atoi(argv[1]), atoi(argv[2]));
}

The answer is here.

## Question 4

Below is a snapshot of the stack when the program in Question 3 is executing.
As in the "fact" example in
Assembler Lecture #3,
label each byte of the stack, and show where we are in the execution of the program (don't just label
the PC -- show where we are in the call stack).
You may assume that **main** starts at label 0x10a0,
**a** starts at label 0x1040,
and **c** starts at label 0x1000.
Here's the Open Office of the picture if you want:
**q3-unlabeled.odg**.
The answer is here.