Search code examples
assemblymipsmachine-code

How to print and attribute integers into an array in MIPS?


I'm trying to just print these 3 integers, but I get the error "-- program is finished running (dropped off bottom) --". When I compare my code to others that work, I can't see exactly what I'm doing wrong. This looks ok to me:

.data
    a: .word 3, 2, 1
main:   
    li $s0, 0 #i = 0
    li $s1, 3 #iterations = 3
    la $s2, a #s2 = adress of arr[0]
loop:
    beq $s0, $s1, end #if i == 3, ends
    lw $t0, 0($s2) #loads value of a[0] into t0
    addi $s2, $s2, 4 #goes to next element of array
    addi $s0, $s0, 1 #i++
    
    
    #some printing function I found online
    li $v0, 4
    lw $a0, 0($t0)
    syscall
    
    j loop
    
end:    

Solution

    • You are dereferencing the pointer to the array twice, which does not make sense in this context, since one dereference is sufficient to get an integer value from an integer array.  If you had an array of pointers, then a second dereference might be indicated.  Not sure what simulator you're using or its configuration, but it should have given a memory access error on the 2nd dereference, since a small integer is not a valid pointer.

    • A program is supposed to tell the operating environment or simulator to quit when it's done — yours doesn't do that.