Convert from Java to MIPS

I have an exam in a days time, and I would really appreciate it if you guys can check an answer of mine. I have to convert a small piece of java code into a MIPS instruction, but no memos are available, and this is the first time I'm doing this sort of thing.

Here is the question :

While (save[i] != k) {
save[i] = v[i];
i=i+2;
} 

a) The code listed above is a high level Java program that assigns each second element of array v to the array save. Assuming that the assembler stores the base addresses of the arrays save and v respectively into registers $s2 and $s3, you are asked to convert the Java program above into an assembly language code. Note: You are free to use different registers for the variables which were not specified explicitly

And here's an attempt :

i = $t1

k = $t2

loop: sll $t3, $t1, 2 //get the offset (i*4) add $t4, $t3, $s2 //t4 is the address for save[i] beq $t4, $t2, exit //check the while condition add $t5, $t3, $s3 //t5 is the address for v[i] sw $t4, $t5 //save[i] = v[i] addi $t1, 2 //inc i j loop
exit:

Any help would be greatly appreciated.

EDIT: changed 'bne' to 'beq'

2

1 Answer

You're missing a couple of loads, and your store is incorrect:

sll $t3, $t1, 2 //get the offset (i*4)
add $t4, $t3, $s2 //t4 is the address for save[i]
lw $t5,($t4) //t5 = save[i]
beq $t5, $t2, exit //check the while condition
add $t5, $t3, $s3 //t5 is the address for v[i]
lw $t5,($t5) //t5 = v[i]
sw $t5, ($t4) //save[i] = v[i]
addi $t1, 2 //inc i
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like