The following C codes can be translated to the MIPS ASM codes with some missing
ID: 3677156 • Letter: T
Question
The following C codes can be translated to the MIPS ASM codes with some missing instructions. Find the missing instructions and convert the MIPS ASM instructions with the (*) to the binary format.
C Code:
int proc=1;
int A[3]={1,-2,3}
for (i=3; i>0; i--)
{
proc=proc*A[i]
}
proc=proc << 2
MIPS Code:
.data
A: .word 1,-2,3
.text
main: li $s1,1
[Missing 1]
li $s3,3
loop:
[Missing 2]
lw $s4,0($s2) #(*)
mul $s1,$s1,$s4
[Missing 3] #(*)
addi $s3,$s3,-1 #(*)
j loop
AfterFor: sll $s1, $s1, 2 #(*)
jr $ra
Explanation / Answer
Ans:
.data
A: .word 1,-2,3
.text
main: li $s1,1
la $2 , A
li $s3,3
loop:
beqz $s3, AfterFor
lw $s4,0($s2) #(*$4=A[i])
mul $s1,$s1,$s4
addi $s2 , $s2, -4 #(*$s2=&(A[i--])
addi $s3,$s3,-1 #(*i--)
j loop
AfterFor:
sll $s1, $s1, 2 #(* shift left $s1)
jr $ra
Binary code:
Syntax:
addi $s2, $s2, -4
Encoding:
0010 00ss ssst tttt iiii iiii iiii iiii
Syntax:
bgez $s3, After For
Encoding:
0000 01ss sss0 0001 iiii iiii iiii iiii
Syntax: mul $s1,$s1,$s4
Encoding: 0000 00ss ssst tttt 0000 0000 0001 1000
Syntax:
j loop
Encoding:
0000 10ii iiii iiii iiii iiii iiii iiii
Syntax:
lw $s4,0($s2)
Encoding:
1000 11ss ssst tttt iiii iiii iiii iiii
Syntax:
sll $s1, $s1, 2
Encoding:
0000 00ss ssst tttt dddd dhhh hh00 0000
Syntax:
addi $s2, $s2, -4
Encoding:
0010 00ss ssst tttt iiii iiii iiii iiii