简体   繁体   中英

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM