An array is defined as a collection of homogenous elements stored in contiguous memory locations. From this background, you are given eight values obtained from some experiment conducted by a researcher to establish the weights in grams of insects after feeding them:
1.2, 1.4, 1.8, 1.7, 1.0, 1.2, 4.6 and 2.6. Write assembly program to accomplish the following tasks.
1.Calculate and display the total for the weights as recorded from the experiment (8 Marks)
2.Display the weight of second, second last and last weights as observed from the experiment. (7 Marks)
# Description:
# Write a MIPS program in MARS
##########################################################################
.data
total: .asciiz "The total for the weights: "
second: .asciiz "\nSecond: "
secondANDlast: .asciiz "\nSecond last and last weights: "
array: .float 1.2, 1.4, 1.8, 1.7, 1.0, 1.2, 4.6, 2.6
size: .word 8
sum: .float 0.0
.text
.globl main
main:
addi $t0, $zero, 0 # initialize index = 0 in $t0
lw $t1, size
la $s0, array # load the starting address of array into $t2
lwc1 $f0, sum # begining sum
loop:
lwc1 $f1, 0($s0) # Read array[index] from array
add.s $f0, $f0, $f1 # sum
add $s0, $s0, 4
add $t1, $t1, -1
bne $t1, $zero, loop
li $v0,4 # print string
la $a0,total # "The total for the weights: "
syscall
li $v0,2 # print float
mov.s $f12,$f0 # sum
syscall
li $v0,4 # print string
la $a0,second # "second: "
syscall
la $s0, array # load the starting address of array into $t2
lwc1 $f12, 4($s0) # Read array[2-1] from array
li $v0,2 # print
syscall
li $v0,4 # print string
la $a0,secondANDlast # "Second last and last weights: : "
syscall
la $s0, array # load the starting address of array into $t2
lwc1 $f12, 24($s0) # Read array[size-2] from array
li $v0,2 # print
syscall
li $v0,11 # print char
li $a0,' '
syscall
la $s0, array # load the starting address of array into $t2
lwc1 $f12, 28($s0) # Read array[size-1] from array
li $v0,2 # print
syscall
li $v0, 10 # finished .. stop .. return
syscall # make the syscall
Comments
Leave a comment