Assembly 汇编程序:递归?

Assembly 汇编程序:递归?,assembly,recursion,mips,pcspim,Assembly,Recursion,Mips,Pcspim,我需要写一个程序,将迭代10次。每次它都会更新一个值并将其打印到屏幕上 我知道必须创建一个堆栈并保存值,这样它才能迭代回正确的部分继续程序。我试过很多东西,但我想不出来。这是到目前为止我的代码 # ############################################################### # # Phase2.ASM # #

我需要写一个程序,将迭代10次。每次它都会更新一个值并将其打印到屏幕上

我知道必须创建一个堆栈并保存值,这样它才能迭代回正确的部分继续程序。我试过很多东西,但我想不出来。这是到目前为止我的代码

# ############################################################### #
# Phase2.ASM                                                      #
#                                                                 #
# This program will recurse 10 times and show how much interest   #
# is made after 10        "months"                                        #
#                                                                 #
# ############################################################### #   

.data

PRINCIPAL:  .float  100.0   # principal = $100.00
INTEREST_RATE:  .float  0.012   # interest  = 1.2%

promptFirst:     .asciiz "Your starting Principal is $100.00: \n"
promptSecond:     .asciiz "Your interest rate is 1.2%: \n"
promptNow:          .asciiz "Interest Made After a Month:\n"
.text
.globl main

main:   



First:   
     # Prints the first prompt  
     li $v0, 4               # syscall number 4 will print string whose address is in $a0       
     la $a0, promptFirst     # "load address" of the string
     syscall                 # actually print the string   


Second:  
     # Prints the second prompt
     li $v0, 4               # syscall number 4 will print string whose     address is in $a0   
     la $a0, promptSecond    # "load address" of the string
     syscall                 # actually print the string    



jal CI


j EXIT



CI:




    la  $a0, PRINCIPAL  # load the address of the principal
    la  $a1, INTEREST_RATE  # load the address of the principal

    lwc1  $f2, ($a0)    # load the principal
    lwc1  $f4, ($a1)    # load the interest rate    
    mul.s $f12, $f4, $f2    # calculate the balance


    li $v0, 4            # syscall number 4 will print string whose address is in $a0   
    la $a0, promptNow    # "load address" of the string
    syscall              # actually print the string
    li  $v0, 2           # system call #2   
    syscall

jr $ra




EXIT:    
jr $ra


# END OF THE LINES ###############################################
到目前为止,我的当前输出:

您的起始本金为100.00美元:

您的利率是1.2%:

一个月后产生的利息:

1.20000005

任何帮助都将不胜感激。我真的很不擅长汇编编程

PS:赋值必须通过递归完成

编辑!新代码

# ############################################################### #
# Phase2.ASM                                                      #
#                                                                 #
# This program will recurse 10 times and show how much interest   #
# is made after 10     "months"                                       #
#                                                                 #
# ############################################################### #   

.data

PRINCIPAL:  .float  100.0   # principal = $100.00
INTEREST_RATE:  .float  1.012   # interest  = 1.2%

promptFirst:     .asciiz "Your starting Principal is $100.00: \n"
promptSecond:     .asciiz "Your interest rate is 1.2%: \n"
promptNow:          .asciiz "\nYour Balance After A Month:\n"
.text
.globl main

main:   



First:   
     # Prints the first prompt  
     li $v0, 4               # syscall number 4 will print string whose address is in $a0       
     la $a0, promptFirst     # "load address" of the string
     syscall                 # actually print the string   


Second:  
     # Prints the second prompt
     li $v0, 4               # syscall number 4 will print string whose address is in $a0   
     la $a0, promptSecond    # "load address" of the string
     syscall                 # actually print the string    


li $t1, 0
jal CI

ENDCI:
j EXIT



CI:



    add $t1, $t1, 1 
    la  $a0, PRINCIPAL      # load the address of the principal
    la  $a1, INTEREST_RATE  # load the address of the principal

    lwc1  $f2, ($a0)        # load the principal
    lwc1  $f4, ($a1)        # load the interest rate    
    mul.s $f12, $f4, $f2    # calculate the balance


    li $v0, 4               # syscall number 4 will print string whose address is in $a0   
    la $a0, promptNow       # "load address" of the string
    syscall                 # actually print the string
    li  $v0, 2              # system call #2    
    syscall

    beq $t1, 10, ENDCI
    jal CI
jr $ra




EXIT:    
jr $ra


# END OF THE LINES ###############################################
新产出:

我们的起始本金为100.00美元: 您的利率是1.2%:

您一个月后的余额:

101.19999695

您一个月后的余额:

101.19999695

您一个月后的余额:

101.19999695

您一个月后的余额:

101.19999695

您一个月后的余额:

101.19999695

您一个月后的余额:

101.19999695

您一个月后的余额:

101.19999695

您一个月后的余额:

101.19999695

您一个月后的余额:

101.19999695

您一个月后的余额:

101.19999695


所以我让代码迭代10次。我需要更新金额,使其显示前一个月+增加的利息

问题不需要递归-一个简单的循环就可以了。程序集中的循环只是代码的条件跳转(或无条件跳转与条件提前跳转相结合)


条件是基于计数器的,计数器将从0变为9,或从9变为0。在汇编中,通常更容易基于与零的比较进行条件跳转。

每次更新后,您需要存储当前余额,以便下次调用不会继续使用原始值

也就是说,类似这样的事情:

lwc1  $f2, ($a0)        # load the principal
lwc1  $f4, ($a1)        # load the interest rate    
mul.s $f12, $f4, $f2    # calculate the balance
swc1  $f12, ($a0)
在每次调用
CI
之前,您还需要保存当前的返回地址,然后在返回之前将其还原:

addi $sp,$sp,-4     # push the current return address
sw   $ra,($sp)      # ...
beq  $t1, 10, CIRET
jal  CI
CIRET:
lw   $ra,($sp)      # pop the saved return address
addi $sp,$sp,4      # ....
jr   $ra

任务的一部分是,它必须通过recursionIv尝试一些lil的东西。没有什么真正值得一提的。我在课堂上有一些关于如何设置堆栈框架和框架指针的例子,但我不知道我在做什么。我知道它现在迭代了10次。但该值不会更新。我真的不知道如何使用浮点数,以及如何更新和保存它。非常感谢您的回复。帮了很多忙!真的很感激!