You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MIPS汇编程序编译正常但无数字输入入口问题咨询

问题:MIPS汇编程序未触发输入交互直接打印矩阵

我编写了一段MIPS汇编程序,该程序可正常编译,但运行后未出现任何可供输入数字的交互区域。程序代码中已定义提示输入行数(msg2)和列数(msg3)的字符串,但当前执行流程未触发输入相关逻辑,直接执行了矩阵打印操作。完整汇编代码如下:

.data
msg1: .asciiz "array printing "
msg2: .asciiz "enter the number of rows: "
msg3: .asciiz "enter the number of cols: "
msg4: .asciiz "the value at the location is: "
EOL: .byte '\n'
SPACES: .asciiz " "
MATRIX: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25
.text
.globl main
main:
    li $v0,4
    la $a0,msg1
    syscall
    addiu $sp,$sp,-4
    sw $ra,0($sp)
    jal PRTMTX
    lw $ra,0($sp)
    addiu $sp,$sp,4
    li $v0, 10
    syscall
PRTMTX:
    la $s0, MATRIX
    li $s1,1
    li $s2,1
    li $s6,5
TOPPRT:
    addiu $t1,$s1,-1
    addiu $t2,$s2,-1
    li $t4,20
    li $t5,4
    mul $t1,$t1,$t4
    mul $t2,$t2,$t5
    add $s4,$s0,$t1
    add $s4,$s4,$t2
    li $v0,1
    lw $a0,0($s4)
    syscall
    li $v0,4
    la $a0,SPACES
    syscall
    li $t6,5
    div $s2,$t6
    mfhi $t6
    beqz $t6,ENDROW
    addiu $s2,$s2,1
    j TOPPRT
ENDROW:
    li $s2,1
    li $v0, 11
    lb $a0,EOL
    syscall
    li $t6,5
    div $s1,$t6
    mfhi $t6
    beqz $t6,DONEPRT
    addiu $s1,$s1,1
    j TOPPRT
DONEPRT:
    jr $ra
PRTNUM:
    move $t1,$a0
    move $t2,$a1
    addiu $t1,$t1,-1
    addiu $t2,$t2,-1
    li $t3,20
    mul $t1,$t1,$t3
    li $t3,4
    mul $t2,$t2,$t3
    la $s0,MATRIX
    add $s0,$s0,$t1
    add $s0,$s0,$t2
    li $v0,4
    la $a0,msg4
    syscall
    lw $a0,0($s0)
    li $v0,1
    syscall
EXIT:
    li $v0, 10
    syscall

问题原因分析

  • 输入逻辑完全未执行:你只在.data段定义了msg2和msg3这两个输入提示字符串,但整个程序的执行流程里,既没有调用系统输出这些提示,也没有触发读取用户输入的操作。看main函数,它打印完msg1后直接跳去执行PRTMTX打印矩阵,完全跳过了输入环节——相当于你备好了提示语,但根本没让程序把它展示给用户,更没让程序停下来等输入。

  • 矩阵尺寸被硬编码:在PRTMTX函数里,你用li $s6,5、li $t6,5把行数和列数都固定成了5,程序完全不需要从用户那里获取尺寸信息,自然不会触发输入交互。就算之后补上输入逻辑,你也得把这些硬编码的数值替换成用户输入的变量,否则输入的行数、列数根本不会影响矩阵打印的行为。

  • 无关函数未起作用:代码里的PRTNUM函数只是用来打印指定位置的矩阵值,和输入没有任何关系,而且这个函数在整个程序里也没有被调用过,对你的输入需求毫无帮助。

内容的提问来源于stack exchange,提问作者Marken Chris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 21:32:30