I figured out the problem... I think. I got the program to function. My question now is would this be considered recursive??? I tried to comment it the best I could. I manipulate the stack outside of just the function calling itself. My teacher wants a recursive program and I'm really unsure if this qualifies now. Also I would like to know if this is better or worse than an alternative, normal way of recursively doing this program.
.data
string: .space 11 #allocates space for string + /n
char: .space 2
prompt0: .asciiz "Enter tribonacci(0): "
prompt1: .asciiz "Enter tribonacci(1): "
prompt2: .asciiz "Enter tribonacci(2): "
promptn: .asciiz "Enter n: "
prompt00: .asciiz "tribonacci("
prompt01: .asciiz ") = "
newline: .asciiz "\n"
cerror: .asciiz "Characters entered were not all digits!"
#s0 = trib0
#s1 = trib1
#s2 = trib2
#s3 = n
#a3 = current poistion on stack
#v0 = trib(current postion on stack)
.text
#trib0
start: la $a0, prompt0 #a0 = prompt address
li $v0, 4 #v0 = print string
syscall #print prompt message
li $v0, 5 #v0 = read int
syscall #read
move $s0, $v0 #store trib0 in s0
#trib1
la $a0, prompt1 #a0 = prompt address
li $v0, 4 #v0 = print string
syscall #print prompt message
li $v0, 5 #v0 = read int
syscall #read
move $s1, $v0 #store trib1 in s1
#trib2
la $a0, prompt2 #a0 = prompt address
li $v0, 4 #v0 = print string
syscall #print prompt message
li $v0, 5 #v0 = read int
syscall #read
move $s2, $v0 #store trib2 in s2
#n
la $a0, promptn #a0 = prompt address
li $v0, 4 #v0 = print string
syscall #print prompt message
li $v0, 5 #v0 = read int
syscall #read
move $s3, $v0 #store n in s3
#trib
recurs: add $a3, $s3, $zero #store n in a3
add $v0, $zero, $zero #initialize v0 to 0
jal trib #call trib function
move $a1, $v0 #a1 = trib(n)
#output
print: la $a0, prompt00 #a0 = prompt address
li $v0, 4 #v0 = print string
syscall #print prompt message
li $v0, 1 #v0 = print int
add $a0, $s3, $zero #a0 = n
syscall #print
la $a0, prompt01 #a0 = prompt address
li $v0, 4 #v0 = print string
syscall #print prompt message
li $v0, 1 #v0 = print int
add $a0, $a1, $zero #a0 = answer
syscall #print
#exit program
exit: li $v0, 10 #v0 = exit
syscall #exit program
trib: addi $sp, $sp, -12 #save stack size
sw $ra, 0($sp) #store address on stack
sw $a3, 4($sp) #store current location n
sw $v0, 8($sp) #store value trib(n)
beq $a3, 0, return0 #check if current location is 0
beq $a3, 1, return1 #check if current location is 1
beq $a3, 2, return2 #check if current location is 2
addi $a3, $a3, -1 #n = n - 1
jal trib #find n - 1
addi $sp, $sp, -12 #stack shifts down 1
addi $a3, $a3, -1 #n = n - 1
jal trib #find n - 2
addi $sp, $sp, -12 #stack shifts down 1
addi $a3, $a3, -1 #n = n - 1
jal trib #find n - 3
addi $sp, $sp, 24 #move stack back to current location
addi $a3, $a3, 3 #move n back to current location
lw $t0, -4($sp) #temp1 = n - 1
lw $t1, -16($sp) #temp2 = n - 2
lw $t2, -28($sp) #temp3 = n - 3
add $v0, $t0, $t1 #v0 = temp1 + temp2
add $v0, $v0, $t2 #v0 += temp3
sw $v0, 8($sp) #store v0 at trib(n)
j endlp #endcall
return0:sw $s0, 8($sp) #return trib0 to trib(0)
j endlp
return1:sw $s1, 8($sp) #return trib0 to trib(1)
j endlp
return2:sw $s2, 8($sp) #return trib0 to trib(2)
j endlp
endlp: lw $ra, 0($sp) #load register address to ra
lw $a3, 4($sp) #load n location to a3
lw $v0, 8($sp) #load trib(n) to v0
addi $sp, $sp, 12 #stack moves up one
jr $ra #return to ra address
You are using recursion if you solve a smaller sub-problem when solving a problem.
Hint: Are you solving smaller problems (finding n-1, n-2, n-3) in your larger problem? Or, is your problem calling itself?
An alternative to solving recursively is to iteratively compute the tribonacci number starting from whatever numbers you choose to start with.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.