Question

I have this method written out in Java:

public static void getQuot(int[] r, int[] q) {

    int i = 0;
    while(r[i+1] != 0) {
        q[i+1] = r[i] / r[i+1];
        r[i+2] = r[i] % r[i+1];

        System.out.print(q[i+1] + ", ");
        i++;
    }
}

I have this so far in MIPS (as far as the method goes):

# get Quotients, store to q
li $t1,0 # i
getQuot:
add $t2,$t1,4 # i + 1
add $t3,$t1,8 # i + 2
la $a0,r
lw $t8,$t1($a0) # load t8&t9 for division
lw $t9,$t2($a0)
div $t8,$t9
mflo $t4
sw $t4,$t3($a0) # store quotient in r[i+2]
la $a0,q
mfhi $t4
sw $t4,$t2($a0) # store remainder in r[i+1]
addi $t1,$t1,1 # i++

I also have these list in .data if that's relevant:

.data
r:  .word 0:100
q:  .word 0:100

The problem I'm having is with doing something like lw $t8,$t1($a0), from the errors I'm getting I'm assuming I can't use a register to denote a location on the list? If so, how can I go about creating this loop properly?

Also, here's a table of the data, basically r holds remainders with two previously entered integers as the first two, and q holds quotients (offset by one). 45/17=2 r11 17/11=1 etc...

i r_i q_i
0 45
1 17  2
2 11  1
3 6   1
4 5   1
5 1   5
  0
Was it helpful?

Solution

Begin your loop by putting the address of r into register $a0. Then, 0($a0) represents r[i], 4($a0) represents r[i+1], and 8($a0) represents r[i+2]. Increment $a0 by 4 each loop. Similarly for q which we will put in $a1.

You don't need i/$t1, nor $t2 and $t3.

Roughly something like this (it's been a few years, and I don't have an assembler handy):

la $a0,r
la $a1,q
getQuot:
lw $t8,0($a0) # $t8 = r[i]
lw $t9,4($a0) # $t8 = r[i+1]
div $t8,$t9
mflo $t4
sw $t4,4($a1) # store quotient in q[i+1]
mfhi $t4
sw $t4,8($a0) # store remainder in r[i+2]
addi $a0,$a0,4 # next r
addi $a1,$a1,4 # next q

Note that your code did not include the loop condition, which I also omitted because my recollection is so rusty. :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top