Assembly PC上的Mips异常
目前正在尝试开发一个用于生成数学序列的mips程序。但是,我在PC上不断遇到Mips异常Assembly PC上的Mips异常,assembly,mips,spim,mars-simulator,qtspim,Assembly,Mips,Spim,Mars Simulator,Qtspim,目前正在尝试开发一个用于生成数学序列的mips程序。但是,我在PC上不断遇到Mips异常 terms: addi $sp, $sp, -12 #decrement the stack pointer by 12 sw $ra, 8($sp) sw $s1, 4($sp) sw $s0, 0($sp) add $s0, $a0, $0 li $s1, 16 la $a0, sequence # display sequence
terms:
addi $sp, $sp, -12 #decrement the stack pointer by 12
sw $ra, 8($sp)
sw $s1, 4($sp)
sw $s0, 0($sp)
add $s0, $a0, $0
li $s1, 16
la $a0, sequence # display sequence
li $v0, 4
syscall
print:
add $a0, $zero, $s0
li $v0, 1
syscall
addi $s1, $s1, -1
beq $s1, $0, quit
addi $a0, $s0, 0
jal initiliazeCompute
addi $s0, $v0, 0
j print
quit:
la $a0, endline
jr $ra
initiliazeCompute:
addi $v0, $0, 0
li $t2, 10
Compute:
rem $t1, $a0, $t2 # $t1 is units digit of current term
mul $t1, $t1, $t1 # square that value
add $v0, $v0, $t1 # add this value to next term
div $a0, $a0, $t2 # discard units digit of current term
jr $ra # return to calling routine
因为您使用的是QTSpim(正如您的标记所建议的),所以通常需要包含一些代码来指示从何处开始以及退出。请尝试在开头添加以下代码:
.globl main
.text
main:
jal terms #I'm assuming terms is the first routine to be executed
j print #jump to print. When print is done, it jumps to exit
exit:
.end #ends the program
li $v0, 10 #syscall code for exiting
syscall
请注意,您还需要在术语末尾添加一个jr$ra
,以返回到main(然后在术语初始化后调用print)。然后,将j exit
添加到“打印”下的“退出”标签中,以便打印完成后跳转到“退出”例程
现在的问题是没有定义sequence
和endline
,因此调用la$a0,sequence
和la$a0,endline
将不起作用。我去掉了la$a0,endline,因为我不知道它的用途。您应该在开头的.data
标签之后(在.text
标签之前)定义序列。例如,如果序列是设置为0的整数数组,则如下所示:
.globl main
.data
sequence: .word 0:8 #initialize sequence to array of 8 integers (value 0)
.text
main: ...
在print
中的分支指令中,您有beq$s1,$0,退出
。这应该是beq$s1,$0,请退出
有了上面的更改,当我尝试它时,它运行良好。作为参考,以下是我使用的最终代码:
.globl main
.data
sequence: .word 0:8 #sequence := array of 8 integers (value 0)
.text
main:
jal terms #start by executing terms
j print #once terms has run, jump to print
exit:
.end #exit program
li $v0, 10
syscall
terms:
addi $sp, $sp, -12 #decrement the stack pointer by 12
sw $ra, 8($sp)
sw $s1, 4($sp)
sw $s0, 0($sp)
add $s0, $a0, $0
li $s1, 16
la $a0, sequence # display sequence
li $v0, 4
syscall
jr $ra
print:
add $a0, $zero, $s0
li $v0, 1
syscall
addi $s1, $s1, -1
beq $s1, $zero, quit
addi $a0, $s0, 0
jal initiliazeCompute
addi $s0, $v0, 0
j print
quit:
# la $a0, endline - this isn’t necessary
j exit #jump to exit routine
initiliazeCompute:
addi $v0, $0, 0
li $t2, 10
Compute:
rem $t1, $a0, $t2 # $t1 is units digit of current term
mul $t1, $t1, $t1 # square that value
add $v0, $v0, $t1 # add this value to next term
div $a0, $a0, $t2 # discard units digit of current term
jr $ra # return to calling routine
希望有帮助 需要更多信息。它对我来说运行良好。那么,当异常发生时,PC的值是多少?代码中的哪个指令对应?