Reputation: 1
Objective: Using user input, find the average value using five registers.
Example: 1 3 2 9 4
Output: 3
Output is 3 because 3.8 is a decimal and I want it in integers
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
the part where I got my error
# add all integers to $t5
add $a0, $t0, $t1, $t2, $t3, $t4
li $v0, 1
syscall
continued...
# 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
I get an error message
spim:(parser) syntax error on line 71 of ... 'add $a0, $t0, $t1, $t2, $t3, $t4
Upvotes: 0
Views: 3168
Reputation: 27
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
Upvotes: 1
Reputation: 365267
Does that mean, I have to use arrays to add multiple integers in MIPS?
No, you can keep everything in registers, but you have to use a separate add
(or addu
) instruction for every +
operator in your a+b+c+d+e expression.
The add
instruction has exactly two inputs, and one output.
Basic MIPS instruction-set reference.
Upvotes: 0