简体   繁体   English

MIPS,处理数组和列表(将Java代码转换为MIPS)

[英]MIPS, Dealing With Arrays and Lists (Converting Java code to MIPS)

I have this method written out in Java: 我用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): 到目前为止我在MIPS中有这个(就方法而言):

# 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中列出这些列表:

.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? 我遇到的问题是做一些像lw $t8,$t1($a0)的错误,我得到的错误我假设我不能使用寄存器来表示列表中的位置? 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). 此外,这里是一个数据表,基本上r保持剩余部分,其中两个先前输入的整数作为前两个,并且q保持商(偏移一个)。 45/17=2 r11 17/11=1 etc... 45/17 = 2 r11 17/11 = 1等......

i r_i q_i
0 45
1 17  2
2 11  1
3 6   1
4 5   1
5 1   5
  0

Begin your loop by putting the address of r into register $a0 . 通过将r的地址放入寄存器$a0开始循环。 Then, 0($a0) represents r[i] , 4($a0) represents r[i+1] , and 8($a0) represents r[i+2] . 然后, 0($a0)表示r[i]4($a0)表示r[i+1] ,而8($a0)表示r[i+2] Increment $a0 by 4 each loop. 每个循环增加$a0乘以4。 Similarly for q which we will put in $a1 . 同样对于我们将放入$a1 q

You don't need i / $t1 , nor $t2 and $t3 . 你不需要i / $t1 ,也不需要$t2$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. 请注意,您的代码不包含循环条件,我也省略了它,因为我的回忆是如此生疏。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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