我需要编写一个迭代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 :(得分: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
答案 1 :(得分:0)
问题不是要求递归 - 一个简单的循环就可以了。程序集中的循环只是一个条件性的跳转代码(或无条件的跳转与前面的条件跳转相结合)。
条件是基于计数器的,计数器将从0变为9,或从9变为0,组装时通常更容易根据与零的比较进行条件跳转。