【问题标题】:Address Out of range mips地址超出范围的 mips
【发布时间】:2015-06-03 03:28:33
【问题描述】:

您好,我正在尝试用汇编代码实现 mccarthy 91 函数。当我运行我的代码时,它没有错误地组装,但是当我输入 n 的值时,程序的运行时异常地址超出范围。我查看了我的代码,但我不知道为什么会这样。错误发生在

 sw $ra, 0($sp)     #save return address

尝试实现的代码:

 def mcc91(n):global countcount += 1     
       if n > 100:     
          n -10
       else: 
          return mcc91(mcc91(n + 11))

代码:

main:
    #Build stack frame
    subu $sp, $sp, 32   #stack frame is 32 bytes long
    sw $ra, 20($sp)     #save return address
    sw $fp, 16($sp)     #save old frame pointer
    addiu $fp, $sp, 28  #set up frame pointer

    li $v0, 4       #system call code for print_str
    la $a0, prompt      #address of string to print
    syscall         #print the string

    li $v0, 5       #system call for read_int
    syscall         #read n
    move $t0, $v0       #and store it
    move $a0, $t0       #move the argument

    move $t9, $zero     #clear the count
    jal mcc         #call the mcc function

    move $s0, $v0       #get the answer returned from mcc and save it

    li $v0, 4       #system call code for print_str     
    la $a0, ans     #address of string to print
    syscall         #print the ans string

    move $a0, $s0       #move answer to get ready to print
    li $v0, 1       #system call code for print_int
    syscall         #print answer

    li $v0, 4       #system call code for print_str
    la $a0, count       #address of string to print
    syscall         #print count string

    move $a0, $t9       #get count ready to print
    li $v0, 1       #system call code for print_int
    syscall         #print count

    #tear down stack frame
    lw $ra 20($sp)      #restore return address
    lw $fp, 16($sp)     #restore frame pointer
    addiu $sp, $sp, 32  #pop stack frame
    li $v0, 10      #exit program

mcc:
    addi $t9, $t9, 1    #increment global count

    #Build stack frame
    subu $sp, $sp, 12   #stack frame is 32 bytes long
    sw $ra, 0($sp)      #save return address
    sw $a0, 4($sp)      #save n

    bgt $a0, 100, base  #base call

    addi $a0, $a0, 11   #add 11 to n
    jal mcc         #mcc91(n+11)
    move $a0, $v0       #n = mcc91(n + 11)
    jal mcc         #do outer recursion
    j done

base:
    subi $a0, $a0, 10   #subtract 10 from n

done:               #result is in $v0
    #tear down the stack frame
    lw $ra, 0($sp)      #restore $ra
    lw $a0, 4($sp)      #restore n
    addiu $sp, $sp, 12  #pop stack
    jr $ra          #return to caller

【问题讨论】:

  • 对我来说听起来像是堆栈溢出(只是猜测和运行),将看看它。您的堆栈结构看起来有点不合逻辑。预留太多?

标签: assembly mips


【解决方案1】:

这是出错的地方:

move $a0, $v0       #n = mcc91(n + 11)

您试图将返回值从先前调用 mcc 移动到 $a0,但实际上您从未将返回值放入 $v0。所以你需要改变这部分:

base:
    subi $a0, $a0, 10   #subtract 10 from n

进入:

base:
    subi $v0, $a0, 10   # return n - 10

我尝试了具有该更改的程序,并得到 answer=93,count=1 for n=103, answer=91,count=5 for n=99,这对我来说似乎是正确的。

【讨论】:

  • 感谢您的帮助!解决了问题不敢相信我错过了哈哈
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 1970-01-01
相关资源
最近更新 更多