Jill P. answered 03/11/23
Computer Science Professional
.data
a: .word 0
Q: .space 60
ECE375: .space 60
.text
.globl main
main:
# Initialize i to 0
li $t0, 0
# Loop from 0 to 14 inclusive
for_loop:
slti $t1, $t0, 15
beq $t1, $zero, end_for
# Load value from Q[i] into $t2
la $t3, Q
addu $t3, $t3, $t0
lw $t2, ($t3)
# Add 2 to $t2 and store the result in ECE375[i]
addi $t2, $t2, 2
la $t3, ECE375
addu $t3, $t3, $t0
sw $t2, ($t3)
# Increment i by 1
addi $t0, $t0, 1
j for_loop
end_for:
# Call dofsum(7, 5, 3) and store the result in a
li $a0, 7
li $a1, 5
li $a2, 3
jal dofsum
sw $v0, a
# Subtract 4 from a and store the result in t
lw $t0, a
li $t1, 4
sub $t0, $t0, $t1
sw $t0, ($sp)
# Return t
jr $ra
# dofsum(q1, q2, q3)
# Returns the difference between the sums of q1+q2 and q3+4
dofsum:
# Save the return address and the current frame pointer
addi $sp, $sp, -8
sw $ra, 4($sp)
sw $fp, 0($sp)
addi $fp, $sp, 8
# Call sum(q1, q2) and store the result in psum1
add $a0, $a0, $a1
jal sum
move $s0, $v0
# Call sum(q3, 4) and store the result in psum2
li $a0, 4
add $a0, $a0, $a2
jal sum
move $s1, $v0
# Subtract psum2 from psum1 and return the result
sub $v0, $s0, $s1
# Restore the previous frame pointer and the return address
lw $ra, 4($fp)
lw $fp, ($fp)
addi $sp, $sp, 8
jr $ra
# sum(x, y)
# Returns the sum of x and y
sum:
add $v0, $a0, $a1
jr $ra