Take 5 integer numbers as input from the user on a run time base and calculate the average using MIPS.
Sorry, I attached the wrong picture
#1. read n1 n2 n3 n4 n5
#2. avg
# MIPS assembly
#************************************************
.data
msgEnd: .asciiz "\n...Finish"
msgEnterN: .asciiz "Enter N"
msgAnswer: .asciiz "\nAVG: "
five: .float 5.0
# code segment
.text
.globl main
main:
li $v0,4 # print string
la $a0,msgEnterN # "Enter N"
syscall # make the syscall print string
li $v0,11 # print char
li $a0,'1' # 1
syscall
li $v0,11 # print char
li $a0,' ' # space
syscall
#reads and store the n1
li $v0, 5 # read int n1 to $v0
syscall # make the syscall read integer
move $t1, $v0
li $v0,4 # print string
la $a0,msgEnterN # "Enter N"
syscall # make the syscall print string
li $v0,11 # print char
li $a0,'2' # 2
syscall
li $v0,11 # print char
li $a0,' ' # space
syscall
#reads and store the n2
li $v0, 5 # read int n2 to $v0
syscall # make the syscall read integer
move $t2, $v0
li $v0,4 # print string
la $a0,msgEnterN # "Enter N"
syscall # make the syscall print string
li $v0,11 # print char
li $a0,'3' # 3
syscall
li $v0,11 # print char
li $a0,' ' # space
syscall
#reads and store the n3
li $v0, 5 # read int n3 to $v0
syscall # make the syscall read integer
move $t3, $v0
li $v0,4 # print string
la $a0,msgEnterN # "Enter N"
syscall # make the syscall print string
li $v0,11 # print char
li $a0,'4' # 4
syscall
li $v0,11 # print char
li $a0,' ' # space
syscall
#reads and store the n4
li $v0, 5 # read int n4 to $v0
syscall # make the syscall read integer
move $t4, $v0
li $v0,4 # print string
la $a0,msgEnterN # "Enter N"
syscall # make the syscall print string
li $v0,11 # print char
li $a0,'5' # 5
syscall
li $v0,11 # print char
li $a0,' ' # space
syscall
#reads and store the n5
li $v0, 5 # read int n5 to $v0
syscall # make the syscall read integer
move $t5, $v0
add $t0, $t1, $t2
add $t0, $t0, $t3
add $t0, $t0, $t4
add $t0, $t0, $t5 # n1+n2+n3+n4+n5
mtc1 $t0, $f0 # copy $t0 to $f0
cvt.s.w $f2, $f0 # convert int $f0 to float $f2
#la $t0, five
lwc1 $f0, five # $f0 = 5.0
div.s $f12, $f2, $f0
li $v0,4 # print string
la $a0,msgAnswer # "Add: "
syscall # make the syscall print string
li $v0,2 # Print float
syscall # make the syscall print int
finish:
li $v0,4 # print string
la $a0,msgEnd # "Finish"
syscall # make the syscall
li $v0, 10 # finished .. stop .. return
syscall
Comments
Leave a comment