##################################################################### # Program #5: Balance # Programmer: Chris Brakebill # Due Date: Nov. 30, 2009 Course: ECE 355 # Last Modified: Nov. 30, 2009 # ##################################################################### # Functional Description: # Calculates the balance in a checking account using the previous # balance and a series of transactions # Input: Requests a previous balance and a series of transactions, all # of which are floating point numbers # Output: Outputs the the final balance of the account # ##################################################################### # Pseudocode: # Prompt for previous account balance # Read balance as single precision float # Start input loop # Prompt for transaction value # Read in the value as a float # If the value = 0, exit the loop # Otherwise, put it onto stack and increment transaction count # Put the initial balance in $f12 register, and transaction count in $a0 # Pop each value off the stack and add it to the balance # Print the final balance # Exit ###################################################################### # Register Usage: # $t0 - contains the transaction count # $f5 - holds the current balance # $f7 - holds the next transaction value # $f20 - holds the initial balance # #################### Data segment #################################### .data balance_prompt: .asciiz "Please enter a starting account balance: \n" trans_prompt: .asciiz "Please enter a transaction amount: \n" out_msg: .asciiz "\nThe new checkbook balance is: " ################### Code segment ##################### .text .globl main main: la $a0,balance_prompt # prompt user for starting balance li $v0,4 syscall li $v0,6 # read in opening balance syscall mov.s $f20, $f0 addiu $sp, $sp, -4 # Store the return address on the stack sw $ra, 0($sp) addiu $t0, $0, 0 # Make sure that $t0 is cleared before this loop mtc1 $0, $f4 # Move the value of zero to $f4 register cvt.s.w $f21, $f4 # Convert zero value to a float and put in $f21 jal read_loop mov.s $f12, $f20 # Move the balance to $f0 to pass to the balance function move $a0, $t0 # Move the transaction count to $a0 to pass to the function jal balance lw $ra, 0($sp) # Restore the return address addiu $sp, $sp, 4 la $a0,out_msg # write output message li $v0,4 syscall mov.s $f12, $f0 # output the final balance li $v0, 2 syscall li $v0, 10 # exit the program syscall read_loop: la $a0,trans_prompt # prompt user for transaction value li $v0,4 syscall li $v0, 6 # read in the next transaction value syscall c.eq.s $f0, $f21 # If the input value is equal to zero, exit the loop bc1t loop_finish addiu $sp, $sp, -4 # Make room on the stack for the transaction value swc1 $f0, 0($sp) # and store it addi $t0, $t0, 1 # increment the counter j read_loop # Restart the loop loop_finish: # if it's a match, return to search function jr $ra balance: mov.s $f5, $f12 # Move the balance to another register addiu $t0, $0, 0 # Make sure $t0 has 0 in it j sum_loop sum_loop: beq $t0, $a0, finish_sum # If $t0 = $a0, we've read all the values, so we need to return lwc1 $f7, 0($sp) # Load the next value off the stack add.s $f5, $f5, $f7 # Add it to the current balance addiu $t0, $t0, 1 # Increment the counter addiu $sp, $sp, 4 # Increment the stack j sum_loop finish_sum: mov.s $f0, $f5 # Move the balance to $f0 for returning it jr $ra