For the following MIPS programming exercise, you may use only these instructions
ID: 3884665 • Letter: F
Question
For the following MIPS programming exercise, you may use only these instructions or macros:
add addi addiu addu and andi div divu li lui lw mfhi mflo mult multu nor or ori sll sra srl sub subu sw xor xori
Evaluate the expression:
18xy + 12x - 6y + 12
Use symbolic addresses for x, y, and the answer. Assume that the values are small enough so that all results fit into 32 bits. Since load delays are turned on in SPIM be careful what instructions are placed in the delay slots.
Verify that the program works by using several initial values for x and y. Use x=0, y=1 and x=1, y=0 to start since this will make debugging easy. Then try some other values.
Explanation / Answer
.text
.globl main
main:
lui $t0,0x1000 # Init base register
lw $t1,0($t0) # Load x
lui $t2,0x2000 # Init base register
lw $t3,0($t2) # Load y
ori $t4,$zero,12 # Initialize the accumulator
# during the "load delay slot"
ori $t4,$zero,12 # Evaluate second term
mult $t1,$t2 # xy
mflo $t6 # assume 32 bit result
mult $t1,$t4 # 12x
mflo $t1 # assume 32 bit
mult $t2,$t5 # 6y
subu $t4,$t4,$t5 # accumulator = -6y + 12
ori $t7,$zero,18 # Evaluate third term
mult $t7,$t6 # 18xy
mflo $t6 # assume 32 bit result
addu $t1, $t1, $t4 # Acumulator = 12x - 6y + 12
addu $t6, $t6, $t1 # Acumulator = 18xy + 12x - 6y + 12
sw $t6,8($t0) # Store result in res variable
.data
x: .word 0 # Edit this line to change the value of x
y: .word 1 # Edit this line to change the value of y
res: .word 0 # Result is placed here.