1
我想出了問題......我想。我得到了該程序的功能。我現在的問題是這會被視爲遞歸?我試着盡我所能評論它。我只是在調用它自己的函數之外操作堆棧。我的老師想要一個遞歸程序,我真的不確定現在是否合格。此外,我想知道這是否比遞歸執行此程序的替代正常方式更好或更差。Tribonacci Mips Assembly
.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