In: Computer Science
1. Translate the following code into MIPS
code.
B[i + 10] = B[i -2] + 40;
i = i + 10;
B[3] = B[i - 1];
a) Assume B is an array of integers (each integer takes 4
bytes). B's address
is stored at register $10. Also assume that the compiler associates
the
variable i to the register $11.
b) Assume B is an array of characters (each character takes one
byte). B's address
is stored at register $10. Also assume that the compiler associates
the variable i to the register $11.
2. Translate the following code into MIPS
code.
B [0] = 5;
for (i = 1 ; i < 50 ; i = i + 2)
{
B[i] =i + B[i-1];
}
Assume the compiler associates the variable i to the register
$t0. Also, assume B is an array of integers and its address is
stored at register $s1.
3. Translate the following code into MIPS
code.
for (i=0; i<=5; i=i+1)
{
if (i != k)
k=(k *2)-1;
else
k=(k *4)+1;
}
Assume the compiler associates the variables i and k to the registers $s0 and $s1, respectively.
4. Translate the following code into MIPS code.
Test (int i, int j)
{
int k;
k = Double(i+1) + Double (j-10)
return k;
}
Sub (int m)
{
int g;
g = m + m;
return g;
}
Assume the compiler associates the variable k to the register $s0. Assume the compiler associates the variable g to the register $t0.
Question 1
a) Translate
la $10,B
#a0 hold the value of i-2
addi $4,$11,-2
#That add to $10 to get B[i-2] index
add $10,$0,$4
#a0 hold the value in index B[i-2]
lw $4,($10)
#a0 hold value of B[i-2]+40
addi $4,$4,40
#$8 hold value of i+10
addi $8,$11,10
#Get the base address of the array in $10
la $10,B
#Increment address to get B[i+10]
add $10,$0,$8
#store value into that address,B[i+10]=B[i-2]+40
sw $4,($10)
#Increment i value into i=i+10
addi $11,$11,10
#$4 store the value i-1
add $4,$11,-1
#Get base address of B
la $10,B
#address offset , means B[i-1]
add $10,$0,$4
#B[i-1] value in $4
lw $4,($10)
#Get base address in $10
la $10,B
#Change into B[3]
addi $10,$0,12
#store B[i-1] into B[3], B[3]=B[i-1]
sw $4,($10)
b) Translate
#Base address of B in $10
la $10,B
#a0 hold the value of i-2
addi $4,$11,-2
#That add to $10 to get B[i-2] index
add $10,$0,$4
#a0 hold the value in index B[i-2]
lb $4,($10)
#a0 hold value of B[i-2]+40
addi $4,$4,40
#$8 hold value of i+10
addi $8,$11,10
#Get the base address of the array in $10
la $10,B
#Increment address to get B[i+10]
add $10,$0,$8
#store value into that address,B[i+10]=B[i-2]+40
sb $4,($10)
#Increment i value into i=i+10
addi $11,$11,10
#$4 store the value i-1
add $4,$11,-1
#Get base address of B
la $10,B
#address offset , means B[i-1]
add $10,$0,$4
#B[i-1] value in $4
lb $4,($10)
#Get base address in $10
la $10,B
#Change into B[3]
addi $10,$0,3
#store B[i-1] into B[3], B[3]=B[i-1]
sb $4,($10)
-----------------------------------------------------------------------------------
Question 2
#Get base address of B
la $s1,B
#$t1=5
addi $t1,$0,5
#B[0]=5
sw $t1,($s1)
#$t0=i=1
addi $t0,$0,1
#For loop
loop:
#i<50
blt $t0,50,exit
#i-1 in $t2
addi $t2,$t0,-1
la $s1,B
#Get address of B[i-1]
add $s1,$0,$t2
#Value of B[i-1]
lw $t2,($s1)
#1+B[i-1]
addi $t2,$0,1
la $s1,B
#Get B[i] address
add $s1,$0,$t0
#B[i]=1+B[i-1]
sw $t2,($s1)
#i=i+2
addi $t0,$t0,2
#Repeat
j loop
exit:
----------------------------------------------------------------------
Question 3
#i=0
addi $s0,$0,0
#For loop
loop:
#i<=5
ble $s0,5,exit
# if (i != k)
bne $s0,$s1,if
# k=(k *4)+1;
mul $s1,$s1,4
addi $s1,$s1,1
#i=i+1
addi $s0,$s0,1
#Repeat
j loop
#k=(k *2)-1
if:
#k*2
mul $s1,$s1,2
#k=(k *2)-1
addi $s1,$s1,-1
#i=i+1
addi $s0,$s0,1
#return to for loop
j loop
exit:
-------------------------------------------------------------------------
Question 4
Sub (int m)
{
int g;
g = m + m;
return g;
}
Translate
#m in a0
jal Sub
Sub:
#g = m + m;
add $t0,$a0,$a0
#return value in v0
move $v0,$t0
#Return g
jr $ra
------------------------------------------------------------------------
Note
Question 4's first part "Test" method, there is "Double()" is that means a new method or conversion.
I have doubt in that, so i skipped that.