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 14 .text # begin program text 15 .global main 16 main: 17 addi $t0, $0, 1 # $t0 will hold result, initially 1 18 lw $t1, N($gp) # $t1 will hold N, initially 9 19 20 top: beq $t1, $0, bottom 21 mult $t0, $t0, $t1 # result = result * N 22 addi $t1, $t1, -1 # decrement N 23 j top # goto top 24 25 bottom: 26 sw $t0, result($gp) # we'd better save result 27 addi $v0, $0, 4 # finished w/ loop, now print out 28 addi $a0, $gp, msg # message, by invoking syscall 4 29 syscall # (print_string) 30 31 addi $v0, $0, 1 # now print out result, by 32 lw $a0, result($gp) # invoking syscall 1 (print_int) 33 syscall 34 35 halt # all doneThankfully, you don't have to type the program in yourself, as you can download it here. Put it in directory
Z:\cebollita\apps\myPrograms
.
Make sure it's called fact.s
(not fact.s.txt
, as IE will happily try to name it).
fact.s
.
To do this, you edit file Makefile
(in directory
cebollita/apps/myPrograms
) once:
Look for the big comment near the top. It's saying you need to add
fact
to the end of the line just below the block of comments.
Do that.
In a cygwin shell, in the directoryZ:\cebollita\apps\myPrograms
, saymake for-standalone
make fact
.
cebsim fact.exe
.
make
is a program that saves you some typing by taking
lines from a file, called Makefile
, and essentially typing
them for you when you ask.
Step 1 above is a simple modification required so that make
knows what to type to build fact.s
into fact.exe
.
.exe
file, but that gets tedious when
you're debugging and have to build a large number of times.
In Step 2 above you're saying you want no prologue and no runtime
libary -- you're going to run standalone.
That information is put into a (hidden) file, named
.cebenv.mk
, in the current directory, and is taken
from there each time the program has to be linked.
fact.s
you could type the following by hand:
cebasm fact.s
ceblink --output fact.exe [prologue file] [runtime libary file] fact.o
make
is a program that types these things for you.
The command issued in step 3 above (after doing steps 1 and 2)
(a) lets make
figure out that it needs to issue those
commands, and (b) tells make
the names of the
prologue and runtime library files to use.
fact.exe
is loaded into memory, and the machine is waiting for you to do something.
.o
and .exe
files -- print them for you.
They're binary files, not characters, so you can't just print them in
the normal way.
Try them out:
cebdumpm fact.o
cebdumpe face.exe
cebsim
) and
so writes things out the hard way.
cebsim fact.exe
ought to do
it), and single-step through it.
Location | Line 18 | Line 22 3rd iteration |
Line 22 5th iteration |
Line 22 8th iteration |
Line 26 |
---|---|---|---|---|---|
$t0 | |||||
$t1 | |||||
result($gp) |
main() { int N = 9; int result = 1; while (N != 0) { result = result * N; N = N - 1; } printString("The factorial is: "); printInt(result); }(Technically, this is a little different because it doesn't do a Halt - it does something similar, though.)
Makefile
to add hw2
to that
famous line.
make for-os
to arrange
for the right ones to be used.
hw2.c
to produce
hw2.exe
: make hw2
.
hw2.exe
: cebsim hw2.exe
.
Hopefully, you'll get the same result as above.
Finally, modify the above C-- program in the same manner as you modified the assembly program (above). It should call the function
readInt
to get
an integer from the user. Below is an example C-- program that uses readInt:
main() { printString("Please enter a number: "); int x = readInt(); if (x < 0) { printString("Your number is negative.\n"); } else { printString("Your number is non-negative.\n"); } }Contrast the tradeoffs between programming in C-- and assembly code. How long did it take you to make the respective modifications?
Please turnin your modified assembly program, your modified C-- program, your Makefile, and a file with the answers to the questions above.