Nothing.
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 38 addi $a0, $gp, newline 39 syscall 40 41 halt # all doneThankfully, 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 calledfact.s
(notfact.s.txt
, as IE will happily try to name it).
- As well as the file
fact.s
, you're going to need some "prologue" code. We've writtenfact.s
to run "stand alone" -- without requiring any additional libraries (or even any real operating system). The prologue we need isZ:\cebollita\apps\prologues\prologue-standalone.o
. Copy that file to the directory that hasfact.s
in it.
- Now you need to assemble
fact.s
to produce the "object code" filefact.o
. You can do that with:or, if you've set up the standard aliases (#7 here):$ java asm.parser fact.s
The$ cebasm fact.s
.o
file contains a translation of the assembler instructions into machine code, plus some information needed to link the object code into an executable.
- To create an executable file,
fact
:Note: it's important to specify the two
$ java asm.Linker --output fact prologue-standalone.o fact.o
or $ ceblink --output fact prologue-standalone.o fact.o
.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).
Cebollita provides tools that will "dump".o
andfiles -- 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:
(For added excitement, try
$ java asm.Module fact.o
$ java util.Exe fact
or $ cebdumpm fact.o
$ cebdumpe fact
cebdumpe fact | less
.)
- 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.)
- Start the simulator:
$ java dbg.UI fact
or $ cebsim fact
- 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 22
3rd iterationLine 22
5th iterationLine 22
8th iterationLine 26 $t0 $t1 result($gp)
- 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).
- Did you answer 4 already? Turns out the simulator prints out some useful information in the console window when you're done simulating...
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.