Hey Im wondering how to convert JAVA into MIPS assembly code? Here are some exam
ID: 3623963 • Letter: H
Question
Hey Im wondering how to convert JAVA into MIPS assembly code?Here are some examples: please answer one, any or all (all would be VERY, VERY much appreciated!!)
n = 10;
sum = 100;
for (i=0; i<n; i++)
if (isRem4(i)== 0) sum -= i;
int isRem4(int i)
return (i%4);
Assume sum is in $s1, i is in $t0, n is in $t1;
and...
This question deals with matrix multiplication:
for (i=0; i<I; i++) {
for (j=0; j<J; j++) {
for (k=0; k<K; k++) {
c[i][j] = c[i][j] + a[i][k]*b[k][j];
}
}
}
Lastly...
Assume that the address of a[0] and b[0] are in registers $s0 and $s1. i is in $s2 and j is in $s3. Translate the following into MIPS assembly code:
j=0;
for (i=10;i>1;i-=2) {
a[i-2]=b[j]+a[i-1]+a[i]-1;
j++;
}
THANK you so much, A+ rating will be given definitely
Explanation / Answer
There is no direct way to convert java into mips, but you write code equivalent to it. For instance, you have n = 10. What you do is assign the variable (n) to a register, say $t1. To set it to an integer, you would do Add Immediate (addi): addi $t1, $t1, 10. This adds 10 to t1 (we assume that $t1 is 0 already). For loops are a bit tricker. You name the loop (let's say ForOne) and then have it call itself when it reaches the end. Set i to a variable (listed as $t0 in your assumption part) and increment it each time. Using the slt MIPS call, you would set a temporary register (let's use $t3) to 1 if $t0 is less than $t1. If not, it is 0. The final part (that calls the loop or exits) is bne which branches if $t3 does not equal 0 (meaning i is still less than n). jal means jump and link, where it stores the return address in $31. To return, it calls jr $31. beq is branch if equal to, mfhi moves the upper 16 bits to the register (the modulus of the division is stored there). For the first example, you have: addi $s1, $s1, 100 addi $t1, $t1, 10 ForOne: addi $t0, $t0, 1 jal isRem4 beq $t4, $0, isEqual slt $t3, $t0, $t1 bne $t3, $0, ForOne isRem4: add $t4, $t0, $zero addi $t5, $zero, 4 div $t4, $t5 mfhi $t4 jr $31 isEqual: sub $s1, $s1, $t0 jr $31 Hopefully this points you in the right direction.