Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads an array of 20 integers with an appropriate prompt, s

ID: 3714048 • Letter: W

Question

Write a program that reads an array of 20 integers with an appropriate prompt, stores it, and then prints in three formats 1. a. b. c. One integer per line; All integers in a single line separated by spaces; All in integers in a single line in the reverse order separated by spaces, Your program also requests entering a positive integer n20 and then prints the 20 integers, n integers per line with separated by space. Please write a separate program for each of the required formats. Test your program with one set of 20 integers and for n-5 and n-6

Explanation / Answer

1) Printing elements in array with spaces:
# Data section
.data
# Allocate space for array
arr: .space 100
# Message to read integer
msg1: .asciiz "Enter integer "
# Message to print the array
msg2: .asciiz "The array elements are: "
# Printing message
spaces: .asciiz " "
  
# Text section
.text
.globl main
# Main section
main:
# Load the base address of the array
la $a1, arr
# Initialize with 0
li $s0, 0
li $t5, 0
# For reading 20 integers
li $t1,20
# Read integers and store to array
readArray:
# If 20 elements read jump tp print the array
beq $t5,$t1,arrayPrint
# Print msg1
la $a0, msg1
li $v0, 4
syscall
# Read number
li $v0, 5
syscall
# Increment values
addi $t5,$t5,1
addi $s0,$s0,4
# Store value to array
sw $v0, ($a1)
addi $a1, $a1, 4
# If 20 values not read jump to readArray
j readArray
# Printing array
arrayPrint:
# Load the array base address
la $a1,arr
# Load the message
la $a0, msg2
# Print the message
li $v0, 4
syscall
# Print each value
write:
# If printing over go to termination
blez $s0, end
# Print the value
li $v0, 1
lw $a0, 0($a1)
syscall
# Print the space
la $a0, spaces
li $v0, 4
syscall
addi $a1, $a1, 4
addi $s0, $s0, -4
# If all the values not printed loop
j write
# Termination
end:
j end

2) Printing elements with spaces and newline:
# Data section
.data
# Allocate space for array
arr: .space 100
# Message to read integer
msg1: .asciiz "Enter integer "
# Message to print the array
msg2: .asciiz "The array elements are: "
# Message for reading values to print per line
msg3: .asciiz "The number of values per line: "
# Printing space
spaces: .asciiz " "
# Printing newline
enter: .asciiz " "
  
# Text section
.text
.globl main
# Main section
main:
# Load the base address of the array
la $a1, arr
# Initialize with 0
li $s0, 0
li $t5, 0
li $t9,0
# For reading 20 integers
li $t1,20
# Read integers and store to array
readArray:
# If 20 elements read jump tp print the array
beq $t5,$t1,arrayPrint
# Print msg1
la $a0, msg1
li $v0, 4
syscall
# Read number
li $v0, 5
syscall
# Increment values
addi $t5,$t5,1
addi $s0,$s0,4
# Store value to array
sw $v0, ($a1)
addi $a1, $a1, 4
# If 20 values not read jump to readArray
j readArray
# Printing array
arrayPrint:
# Load the array base address
la $a1,arr
li $s4,0
# Print msg3
la $a0, msg3
li $v0, 4
syscall
# Read number
li $v0, 5
syscall
move $t8,$v0
# Load the message
la $a0, msg2
# Print the message
li $v0, 4
syscall
# Print each value
write:
# If printing over go to termination
blez $s0, end
# Print the value
li $v0, 1
lw $a0, 0($a1)
syscall
addi $s4,$s4,1
beq $s4,$t8,printNewLine
# Print the space
la $a0, spaces
li $v0, 4
syscall
loop:
addi $a1, $a1, 4
addi $s0, $s0, -4
# If all the values not printed loop
j write  
printNewLine:
# Resetting value of s4
li $s4,0
la $a0, enter
li $v0, 4
syscall
j loop
# Termination
end:
j end
3) Printing elements in reverse order:
# Data section
.data
# Allocate space for array
arr: .space 100
# Message to read integer
msg1: .asciiz "Enter integer "
# Message to print the array
msg2: .asciiz "The array elements are: "
# Printing message
spaces: .asciiz " "
  
# Text section
.text
.globl main
# Main section
main:
# Load the base address of the array
la $a1, arr
# Initialize with 0
li $s0, 0
li $t5, 0
# For reading 20 integers
li $t1,20
# Read integers and store to array
readArray:
# If 20 elements read jump tp print the array
beq $t5,$t1,arrayPrint
# Print msg1
la $a0, msg1
li $v0, 4
syscall
# Read number
li $v0, 5
syscall
# Increment values
addi $t5,$t5,1
addi $s0,$s0,4
# Store value to array
sw $v0, ($a1)
addi $a1, $a1, 4
# If 20 values not read jump to readArray
j readArray
# Printing array
arrayPrint:
# Load the array base address
la $a1,arr
# Load the message
la $a0, msg2
# Print the message
li $v0, 4
syscall
# Print each value
write:
# If printing over go to termination
blez $s0, end
# Print the value
li $v0, 1
lw $a0, 76($a1)
syscall
# Print the space
la $a0, spaces
li $v0, 4
syscall
addi $a1, $a1, -4
addi $s0, $s0, -4
# If all the values not printed loop
j write
# Termination
end:
j end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote