PC上的Mips异常

时间:2013-11-29 18:27:51

标签: 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
    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

1 个答案:

答案 0 :(得分:0)

由于您使用的是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添加到打印下的退出标签,以便在打印完成后跳转到退出例程。

现在问题是sequenceendline未定义,因此调用la $a0, sequencela $a0, endline将无效。因为我没有看到它的目的,我摆脱了la $a0, endline。您应该在开头.data标签之后(.text标签之前)定义序列。例如,如果sequence是一个设置为0的整数数组,那么这就是它的样子:

.globl main

.data 
    sequence: .word 0:8    #initialize sequence to array of 8 integers (value 0)

.text

 main: ...

print的分支说明中,您有beq $s1, $0, quit。这应该是beq $s1, $zero, quit

通过上述更改,我尝试时运行正常。作为参考,这是我使用的最终代码:

.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

希望有所帮助!