main:
ori $1, $0, 456
addi $1, $1, -229
addi $1, $1, 325
addi $1, $1, -552
All this does is perform the calculation 456 - 229 + 325 - 552. At the start, ori is used to perform an or operation between $0 (which contains 0 always) and 456. The result of doing this is that it always stored the value you provide in the register specified. I could have also just added 456 to register $0 but that's boring :)
This is an extract from the third exercise of chapter 13.
main:
ori $9, $0, 0x7000
sll $9, $9, 16
addu $9, $9, $9
This stores the value 0x7000 (28672) in register $9, then shifts the bits 16 places to the left. This has the same function as multiplying it by 2^16. It then finally adds itself to itself. The interesting thing here is that the add performed is an unsigned addition. This means that if it overflow, it doesn't trap the program. In this case, using add would cause an overflow since the value being stored, as a two's compliment, would be negative.
The rest of exercise 13 is pretty self explanatory too. The more interesting stuff was to be found in exercise 15. Here's what I wrote for exercise 1.
main:
#Program to calculate 3x^2 + 5x - 12
#6 : Store coefficients
#7 : Memory address of x
#8 : x
#10: Final result
#11: Accumulator
#Set the position of x in memory
lui $7, 0x1001
#Load x and poly
lw $8, 0($7)
#Square x and store result in $10
multu $8, $8
mflo $10
sll $0, $0, 0
#Multiply result by 3
ori $6, $0, 3
multu $10, $6
mflo $10
sll $0, $0, 0
#Store 5x in $11
ori $6, $0, 5
multu $8, $6
mflo $11
#Subtract $11 from $10
sub $10, $10, $11
#Subtract a further 12
addi $10, $10, -12
#Store result as poly
sw $10, 4($7)
.data
x: .word 7
poly: .word 0
I decided to make the effort to comment this one since it's a bit more complex and a little confusing looking at it all at once. The interesting thing here is the usage of memory. At the bottom I've defined a variable called x and a variable called poly. Both of these are words (4bytes) in memory. MARS (the program I'm using to emulate MIPS) uses 0x10010000 as its memory base. So basically, x is stored at 0x10010000 and poly is stored at 0x10010004, since it is 4 bytes further in, and memory addresses refer to specific bytes. The lui command is then used to store a memory address (0x1001) in a register. Clearly it is impossible to fit a 32bit address into a 32bit instruction which also has the instruction itself. Therefore, only 0x1001 is stored, and the rest is accessed with offsets. That can be see in the load word function.
lw $8, 0($7) #Load x
lw $8, 4($7) #Load poly
This loads a word of data from the memory address in register $7 into register $8. The offset is stored in the value before the bracket. For x, there is obviously no offset needed, but poly is 4 bytes in so an offset of 4 is provided.
Finally, this is my code for exercise 4 which is the most interesting off all, enabling you to calculate a polynomial in the form ax^3 + bx^2 + cx + d by entering values into memory.
#Program to calculate ax^3 - bx^2 + cx + 2
#6 : Coefficients
#7 : Memory address of x
#8 : x
#10: Final result
#11: Accumulator
#13: a
#14: b
#15: c
#16: d
#Set the position of x in memory
lui $7, 0x1001
#Load x, a, b, c, d
lw $8, 0($7) #x
lw $13, 8($7) #a
lw $14, 12($7) #b
lw $15, 0x10($7) #c
lw $16, 0x14($7) #d
#ax
mult $13, $8
mflo $10
#ax + b
addu $10, $10, $14
#ax^2 + bx
mult $10, $8
mflo $10
#ax^2 + bx + c
addu $10, $10, $15
#ax^3 + bx^2 + cx
multu $10, $8
mflo $10
#ax^3 + bx^2 + cx + d
addu $10, $10, $16
#Store result as answer
sw $10, 4($7)
.data
x: .word 10
answer: .word 0
a: .word 4
b2: .word 3
c: .word 2
d: .word 1
The code is nothing particularly different from the code shown so far. One thing to note though, is that b cannot be used as a label for the memory as it is a command. So I just used b2.