Tutorial Week 12 (Solutions)
Task 1. The key to solving this task is to realise that MIPS commands are just numbers and can be loaded and stored just like other numbers. With this in mind, the following program solves the exercise:
.data
.text
.globl main
main:
# First we write the add instruction in place over the first nop
li $a1, 0x20840001 # this is the opcode for: addi $a0, $a0, 1
sw $a1, op1
# Then we write the return instruction in place over the second nop
li $a1, 0x03e00008 # this is the opcode for: jr $ra0
sw $a1, op2
# Print the initial value of $a0
li $v0, 1
syscall
# Run the generated code
jal op1
# Print the final value of $a0
li $v0, 1
syscall
# Exit the program
li $v0, 10
syscall
# Here we create addressed free space in the program’s text segment by
# including nop commands, each under a label. nop commands take up one
# word of memory each, but do nothing.
op1:
nop
op2:
nop
Task 2. In this exercise you need to understand how objects are represented in memory. Below is a sketched solution. I hope I didn’t make any mistakes in the drawing. Red means unreachable, and will be GCed.