Lexie Anne
Lexie Anne

Reputation: 71

Printing from a declared array in MIPS

So I'm making something that looks like this

for(i=0; i<10; i++){
    print i;
}

but in MIPS, and i'm having trouble understanding how i'm supposed to increment the index location. Here is what I have so far

.data
    a: 
        .word 3, 2, 1, 8, 6, 9, 3, 4, 2, 5
    nline: 
        .asciiz "\n"

    .text
    .globl main
main:
    li $v0, 0

    loop1:
        bgt $t0, 10, exit
        addi $t0, $t0, 1 #counter

        lw $t1, a

        li  $v0, 1      
        move    $a0, $t1
        syscall
    exit:
        li  $v0, 10
        syscall

so I wouldn't want to put lw $t1, a inside the loop, but the idea is that with every iteration of the loop it would print 3, 4, 1, 8 and I know that would be a, a+4, a+8, a+12, ect, but I don't understand how to code that without there being a thousand functions.

Upvotes: 1

Views: 25257

Answers (1)

Bruno Alves
Bruno Alves

Reputation: 318

I think you are trying do this:

.data
    a: 
        .word 3, 2, 1, 8, 6, 9, 3, 4, 2, 5
    nline: 
        .asciiz "\n"

    .text
    .globl main
main:
    li      $v0, 0
    la      $t1, a
loop1:
    bge     $t0, 10, exit

    # load word from addrs and goes to the next addrs
    lw      $t2, 0($t1)
    addi    $t1, $t1, 4

    # syscall to print value
    li      $v0, 1      
    move    $a0, $t2
    syscall
    # optional - syscall number for printing character (space)
    li      $a0, 32
    li      $v0, 11  
    syscall


    #increment counter
    addi    $t0, $t0, 1
    j      loop1

 exit:
    li      $v0, 10
    syscall

Upvotes: 3

Related Questions