# CS360 Lab #5 -- Assembler

### James S. Plank CS360

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.

• 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;
}
```

## 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;
}
```

## 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]));
}
```