Tuesday, May 12, 2020

Print the average of integers in float format

This is a sample MIPS program that asks the user to enter as many integer numbers as he wishes, calculates their average and prints it in float format. The code is fully documented.

Launch EzMIPSthe MIPS assembler simulator, copy the following MIPS code and paste it into EzMIPS. Assemble, Run.

# ---------------------------------------------------------------- #

# 1. User is asked to enter an integer number
# 2. if number inputted = 0, print how many numbers the User entered and their average
# 3. else, goto 1

.data
szEnterNumber:        .asciiz "Enter a Number (0 to exit): "
szTheSumIs:           .asciiz "\nThe sum is: "
szNumbersWereEntered: .asciiz " numbers were entered\n"
szLF:                 .asciiz "\n"
szTheAverageIs:       .asciiz "\nThe average is: "
szDecimalPoint:       .asciiz "."

# ................................................................ #

.text


main:
    nop

# ................................................................ #

# $t0 = counts how many numbers the user inputted
# $t1 = holds the sum of the numbers entered

start:                        # Loops until the user inputs a zero

    la $a0, szEnterNumber        # Loads the address of szEnterNumber into $a0
    li $v0, 4                    # System call code to print a string
    syscall                      # Print string
    
    li $v0, 5                    # System call code that reads an integer
    syscall                      # Reads the integer that the user had inputted
                                 # and puts it into $v0

    addi $t0, $t0, 1             # Increment the integer counter by 1
    add $t1, $t1, $v0            # Add the recently inputted integer to the current sum

    bne $v0, $zero, start        # Branch if the integer not equals zero

    addi $t0, $t0, -1            # since user inputted a 0,
                                 # numbers_entered = numbers_entered -1

# ..... User inputted a 0, calulate and print the results ........ #

    la $a0, szTheSumIs           # Loads the address of szTheSumIs into $a0
    li $v0, 4                    # System call code to print a string
    syscall                      # Print sum_mess.
    
    move $a0, $t1                # Moves the sum in $t1 into the argument register
    li $v0, 1                    # System call code to print an integer
    syscall                      # Prints the sum

    la $a0, szLF                 # Loads the address of szLF into the argument register
    li $v0, 4                    # System call code to print a string
    syscall                      # Print new line
    
    move $a0, $t0                # Moves the number of integers in $t0 into $a0
    li $v0, 1                    # System call code to print an integer
    syscall                      # Prints the number of integers that were inputted

    la $a0, szNumbersWereEntered # Loads the address of szNumbersWereEntered into $a0
    li $v0, 4                    # System call code to print a string
    syscall                      # Prints szNumbersWereEntered
    
    la $a0, szTheAverageIs       # Loads the address of szTheAverageIs into $a0
    li $v0, 4                    # System call code to print a string
    syscall                      # Prints szTheAverageIs
    
    jal average                  # Jump and link to average
    
    move $a0, $t2                # Move the value of $t2 into $a0
    li $v0, 1                    # System call code to print an integer
    syscall                      # Print the integer
    
    la $a0, szDecimalPoint       # Loads the address of szDecimalPoint into $a0
    li $v0, 4                    # System call code to print a string
    syscall                      # Prints szDecimalPoint
    
    move $a0, $t5                # Move the value of $t5 (the decimal value) to $a0
    li $v0, 1                    # System call code to print an integer
    syscall                      # Print the decimal value

# ................................................................ #

exit:    

    li $v0, 10                   # System call code to exit the program
    syscall                      # Exit the program

# .................. Calculate the average ....................... #

average:

    div $t1, $t0                 # Divide the sum in $t1 by $t0 (numbers_entered)
    mflo $t2                     # Store the quotient in $t2
    mfhi $t3                     # Store the remainder in $t3

    # ......... Convert remainder into decimal part .............. #
    li $t4, 1000                 # Stores the immediate value 1000 in $t4
    mult $t4, $t3                # Multiply the remainder by 1000
    mflo $t5                     # Store the product in $t5
    div $t5, $t0                 # Divide $t5 by the number of integers 
    mflo $t5                     # and store the result in $t5
    # ......... Convert remainder into decimal part .............. #
    
    jr $ra                       # Return to the caller

# ................................................................ #

Please let me know of any suggestions or bugs regarding the code above.

Regards,

Antonis

No comments:

Post a Comment