MIPS:求 5 个整数的平均值 [英] MIPS: Finding average of 5 integers
本文介绍了MIPS:求 5 个整数的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目标:使用用户输入,使用五个寄存器找到平均值.
Objective: Using user input, find the average value using five registers.
示例: 1 3 2 9 4
输出: 3
输出是 3,因为 3.8 是小数,我想要整数
Output is 3 because 3.8 is a decimal and I want it in integers
我的目标是不使用数组,但我在添加所有 5 个寄存器时遇到问题:
My goal is to not use array but I am having trouble adding all 5 registers:
.data
prompt1: .asciiz " Please enter an integer: "
prompt2: .asciiz " Please enter an integer: "
prompt3: .asciiz " Please enter an integer: "
prompt4: .asciiz " Please enter an integer: "
prompt5: .asciiz " Please enter an integer: "
result: .asciiz "The average is: "
.text
main:
#t0 - hold prompt1
#t1 - hold prompt2
#t2 - hold prompt3
#t3 - hold prompt4
#t4 - hold prompt5
#t5 - hold sum from prompt1 to prompt5
#t6 - hold the average value
# Prompt the user to enter integer1.
li $v0, 4 # syscall to print string
la $a0, prompt1
syscall
# Read integer1
li $v0, 5 # syscall to read an integer
syscall
move $t0, $v0 # move number to read into $t0
# Prompt the user to enter integer 2.
li $v0, 4
la $a0, prompt2
syscall
# Read integer2
li $v0,5
syscall
move $t1, $v0
# Prompt the user to enter integer 3.
li $v0, 4
la $a0, prompt3
syscall
# Read integer3
li $v0,5
syscall
move $t2, $v0
# Prompt the user to enter integer 4.
li $v0, 4
la $a0, prompt4
syscall
# Read integer4
li $v0,5
syscall
move $t3, $v0
# Prompt the user to enter integer 5.
li $v0, 4
la $a0, prompt5
syscall
# Read integer5
li $v0,5
syscall
move $t4, $v0
出现错误的部分
# add all integers to $t5
add $a0, $t0, $t1, $t2, $t3, $t4
li $v0, 1
syscall
继续...
# Read the sum
li $v0, 5
syscall
move $t5, $v0
# Divide Sum / count
div $t5, 5
# retrieve it
mflo $t6
#print out the average
move $a0, t6
li $v0, 1
la $a0, result
syscall
exit:
li $v0, 10
syscall
我收到一条错误消息
spim:(parser) 语法错误在第 71 行 of ... 'add $a0, $t0, $t1, $t2,$t3, $t4
spim:(parser) syntax error on line 71 of ... 'add $a0, $t0, $t1, $t2, $t3, $t4
推荐答案
您可以使用此代码添加 5 个整数,这是您代码的临时版本.
You can use this code to add 5 integers, this is an improvised version of your code.
.data
prompt1: .asciiz " Please enter an integer: "
prompt2: .asciiz " Please enter an integer: "
prompt3: .asciiz " Please enter an integer: "
prompt4: .asciiz " Please enter an integer: "
prompt5: .asciiz " Please enter an integer: "
result: .asciiz " The average is: "
.text
main:
#t0 - hold prompt1
#t1 - hold prompt2
#t2 - hold prompt3
#t3 - hold prompt4
#t4 - hold prompt5
#t5 - hold sum from prompt1 to prompt5
#t6 - hold the average value
# Prompt the user to enter integer1.
li $v0, 4 # syscall to print string
la $a0, prompt1
syscall
# Read integer1
li $v0, 5 # syscall to read an integer
syscall
move $t0, $v0 # move number to read into $t0
# Prompt the user to enter integer 2.
li $v0, 4
la $a0, prompt2
syscall
# Read integer2
li $v0,5
syscall
move $t1, $v0
# Prompt the user to enter integer 3.
li $v0, 4
la $a0, prompt3
syscall
# Read integer3
li $v0,5
syscall
move $t2, $v0
# Prompt the user to enter integer 4.
li $v0, 4
la $a0, prompt4
syscall
# Read integer4
li $v0,5
syscall
move $t3, $v0
# Prompt the user to enter integer 5.
li $v0, 4
la $a0, prompt5
syscall
# Read integer5
li $v0,5
syscall
move $t4, $v0
# add all integers to $t5
add $t5, $t0, $t1
add $t5, $t5, $t2
add $t5, $t5, $t3
add $t5, $t5, $t4
li $t6,5
# Divide Sum / count
div $t5, $t5, $t6
#print out the average
li $v0, 4
la $a0, result
syscall
move $a0, $t5
li $v0, 1 #prints average value
syscall
exit:
li $v0, 10
syscall
这篇关于MIPS:求 5 个整数的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文