MIPS and SPIM
We are going to practice writing some MIPS assembly code by hand
and running it in SPIM.
On-line SPIM and MIPS references are available under the "On-line Resources"
section of the CS75 homepage:
Links to MIPS and SPIM references. We are going to start by looking at the "brief intro
to MIPS and SPIM" link.
In addition, two copies of the SPIM Reference Manual are in the CS labs, bound
with yellow covers. Please leave these in the main and/or overflow labs.
Create a new file called simple.mips into which we will write our first
MIPS program.
- All SPIM assembly files will start with the following:
.data
_newline_:
.asciiz "\n"
.text
.globl main
_newline_ is program data, that is the string "\n" that will used for
writeln statements, and the only global text symbol is main (main will
be a label that corresponds to the first instruction in main).
-
We are going to write SPIM code for some simple programs that consist
of some statements in a single function main. To do this we need to
implement main's prolog code to set up its stack frame next. We will
ignore saving callee saved registers for now and just
save the FP and the RA and adjust FP and SP for main's stack frame which
will look like this:
SP ---------> saved RA value # top of main's stack frame
saved FP value
FP ---------> # bottom of main's stack frame (the caller's SP)
- Now we are going to generate the body of the main function for the
following program:
int main() {
write 6;
}
write can be implemented using the SPIM system call print_int.
- finally, we are going to implement main's epilog code. In the
epilog, we need to restore the caller's stack frame using the current FP
value, and the saved FP value (if we had save callee saved registers to
the stack, we would also restore their values here). We then restore
the RA value that was saved in main's stack frame and generate a jr
instruction to jump to the value in the $ra register.
- okay, let's try running our program in xspim:
$ xspim simple.mips
- After we get this working, try some of the hw4 problems.