【问题标题】:Why this MIPS loop stops printing strings while asking for integers为什么这个 MIPS 循环在请求整数时停止打印字符串
【发布时间】:2019-04-22 15:07:59
【问题描述】:

我正在尝试通过用户输入带有一些不错的字符串来输入一个 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: 

【问题讨论】:

  • 乍一看,您只为matrix分配了一个单个词,其为16。一旦填满它'将开始溢出到Introduce... 字符串中。对于 4x4 矩阵,您需要 16 个字:matrix: .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  • 耶稣!就是这样,由于字符串被溢出替换,它停止了打印。
  • matrix: .word 0:16 彻底解决问题
  • 更好!我不知道这种语法。

标签: assembly mips mars-simulator risc


【解决方案1】:

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

简单的解决方法:只需确保为matrix 分配足够的空间。

matrix: .word 0:16

【讨论】:

    猜你喜欢
    • 2015-01-10
    • 2021-06-15
    • 1970-01-01
    • 2016-08-22
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多