0

Currently trying to develop a mips program for generating a math sequence. However, i keep getting a Mips exception at PC.

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
4

1 回答 1

0

由于您使用的是 QTSimp(正如您的标签所建议的那样),您通常需要包含一些代码来表示从哪里开始以及退出。尝试在开头添加以下代码:

.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在条款末尾添加 a 以返回 main (然后在条款初始化后将调用 print )。然后,添加j exit到 print 下的退出标签,以便在打印完成时跳转到退出例程。

现在的问题是sequenceandendline没有定义,所以调用la $a0, sequenceandla $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

希望有帮助!

于 2013-12-14T19:22:21.507 回答