简体   繁体   中英

Assembly Program: Recursion?

I need to write a program that will iterate 10 times. Each time it will update a value and print it to the screen.

I know there has to do something with creating a stack and saving the the value so it can iterate back and get to the correct portion to continue with the program. Iv tried many things but I cant figure it out. Here is my code so far

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

My Current output so far:

Your starting Principal is $100.00:

Your interest rate is 1.2%:

Interest Made After a Month:

1.20000005

Aany help would really be appreciated. I really am terrible at assembly programming.

PS: The assignment HAS to be done via recursion

EDIT! NEW CODE

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

new output:

our starting Principal is $100.00: Your interest rate is 1.2%:

Your Balance After A Month:

101.19999695

Your Balance After A Month:

101.19999695

Your Balance After A Month:

101.19999695

Your Balance After A Month:

101.19999695

Your Balance After A Month:

101.19999695

Your Balance After A Month:

101.19999695

Your Balance After A Month:

101.19999695

Your Balance After A Month:

101.19999695

Your Balance After A Month:

101.19999695

Your Balance After A Month:

101.19999695

So i got the code to iterate 10 times. I need to update the amount so it shows up the previous month + interest added

You need to store the current balance after each update so that the next call doesn't keep using the original value.

Ie something like this:

lwc1  $f2, ($a0)        # load the principal
lwc1  $f4, ($a1)        # load the interest rate    
mul.s $f12, $f4, $f2    # calculate the balance
swc1  $f12, ($a0)

Before each additional call to CI you also need to save the current return address, and then restore it before returning:

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

The problem does not call for a recursion - a simple loop would do. A loop in assembly is just a conditional jump up the code (or an unconditional jump up combined with a conditional jump ahead).

The condition is counter-based, the counter would go from 0 to 9, or from 9 to 0, it's usually easier in assembly to do a conditional jump based on a comparison with zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM