【发布时间】:2016-01-05 13:19:56
【问题描述】:
最近在大学开始学习 MIPS 时,我在尝试打印 1 个字符串,接受用户输入,然后打印另一个字符串并接受用户输入时遇到了问题。两个用户输入应分别存储到寄存器 a0 和 a1。
每个字符串的名称是promptD 用于Dividend 输入,enterD 用于Divisor 输入(您可能会猜到这是一个无符号除法计算器程序)。
在我的调试尝试中,我已将问题缩小到代码的一小段 sn-p,发布在下面。
我认为我错误地将我的第一个 .data 寄存器偏移到我的第二个 .data 寄存器。我在尝试 QTspim、xspim、PCspim 和 MARS 时注意到的问题是,所有这 4 个都为 .data 中的第一个字符串提供了不同的初始寄存器地址。
例如:字符串“Enter Dividend”在 MARS 中将位于注册地址 0x10010000 中,但在 PCspim 中将从 0x10000000 开始。 “输入除数”的以下寄存器地址将在 MARS 中的 0x10010011 或 PCspim 中的 0x10000010 中。
在通过 MARS 的当前状态下,下面的程序 sn-p 要求用户输入被除数,并将存储该值。存储到 a0 后,代码将立即失败,原因是 0x00400024 处的第 37 行(这只是第三个系统调用)运行时异常:地址超出范围 0x00000004。它根本没有提示“输入除数”。
要真正看到实际存在的问题,我认为在 MARS 中运行它会有助于使其更加清晰。是抵消的问题吗?我是否在没有看到它的情况下破坏了一个寄存器?我在这里没有找到很多 MIPS 帮助来处理没有伪指令的问题。我意识到,我可以直接加载地址(la)......但我不能在这里使用它们。
谢谢
.globl main
.data #for the data
promptD: .asciiz "Enter Dividend \n"
enterD: .asciiz "Enter Divisor \n"
# result: .asciiz "Result = "
.text #for the instructions
main:
#for Dividend
addi $v0, $0, 4 #store string instr to v0
lui $a0, 0x1001 #address of promptD
syscall #display promptD
addi $v0, $0, 5 #store input instr to v0
syscall # Get dividend
add $a0, $0, $v0 # Dividend to $a0
#for Divisor
addi $v0, $0, 4 #store string instr to v0
lui $a1, 0x1001 #Where I think the problem is...
#Address of first string followed by add offset?
addi $a1, $a1, 33 #Maybe incorrect offset?
syscall #display enterD
addi $v0, $0, 5 #store input instr to v0
syscall # Get divisor
add $a1, $0, $v0 # Divisor to $a1
#end snippet
【问题讨论】:
标签: string mips spim mars-simulator bare