简体   繁体   English

Tribonacci在Mips组装中

[英]Tribonacci in Mips Assembly

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? 提示:您是否正在解决较大问题中的较小问题(找到n-1,n-2,n-3)? 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. 递归求解的另一种方法是从您选择的任何数字开始迭代计算tribonacci数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM