Mips how to read an array and print them?

okay, C ++ and java, I have no problems learning or that when it comes to mips, it seems like hell

Ok i want to learn to read in an array and print the whole element

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

+3
source share
1 answer

Assuming you have the address of your array in the 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 give the same result as your java code, except that you used println (which will print each element on a new line), and this code will print all elements of the array on one line.

, , Java 1s, 1 19, $t0, loop1

+2

Source: https://habr.com/ru/post/1746776/


All Articles