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

求助:qtSpim环境下MIPS程序比较逻辑未执行问题排查

MIPS程序比较逻辑跳过问题排查求助

我正在为课程编写一款在qtSpim中模拟运行的MIPS程序,需实现以下功能:读取用户输入的两个整数,判断每个整数的奇偶性,比较两数大小,再执行用户指定的运算。目前除比较逻辑外,其他功能均正常运行——程序会直接跳过比较环节,既不崩溃也无其他异常表现。我尝试过调整代码结构、在读取第二个整数后显式跳转至比较逻辑,但均未解决问题。附上程序代码,恳请汇编技术能力较强的人士协助排查问题所在:

.text
Main:
# Initialize registers
li $s5, 0
li $s6, 2

Int1:
# Prompt for the integer to enter
li $v0, 4
la $a0, prompt1
syscall

# Read the integer and save it in $s0
li $v0, 5
syscall
move $s0, $v0
addi $s3, $s0, 0
jal CheckEven

Int2:
# Prompt for another integer to enter
li $v0, 4
la $a0, prompt2
syscall

# Read the second integer and save it in $s1
li $v0, 5
syscall
move $s1, $v0
addi $s3, $s1, 0
jal CheckEven
nop

# Comparison logic
Compare:
#print first integer
li $v0, 1
move $a0, $s0
syscall

beq $s0, $s1, Equal
slt $s3, $s0, $s1
bne $s3, $zero, Less

Greater:
li $v0, 4
la $a0, gt
syscall
j FinishCompare

Less:
li $v0, 4
la $a0, lt
syscall
j FinishCompare

Equal:
li $v0, 4
la $a0, et
syscall

FinishCompare:
li $v0, 1
move $a0, $s1
syscall

# Prompt for operator
Operator:
li $v0, 4
la $a0, prompt3
syscall

# Read the operator and save it in $s2
li $v0, 12
syscall
addi $s2, $zero, 0
move $s2, $v0

# Branch to addition
li $t0, '+'
beq $s2, $t0, Add

# Branch to subtraction
li $t0, '-'
beq $s2, $t0, Subtract

# Branch to multiplication
li $t0, '*'
beq $s2, $t0, Multiply

# Branch to division
li $t0, '/'
beq $s2, $t0, Divide

# Handle unexpected character
li $v0, 4
la $a0, error
syscall
j Operator

Add:
add $s4, $s0, $s1
j Print
nop

Subtract:
sub $s4, $s0, $s1
j Print
nop

Multiply:
mul $s4, $s0, $s1
j Print
nop

Divide:
div $s4, $s0, $s1
mflo $s4
mfhi $s5
j Print
nop

Print:

# Line break
li $v0, 4
la $a0, br
syscall

# Print 1st value
li $v0, 1
move $a0, $s0
syscall

# Print operator
li $v0, 11
move $a0, $s2
syscall

# Print 2nd value
li $v0, 1
move $a0, $s1
syscall

# Print equals sign
li $v0, 4
la $a0, e
syscall

# Print result
li $v0, 1
move $a0, $s4
syscall

#branch for division
beq $s5, $zero, Exit
li $v0, 4
la $a0, remainder
syscall

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

#Check whether user-supplied integers are even
CheckEven:
div $s3, $s3, $s6
mfhi $s3
beq $s3, 0, PrintEven
li $v0, 4
la $a0, odd
syscall
addi $s3, $zero, 0
jr $ra

#Branch to print if even
PrintEven:
li $v0, 4
la $a0, even
syscall
jr $ra

# Exit the program
Exit:
li $v0, 10
syscall

.data
prompt1: .asciiz "\nPlease enter an integer: "
prompt2: .asciiz "\nPlease enter another integer: "
prompt3: .asciiz "\nPlease choose a math operation (+, -, *, or /): "
error: .asciiz "\nNot gonna work!"
remainder: .asciiz " with remainder "

br: .asciiz "\n"
e: .asciiz " = "
gt: .asciiz " is greater than "
lt: .asciiz " is less than than "
et: .asciiz " is equal to "

even: .asciiz "(even)"
odd: .asciiz "(odd)"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 14:32:45