Question: Assmbly language Write an assembly program that asks for the number of integers to be read. It then dynamically allocates an array of that size.
Assmbly language
Write an assembly program that asks for the number of integers to be read. It then dynamically allocates an array of that size. It will stop reading integers when the array is full. After all the integers are read and validated, the program displays the entries in reverse order, the sum of the entries, and the average of the entries to 6 decimal places using only MIPS Integer commands. There will be at least 4 subprograms.
The first subprogram allocate_array will ask the user for the number of integers to be read and dynamically declares an array to hold them. It receives no arguments IN and has two arguments OUT, the base address of the dynamically declared array and it size. Do not forget to validate array size (array size should be greater than zero).
The second subprogram read_values which receives two arguments IN, the array base address and its size. Also, it makes sure entered numbers are between -100 and +100 inclusive. It returns (OUT) the sum of valid numbers read in the appropriate registers.
The third subprogram print_backwards receives as arguments IN the base address of the array and its size. It has no arguments OUT. It outputs the array in reverse order one number per line at a time.
The fourth subprogram print_average which receives as arguments IN the total and count and has no arguments OUT. It outputs the average of the numbers read to 6 decimal places using only integer commands.
In main, you will call all 4 subprograms and declare and use static variables to hold the base address of the array, count of element, and the sum of values of the elements.
program1
.data prompt_p: .asciiz "Enter a non-negative integer (greater than or equal to zero) that are odd: " invalid_p: .asciiz "Invalid entry. Number should be odd and non-negative. " sum_p: .asciiz "Sum is: " count_p: .asciiz "Count is: " average_p: .asciiz "Average is: " ########################################################### .text main: li $t0, 0 # initialize sum to zero li $t1, 0 # initialize count to zero li $t7, -7 # initialize $t7 to constant value -7 li $t8, 10 # initialize $t8 to constant value 10 li $t9, 2 # initialize $t9 to constant value 2 entry_loop: li $v0, 4 # prompt for number la $a0, prompt_p syscall
li $v0, 5 # read integer syscall
beq $t1, $t8, calculation # branch to calculation if count value is 10 beq $v0, $t7, calculation # branch to calculation if value is -7 bltz $v0, invalid_entry # branch to invalid_entry if value is less than zero rem $t6, $v0, $t9 # divide the number by 2 and put entry into register $t6 beqz $t6, invalid_entry # branch to invalid_entry if value is even add $t0, $t0, $v0 # add the number to the sum addi $t1, $t1, 1 # increment count by 1
b entry_loop # branch unconditionally to the beginning of the loop
invalid_entry: li $v0, 4 # print error message la $a0, invalid_p syscall
b entry_loop # branch unconditionally to entry_loop
calculation: li $v0, 4 # print sum is la $a0, sum_p syscall
li $v0, 1 # print sum integer move $a0, $t0 syscall
li $v0, 11 # print newline character li $a0, 13 syscall
li $v0, 4 # print count is la $a0, count_p syscall
li $v0, 1 # print count integer move $a0, $t1 syscall
li $v0, 11 # print newline character li $a0, 13 syscall
li $v0, 4 # print average is la $a0, average_p syscall
beqz $t1, divide_by_zero # if count is zero, we should not divide sum by zero li $v0, 1 # print quotient div $a0, $t0, $t1 # divide sum by count and put quotient in register $a0 syscall
b exit # branch unconditionally to halt
divide_by_zero: li $v0, 1 # print zero move $a0, $0 syscall
exit: li $v0, 10 # halt syscall ###########################################################
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
