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