## CSE 378 Homework 1 - Part 2

### Cebollita Exercises

Nothing.

#### Introduction

We begin with some assembler programming, using the following program that computes the factorial of 9. The line numbers you see are for reference only, and are not part of the actual assembly program.
``` 1  # compute the factorial of 9 using the following algorithm
2  #   N = 9
3  #   result = 1
4  #   while (N != 0) {
5  #     result = result * N
6  #     N = N - 1
7  #   }
8
9  .data                                   # begin data section
10  N:      .word    9                      # reserve and initialize a word
11  result: .space   4                      # reserve but don't initalize a word
12  msg:    .asciiz  "The factorial is: "   # message string
13  newline: .asciiz "\n"
14
15  .text                                   # begin program text
16      .global main
17  main:
18          addi    \$t0, \$0, 1              # \$t0 will hold result, initially 1
19          lw      \$t1, N(\$gp)             # \$t1 will hold N, initially 9
20
21  top:    beq    \$t1, \$0, bottom
22          mult    \$t0, \$t0, \$t1           # result = result * N
23          addi    \$t1, \$t1, -1            # decrement N
24          j       top                     # goto top
25
26  bottom:
27          sw      \$t0, result(\$gp)        # we'd better save result
28
29          addi    \$v0, \$0, 4              # finished w/ loop, now print out
30          addi    \$a0, \$gp, msg           # message, by invoking syscall 4
31          syscall                         # (print_string)
32
33          addi    \$v0, \$0, 1              # now print out result, by
34          lw      \$a0, result(\$gp)        # invoking syscall 1 (print_int)
35          syscall
36
37          addi    \$v0, \$0, 4              # print a newline
39          syscall
40
41          halt                            # all done
```
Thankfully, you don't have to type the program in yourself, as you can download it here. Put it in some directory, e.g., `Z:\cse378\hw1\`. Make sure it's called `fact.s` (not `fact.s.txt`, as IE will happily try to name it).

1. As well as the file `fact.s`, you're going to need some "prologue" code. We've written `fact.s` to run "stand alone" -- without requiring any additional libraries (or even any real operating system). The prologue we need is `Z:\cebollita\apps\prologues\prologue-standalone.o`. Copy that file to the directory that has `fact.s` in it.

2. Now you need to assemble `fact.s` to produce the "object code" file `fact.o`. You can do that with:
`\$ java asm.parser fact.s`
or, if you've set up the standard aliases (#7 here):
`\$ cebasm fact.s`
The `.o` file contains a translation of the assembler instructions into machine code, plus some information needed to link the object code into an executable.

3. To create an executable file, `fact`:
 `\$ java asm.Linker --output fact prologue-standalone.o fact.o` or `\$ ceblink --output fact prologue-standalone.o fact.o`
Note: it's important to specify the two `.o` files in the order shown.

Each time you change `fact.s` you'll need to re-assemble and re-link (steps 2-3 above).

#### Look at the .o and Files

Cebollita provides tools that will "dump" `.o` and files -- print them on the screen for you. They're binary files, not characters, so you can't just print them in the normal way.

Try them out:

 `\$ java asm.Module fact.o` `\$ java util.Exe fact` or `\$ cebdumpm fact.o` `\$ cebdumpe fact`
(For added excitement, try `cebdumpe fact | less`.)

#### Running Your Program on the Cebollita Simulator

1. You'll first want to read over the document The Cebollita ISA Simulator/Debugger. (Note that that document doesn't know about the aliases you established (like `cebsim`) and so writes things out the hard way.)

2. Start the simulator:
 `\$ java dbg.UI fact` or `\$ cebsim fact`
3. Complete the following table by examining various registers and memory location contents after the appropriate instructions are executed. Setting breakpoints (right-click on an instruction...) may help in certain situations.

 Location Line 18 Line 223rd iteration Line 225th iteration Line 228th iteration Line 26 \$t0 \$t1 result(\$gp)

4. In total, how many instructions (in the simulator) are executed to calculate the factorial of 9? The right way to answer this question is not to step the program through 9 loops and count instructions, but rather determine how many instructions are exectuted before the loop, during one iteration of the loop, and after the loop. Then you can give us an answer in terms of N, where N is the value we're calculating the factorial of (as well as the number of times the loop iterates).
```

```

5. Did you answer 4 already? Turns out the simulator prints out some useful information in the console window when you're done simulating...

#### Assembly Programming: Modifications

Modify `fact.s` so that it reads a value for N from the user, and then computes its factorial. Do this by reading the simulator user's manual, as well as Appendix A in the text, to figure out how to make the appropriate syscall. Next, modify the program to continue prompting the user for values of N and computing their factorials. Exit the program when the user enters a negative number.