MIPS:找到5个整数的平均值

时间:2018-01-18 05:36:08

标签: assembly mips

目标:使用用户输入,使用五个寄存器查找平均值。

示例: 1 3 2 9 4

输出 3

输出为3,因为3.8是小数,我想用整数

我的目标是不使用数组,但我在添加所有5个寄存器时遇到问题:

.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 :(解析器)语法错误在第71行...'添加$ a0,$ t0,$ t1,$ t2,   $ t3​​,$ t4

2 个答案:

答案 0 :(得分:1)

您可以使用此代码添加 5 个整数,这是您代码的临时版本。

.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

答案 1 :(得分:-1)

  

这是否意味着,我必须使用数组在MIPS中添加多个整数?

不,您可以将所有内容保存在寄存器中,但是您必须为a + b + c + d中的每个add运算符使用单独的addu(或+)指令+ e表达。

add指令只有两个输入和一个输出。

基本MIPS instruction-set reference