Mips组装中的Tribonacci

时间:2014-10-07 16:24:03

标签: assembly mips fibonacci

我想出了问题......我想。我让程序运行起来。我现在的问题是这会被认为是递归的吗?我尽力评论它。我只是在调用自身的函数之外操作堆栈。我的老师想要一个递归程序,我真的不确定这是否符合条件。此外,我想知道这是否比递归执行此程序的替代正常方式更好或更差。

.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

1 个答案:

答案 0 :(得分:1)

如果在解决问题时解决了较小的子问题,则使用递归。

提示:您是否在较大的问题中解决较小的问题(找到n-1,n-2,n-3)?或者,你的问题是自称吗?

递归求解的另一种方法是从您选择的任何数字开始迭代计算tribonacci数。