Assembly Language: Write an MIPS program with main(), init(), and avg(). The mai
ID: 3749012 • Letter: A
Question
Assembly Language:
Write an MIPS program with main(), init(), and avg(). The main() calls functions init() and avg(), and prints the value returned from avg(). Function main() calls functions init() and avg(). A function call to init() for example in MIPS is by
jal init
and return from a function is by
jr $ra
Function init() initializes a 4-integer array A with arbitrary integers. The array A is global, and thus does not need to passed as an argument to init().
Function avg() computes the average of values in array A (array A is global), and returns the computed average value via $a0 register.
Function main() prints the returned average value in $a0.
Explanation / Answer
.data
array: .space 16 #4 element integer array
mess: .asciiz " Enter 4 numbers to be stored in the array. "
.globl main
.text
main:
la $t1, array
la $a0, 0
jal read
b done
read:
la $t0, 0 #count variable
b Init
jr $ra
Init:
beq $t0, 16, read #branch if equal to 16, 4 items
li $v0, 4 #Print string
la $a0, mess #load prompt
syscall
li $v0, 5 #read int
syscall
sw $v0, $t0($t1) #store input in array
addi $t0, $t0, 4 #add by 4 to count
b Init
jal Avg
read1:
la $t0, 0 #count variable
b Avg
jr $ra
Avg:
beq $t0, 16, read1 #looping 0,4,8,12
div $t0(t1), 4 #dividing each element in each turn
addi $a0, $a0, $LO #adding the quotient in and rest of the average value
addi $t0, $t0, 4 #increasing the count value
jr $ra
done:
li $v0,1
lw $a0, $a0
syscall
This is not the complete code but it should help you in solving the question
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.