2013-03-11 89 views
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

Aany help would rea值得讚賞。彙編編程真的很糟糕。

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

你的平衡之後A月:

101。19999695

您的餘額後一個月:

101.19999695

所以我得到的代碼重複10次。我需要更新金額,以便顯示上個月+增加的利息

+0

㈣嘗試了一些東西律。沒有什麼值得一提的。我有一些關於如何設置堆棧幀和幀指針的例子,但並不真正知道我在做什麼。 – CoffeePeddlerIntern 2013-03-11 03:06:52

+0

我知道它現在迭代10次。但價值不更新。我真的不知道如何去浮點數,以及如何更新和保存它。 – CoffeePeddlerIntern 2013-03-11 03:08:03

回答

1

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

I.e.是這樣的:

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

非常感謝您的迴應。幫了很多!真的很感激它! – CoffeePeddlerIntern 2013-03-11 15:11:09

0

該問題不需要遞歸 - 一個簡單的循環就可以做到。彙編中的循環只是條件跳轉代碼(或無條件跳轉與前面的條件跳轉相結合)。

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

+0

部分任務是必須通過遞歸 – CoffeePeddlerIntern 2013-03-11 01:50:08