0

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'

4

1 回答 1

2

您缺少几个负载,并且您的商店不正确:

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
于 2013-11-06T18:46:57.760 回答