几个问题...
在做syscall 5 得到用户的值后,在$v0。您试图使用 lw 指令将其转移到 $t1。但是,您使用$v0 作为lw 的基地址。如果用户输入的值不是四的倍数,这将导致对齐异常 [as you got]
你想要的是move [注册到注册] 并且不是从内存中加载。但是,实际上,您真的不需要这样做。
相反,只需将$v0 存储到n1 中,$t0 指向它。所以,你的sw 也是不正确的。
另外,因为n1 和n2 不是.data 指令之后的第一项,它们可能没有对齐到四字节边界,因为他们必须在他们身上使用lw/sw。所以,你需要一个.align 指令。
n2 的输入重复了这些问题。
而且,在打印之后,没有任何东西可以停止程序,所以它会继续运行,尝试使用内存中的 任何 随机值获取和执行指令。该程序需要syscall 才能在完成后正确终止执行。
我已经注释了你的代码,显示了之前和之后,并修复了错误[请原谅无偿的风格清理]:
.data
prompt_n1: .asciiz "Enter first integer n1: "
prompt_n2: .asciiz "Enter second integer n2: "
debug_print: .asciiz "Your numbers are: "
space: .asciiz " "
# NOTE/BUG: because of the variable length byte arrays above (i.e. the .asciiz
# directives), these may not be aligned to a four byte boundary. there are two
# solutions:
# (1) place n1 and n2 immediately after the .data directive
# (2) add a .align directive [as below]
.align 4
n1: .word 0
n2: .word 0
.text
main:
# Prompt the user for n1.
li $v0,4
la $a0,prompt_n1
syscall
# Store console: n1
li $v0,5
syscall
# Attempts to load value of v0 into n1 - Problem Here
la $t0,n1
# NOTE/BUG: this is incorrect for several reasons:
# (1) $v0 has a value input by the user -- it could be 0, 1, etc.
# (2) these are not valid data addresses
# (3) if the user enters a value that is _not_ a multiple of four, it
# generates an alignment exception
# (4) this instruction is just not needed and is just wrong
###lw $t1,0($v0)
# NOTE/BUG: $t1 has _not_ been set -- this should use the [correctly] set
# register $t0 [which has the address of n1] and should store the value
# read from the user
###sw $t0,0($t1)
sw $v0,0($t0)
# Prompt the user for n2.
li $v0,4
la $a0,prompt_n2
syscall
# Store console: n2
li $v0,5
syscall
# Attempts to load value of v0 into n1 - Problem Here
# NOTE/BUG: the lw is commented out for the same reason as above and has
# a similar mistake as above
la $t0,n2
###lw $t4,0($v0)
###sw $t0,0($t4)
sw $v0,0($t0)
j print_statement
print_statement:
li $v0,4
la $a0,debug_print
syscall
li $v0,1
lw $a0,n1
syscall
# NOTE/BUG: added this to separate the numbers
li $v0,4
la $a0,space
syscall
li $v0,1
lw $a0,n2
syscall
# NOTE/BUG: there was no more code here so it just "falls off the end of
# the world" -- we need to terminate program execution correctly with the
# exit syscall
li $v0,10
syscall
一些资源:
http://www.mrc.uidaho.edu/mrc/people/jff/digital/MIPSir.html
http://courses.missouristate.edu/kenvollmar/mars/help/syscallhelp.html
http://logos.cs.uic.edu/366/notes/mips%20quick%20tutorial.htm
http://chortle.ccsu.edu/assemblytutorial/index.html