开发者

Mips, how to read array and print them?

okay, C++ and java i have no problem learning or what so ever when it comes to mips it is like hell

okay i wanna learn how to read in the an array and print all the element out

here is a simple array that i wrote

int[] a = new int[20];

for(int i=0; i<开发者_开发知识库;a.length; i++){
  a[i]=1;
}

for(int j=0; j<a.length; j++){
  System.out.Println(a[i])
}

how do you do it in mips


Assuming that you have your array address at register $a1, you can do the following:

    li $t0, 1
    move $t1, $a1
    addi $t2, $a1, 80
loop1:
    sw $t0, ($t1)
    addi $t1, $t1, 4
    bne $t1, $t2, loop1

move $t1, $a1

loop2:
    lw $t0, ($t1)
    li $v0, 1
    move $a0, $t0
    syscall
    addi $t1, $t1, 4
    bne $t1, $t2, loop2

This code should produce the same result as your java code, except that you used println (which will print each element in a new line) and this code will print all the elements of the array in the same line.

I don't know if you have noticed, but your Java code and this code will print all 1s, if you want to print numbers from 1 to 19, you will have to increment $t0, inside loop1

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜