1

我正在尝试通过用户输入带有一些不错的字符串来输入一个 4x4 矩阵。有些东西使其中一个字符串在第三次迭代时停止打印,然后另一个字符串在第 7 次迭代时停止打印。

我认为这可能是 MARS 模拟器错误或其他问题,我试图重新启动它,但错误仍然存​​在。我确定它必须与我的代码有关,但我找不到错误。

产生错误的 .asm:

.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:    

产生的输出:

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) --

正如我们所看到的,ASCII 块意味着它打印了一些东西......错误(它们被 MARS 抑制并且只能看到将其粘贴到其他地方)(编辑:也被 stackoverflow 抑制......失败)

预期结果应该是 16 条消息,要求用户输入以下格式:

Introduce value [col|row] of the matrix: 
4

1 回答 1

1

matrix: .word 16表示值为16 的单个单词。一旦将第二个用户输入存储到,它将溢出到字符串中。对于您输入的小值,您将不可避免地在字符串的开头存储几个零,这将导致它提前停止打印该字符串。matrixIntroduce...

简单修复:只需确保为matrix.

matrix: .word 0:16
于 2019-04-22T15:20:14.877 回答