1
나는 문제를 발견했다. 나는 생각한다. 나는 프로그램을 작동 시키도록했다. 내 질문은 지금 이것은 재귀로 간주됩니까 ??? 내가 할 수있는 최선의 의견을 말하려고했다. 나는 그 자체를 호출하는 함수 밖에서 스택을 조작한다. 선생님은 재귀 프로그램을 원하며 현재 자격이되는지 확실하지 않습니다. 또한 이것이 재귀 적으로이 프로그램을 수행하는 대체, 일반적인 방법보다 좋고 나쁜지 알고 싶습니다.Mips Assembly의 Tribonacci
.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