assembly - Address Out of range mips -


hello trying implement mccarthy 91 function in assembly code. when run code assembles no errors when enter value n program gets runtime exception address out of range. i've looked on code , cant find out why keeps happening. error happens on

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

code trying implement:

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

code:

main:     #build stack frame     subu $sp, $sp, 32   #stack frame 32 bytes long     sw $ra, 20($sp)     #save return address     sw $fp, 16($sp)     #save old frame pointer     addiu $fp, $sp, 28  #set frame pointer      li $v0, 4       #system call code print_str     la $a0, prompt      #address of string print     syscall         #print string      li $v0, 5       #system call read_int     syscall         #read n     move $t0, $v0       #and store     move $a0, $t0       #move argument      move $t9, $zero     #clear count     jal mcc         #call mcc function      move $s0, $v0       #get answer returned mcc , save      li $v0, 4       #system call code print_str          la $a0, ans     #address of string print     syscall         #print ans string      move $a0, $s0       #move answer ready print     li $v0, 1       #system call code print_int     syscall         #print answer      li $v0, 4       #system call code print_str     la $a0, count       #address of string print     syscall         #print count string      move $a0, $t9       #get count ready print     li $v0, 1       #system call code 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 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 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 n  done:               #result in $v0     #tear down stack frame     lw $ra, 0($sp)      #restore $ra     lw $a0, 4($sp)      #restore n     addiu $sp, $sp, 12  #pop stack     jr $ra          #return caller 

here's line things go wrong:

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

you're trying move return value previous call mcc $a0, never place return value in $v0. you'll need change part:

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

into:

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

i tried program change , got answer=93,count=1 n=103, , answer=91,count=5 n=99, seems correct me.


Comments