0

我需要编写一个可以迭代 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次的代码。我需要更新金额,以便显示上个月+利息

4

2 回答 2

1

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

即是这样的:

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
于 2013-03-11T08:50:23.067 回答
0

这个问题不需要递归——一个简单的循环就可以了。汇编中的循环只是代码的有条件跳转(或无条件跳转与条件跳转结合)。

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

于 2013-03-11T01:46:45.517 回答