assemblymipsmars-simulatorrisc

Why this MIPS loop stops printing strings while asking for integers


I'm trying to enter a 4x4 matrix by user input with some nice strings. Something makes one of the strings stop printing at the third iteration, and then the other string stops printing at iteration number 7.

I thought it could be a MARS emulator bug or something, i tried to restart it but the error persists. I'm sure it has to be with my code but I can't find the bug.

The .asm that produces the error:

.data
matrix: .word 16
msg1: .asciiz "Introduce value ["
msg2: .asciiz "] of the matrix: "

.text
li $t0, 0
la $s0, matrix
cols:
    beq $t0, 4, endLoop
    addi $t0, $t0, 1
    li $t1, 0
    rows:
        beq $t1, 4, cols
        addi $t1, $t1, 1

        li $v0, 4
        la $a0, msg1
        syscall

        li $v0, 1
        move $a0, $t0
        syscall

        li $v0, 11
        li $a0, '|'
        syscall

        li $v0, 1
        move $a0, $t1
        syscall

        li $v0, 4
        la $a0, msg2
        syscall

        li $v0, 5
        syscall

        sw $v0, 0($s0)
        addi $s0, $s0, 4

        j rows

endLoop:    

The output produced:

Introduce value [1|1] of the matrix: 1
Introduce value [1|2] of the matrix: 2
1|3] of the matrix: 3
1|4] of the matrix: 4
2|1] of the matrix: 5
2|2] of the matrix: 6
2|37
2|48
3|19
3|210
3|311
3|412
4|113
4|214
4|315
4|416

-- program is finished running (dropped off bottom) --

As we can see, the ASCII block means it prints something... wrong (they are supressed by MARS and can only be seen pasting it somewhere else) (Edit: also supressed by stackoverflow... fail)

The expected result should be 16 messages asking for the user input with this format:

Introduce value [col|row] of the matrix: 

Solution

  • matrix: .word 16 means a single word with a value of 16. Once the second user input is stored to matrix, it'll have overflowed into the Introduce... string. For the small values you're inputting, you'll inevitably be storing a few zeroes at the start of the string, which will cause it to stop printing that string early.

    Easy fix: just make sure you allocate enough space for matrix.

    matrix: .word 0:16