Reputation: 73
Hello i am trying to implement the mccarthy 91 function in assembly code. When i run my code it assembles with no errors but when i enter a value for n the program gets runtime exception address out of range. I've looked over my code and i cant find out why this keeps happening. The error happens on
sw $ra, 0($sp) #save return address
Code trying to 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 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
Upvotes: 0
Views: 2078
Reputation: 58427
Here's the line where things go wrong:
move $a0, $v0 #n = mcc91(n + 11)
You're trying to move the return value from a previous call to mcc
into $a0
, but you never actually place the return value in $v0
. So you'll need to change this part:
base:
subi $a0, $a0, 10 #subtract 10 from n
into:
base:
subi $v0, $a0, 10 # return n - 10
I tried the program with that change and got answer=93,count=1 for n=103, and answer=91,count=5 for n=99, which seems correct to me.
Upvotes: 1