Write MIPS codes to compute the following sums:
(a) 1^2 - 2^3 + 3^2 -4^3 (b) 5^3 -6^2 + 7^3 - 8^2
solution:
given data:
MIPS codes:
A)
.text
li $t0,1
li $t1,2
jal getPower
move $s0,$t2 #get 1^2
li $t0,2
li $t1,3
jal getPower
move $s1,$t2 #get 2^3
li $t0,3
li $t1,2
jal getPower
move $s2,$t2 #get 3^2
li $t0,4
li $t1,3
jal getPower
move $s3,$t2 #get 4^3
sub $t7,$s0,$s1
add $t7,$t7,$s2
sub $t7,$t7,$s3
li $v0,1
move $a0,$t7
syscall
li $v0,10
syscall
getPower: # return power $t0 ^ $t1 and return in $t2
li $t2,1
loop :
mul $t2,$t2,$t0 #loop for the value of s2
subi $t1,$t1,1
bgtz $t1,loop
jr $ra
B)
.text
li $t0,5
li $t1,3
jal getPower
move $s0,$t2 #get 5^3
li $t0,6
li $t1,2
jal getPower
move $s1,$t2 #get 6^2
li $t0,7
li $t1,3
jal getPower
move $s2,$t2 #get 7^3
li $t0,8
li $t1,2
jal getPower
move $s3,$t2 #get 8^2
sub $t7,$s0,$s1
add $t7,$t7,$s2
sub $t7,$t7,$s3
li $v0,1
move $a0,$t7
syscall
li $v0,10
syscall
getPower: # return power $t0 ^ $t1 and return in $t2
li $t2,1
loop :
mul $t2,$t2,$t0 #loop for the value of s2
subi $t1,$t1,1
bgtz $t1,loop
jr $ra
please give me thumb up
Get Answers For Free
Most questions answered within 1 hours.