簡體   English   中英

匯編程序:遞歸?

[英]Assembly Program: Recursion?

我需要編寫一個將迭代10次的程序。 每次它將更新一個值並將其打印到屏幕上。

我知道必須做一些事情來創建堆棧並保存值,以便它可以迭代並到達正確的部分以繼續執行程序。 iv嘗試了很多事情,但我無法弄清楚。 到目前為止,這是我的代碼

# ############################################################### #
# 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

Aany的幫助將不勝感激。 我真的很擅長匯編程序設計。

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次的代碼。 我需要更新金額,以便顯示上個月的利息+已添加的利息

您需要在每次更新后存儲當前余額,以便下次調用不會繼續使用原始值。

即是這樣的:

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

問題不要求遞歸-一個簡單的循環就可以。 匯編中的循環僅僅是代碼的有條件跳轉(或無條件跳轉與有條件跳轉的結合)。

條件是基於計數器的,計數器從0到9,或者從9到0,通常在組裝時基於與零的比較來進行條件跳轉通常比較容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM