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

任何建议都会很好。我需要做的问题还有很多。但我需要先完成这部分。我觉得我的大脑已经筋疲力尽了

4

1 回答 1

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
于 2013-03-25T06:12:11.010 回答