2013-03-25 89 views
0

我試圖編寫一個程序集,它將充當使用遞歸循環的複利計數器。我能夠讓程序與一個委託人一起工作,並設定利率,並重復10次,在每次迭代後顯示餘額。現在我試圖更改它因此它要求用戶的起始本金,利率和目標本金。然後該程序需要迭代,直到滿足目標主體。程序集問題

這是我迄今爲止的非工作代碼。我想即時搞亂我正在使用哪些寄存器。 Iv嘗試將beq行上使用的這些寄存器更改爲$ a2和$ a0,但該功能也無效。有什麼建議麼? Idk如果關閉或離開。我有一個困難的時間以下寄存器=/

promptFirst:  .asciiz "Enter Your Starting Balance: \n" 
promptSecond:  .asciiz "Enter the Interst Rate: \n" 
promptThird:  .asciiz "Enter Your Target Balance \n" 


promptNow:   .asciiz "\nYour Balance After A Iteration:\n" 
.text 
.globl main 

main: 




    # 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 

    # Reads in the first operand 
    li $v0, 5    # syscall number 5 will read an int 
    syscall     # actually read the int 
    move $s0, $v0   # save result in $s0 for later 


    # 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  

    # Reads in the second operand 
    li $v0, 5    # syscall number 5 will read an int 
    syscall     # actually read the int 
    move $s1, $v0   # save result in $s1 for later 

    # Prints the third prompt 
    li $v0, 4    # syscall number 4 will print string whose address is in $a0 
    la $a0, promptThird # "load address" of the string 
    syscall     # actually print the string  

    # Reads in the third operand 
    li $v0, 5    # syscall number 5 will read an int 
    syscall     # actually read the int 
    move $s2, $v0   # save result in $s2 for later 



jal LOOP 

ENDLOOP: 
j EXIT 



LOOP: 




    la $a0, $s0 # load the address of the principal 
    la $a1, $s1 # load the address of the interest 
    la $a2, $s2 # load the address of the goal principal 


    lwc1 $f2, ($a0)  # load the principal 
    lwc1 $f4, ($a1)  # load the interest rate  
    lwc1 $f6 ($a2) 

    mul.s $f12, $f4, $f2 # calculate the balance 
    swc1 $f12, ($a0) 

    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 

    addi $sp,$sp,-4  # push the current return address 
    sw $ra,($sp)  
    beq $f12, $f6, LOOPRET 

    beq $f12, $f6, ENDLOOP 

    jal LOOP 


LOOPRET: 

    lw $ra,($sp)  # pop the saved return address 
    addi $sp,$sp,4  
    jr $ra 






EXIT:  
jr $ra 

任何建議將是很好的。這些問題更多的是我需要做的。但我需要首先解決這個問題。我覺得好像我已經精疲力竭了我的大腦

回答

0
la $a0, $s0 # load the address of the principal 

這是否甚至編譯? la的目的是爲[L]添加標籤的[A]地址。你不能取寄存器的地址。

lwc1 $f2, ($a0)  # load the principal 

把不正確的值在$a0一旁,lwc1不執行任何種類的整數到浮動轉換。所以你這樣做不會得到適當的浮動。

什麼,你可能應該做的是報廢la/lwc1指令,而是使用類似:

mtc1 $s0,$f2  # move $s0 to floating point register $f2 
cvt.s.w $f2,$f2 # convert the integer in $f2 to a float 
# ..similarly for the rest of your values 
+0

謝謝,我真的新彙編編程。我使用la指令的原因是因爲我正在加載一個.float變量,我聲明爲PRINCIPAL = 100.我使用了(la $ a0,PRINCIPAL)。當我設置它來讓遞歸工作時,我做了那部分。我猜想我認爲PRINCIPAL部分只是一個存儲位置,它保存了100位。所以我的思考過程將其替換爲用戶歸屬委託人的存儲位置。但我想我錯了。 = S很混亂。它在這裏遲到了。明天我會檢查你的解決方案。我真的很感激你的迴應。謝謝! – user2206189 2013-03-25 06:38:54

+0

我試過你修復。我通過了那個編譯錯誤。但是現在,我得到了一個編譯錯誤的beq函數調用。有浮點beq嗎? – user2206189 2013-03-25 18:56:32

+0

請參閱http://www.doc.ic.ac.uk/lab/secondyear/spim/node20.html – Michael 2013-03-25 20:17:12