所以我正在编写一个应该具有以下要求的程序:
编写一个汇编程序,从控制台读取一系列整数。有效条目介于-110和120(含)之间。如果条目无效,则拒绝该条目并在屏幕上显示错误消息。输入-467时读取停止。输入-467后,程序仅使用MIPS整数命令显示条目数,条目总数和5个小数位的平均值。将至少有3个子程序。第一个'read_int',它不接收任何参数,并返回在适当的寄存器中读取的有效数字的总数和计数。第二个'print_count_total',它在第一个子程序返回的参数中作为参数接收,并且没有参数。它将正确标记的计数和总数输出到屏幕。第三个'print_average',它在第一个子程序返回的参数中作为参数接收,并且没有参数。它仅使用整数命令将读取的数字的平均值输出到5位小数。
我有一切正常,但我怎样才能将整数平均值打印到五位小数?例如:
整数平均值应打印为71. 25000 。
这是我的代码:
###########################################################
# Program Description
#
#
# Reads integers from the keyboard and displays the total
# number of entries, sum, and integer average
# Reads until -1 is entered
# Only accepts values between 10 and 99 (inclusive)
#
#
# t0 = 0 # Total
# t1 = 0 # Count
# t2 = -110 # Min
# t3 = 120 # Max
# t4 = -467 # Stop
#
###########################################################
# Register Usage
# $t0 Running total
# $t1 Entry count
# $t2 Minimum value
# $t3 Maximum value
# $t4 Stop value
# $t5 Entry (from user)
###########################################################
.data
enter_p: .asciiz "Enter a value between -110 and 120 (inclusive): "
invalid_p: .asciiz "Invalid value\n\n"
count_p: .asciiz "\n\nNumber of entries: "
total_p: .asciiz "\nTotal: "
average_p: .asciiz "\nInteger average: "
noentry_p: .asciiz "\n\nNo valid values entered\n"
###########################################################
.text
main:
# Initialize values
li $t0, 0 # Total
li $t1, 0 # Count
li $t2, -110 # Min value
li $t3, 120 # Max value
li $t4, -467 # Stop value
readLoop:
# Get value
li $v0, 4
la $a0, enter_p
syscall # Prompt for value
li $v0, 5
syscall # Read entry
# Validate value
beq $v0, $t4, getResults
blt $v0, $t2, entryInvalid
bgt $v0, $t3, entryInvalid
# If program gets here, the entry is valid
add $t0, $t0, $v0 # Add entry to total
addiu $t1, $t1, 1 # Increment counter
b readLoop # Loop
entryInvalid:
li $v0, 4
la $a0, invalid_p
syscall # Print error message
b readLoop # Go back and read more
getResults:
beqz $t1, zeroEntries # Prevent divide by 0
# Display number of entries
li $v0, 4
la $a0, count_p
syscall # Print 'count' string
li $v0, 1
move $a0, $t1
syscall # Print count
# Display total
li $v0, 4
la $a0, total_p
syscall # Print 'total' string
li $v0, 1
move $a0, $t0
syscall # Print total
# Calculate and display average
li $v0, 4
la $a0, average_p
syscall # Print 'average' string
li $v0, 1
div $a0, $t0, $t1 # Put results directly into $a0
syscall # Print average
b mainEnd # Done
zeroEntries:
li $v0, 4
la $a0, noentry_p
syscall # Print 'no entries' message
mainEnd:
li $v0, 10 # End Program
syscall
###########################################################
非常感谢任何帮助。
答案 0 :(得分:2)
要打印小数部分,将除数余数乘以10 ^ 5 = 100000并再次除。在小数点后打印,注意使用适当的零填充量。