2013-03-25 60 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