Weeks Four and Five - MIPS
Contents
Let’s get low(er) level!
Stack Frames
When a stack frames is set up, the values of the specified registers are copied into the stack.
This allows the register values to be modified, as, at the end of the function, we can restore the original values that we started with.
Not all functions require a stack frame though. You only need to use one if:
- You are using the
$s?
registers
The safe registers are, by convention, registers that can only be modified by the function it is used in, and not any other function. - You are calling another function within your function
If you performjal func
inside your function, the return address to the main function would get lost
Upon finishing the MIPS part of COMP1521, here’s a few things that I’ve found amusing.
There are ten temporary registers
In MIPS, registers $8 - $15
are given the common name $t0 - $t7
.
BUT WAIT THERE’S TWO MORE, $24 - $25
which are $t8 - $t9
.
I guess that would have been good to know for the assignment
Labels point to a memory address
Let’s consider the label display
, which points to a memory space of 720 bytes…
.data
display:
.space 720
What if we wanted to store something into space 101?
.text
main:
li $t0, 100 # t0 = 100
la $t1, display # t1 = base address of `display`
add $t2, $t1, $t0 # t2 = base + 100
li $t9, 'A' # t9 = 'A'
sb $t9, ($t2) # *(base + 100) = 'A'
We could also do this
.text
main:
li $t0, 100 # t0 = 100
li $t9, 'A' # t9 = 'A'
sb $t9, base($t0) # *(100 + base) = 'A'
This way saved two instructions, but more importantly, it used one less register…
And that means more places to do more things - hooray!
Useful debugging
I’m not a fan of debugging software ~because I haven’t been bothered to learn how to use them~ so I often don’t use the useful features like breakpoints and step by step execution.
Instead, what I - and alot of people would do, is to just print out text at certain points.
In MIPS, you can’t print a string directly, rather you need to have created the string in the data segment of the code
.data
test: .ascii "foo bar"
...
.text
la $a0, test
li $v0, 4
syscall
If you wanted to print a single character
li $a0, 'X'
li $v0, 11
syscall
Or maybe an integer
li $a0, 999
li $v0, 1
syscall
