Submission Instruction: You must run the assembly program using the SPIM simulat
ID: 3719923 • Letter: S
Question
Submission Instruction: You must run the assembly program using the SPIM simulator. Please ip the following files: The assembly programs (using file extension.asm) You need to provide the output of the console window and the saved log after the program finishes executing for each of the following programs Submit your zip file via Blackboard 1. Write a program that reads an array of 20 integers with an appropriate prompt, stores it, 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 6Explanation / Answer
As per your requirement I have written code for each and every of your requirements so please follow it step by step and i have included comments for each logic for better understanding
1) Here below code for Printing elements in array with spaces:
# Data section
.data
# Logic to Allocate space for array
arr: .space 100
# Here code snippet for Message to read integer
messageelement1: .asciiz "Enter integer "
# Message to print the array
messageelement2: .asciiz "The array elements are: "
# Used for 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
# Code snippet to Read integers and store to array
readArrayElement:
# Check condition If 20 elements read jump tp print the array
beq $t5,$t1,arrayPrint
# Print messageelement1
la $a0, messageelement1
li $v0, 4
syscall
# Used to Read number
li $v0, 5
syscall
# depends on condition Increment values
addi $t5,$t5,1
addi $s0,$s0,4
# Store value to array
sw $v0, ($a1)
addi $a1, $a1, 4
# Check condition If 20 values not read jump to readArrayElement
j readArrayElement
# Printing array
arrayPrint:
# Load the array base address
la $a1,arr
# Load the message
la $a0, messageelement2
# Used to 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 the loop
end:
j end
2) Below code is for Printing elements with spaces and newline:
# Data section
.data
# First of all Allocate space for array
arr: .space 100
# Message to read integer
messageelement1: .asciiz "Enter integer "
# Here code for Message to print the array
messageelement2: .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 " "
# For better understanding for Print in newline
enter: .asciiz " "
# Text section
.text
.globl main
# Main section
main:
# Used to Load the base address of the array
la $a1, arr
# Initialize with 0
li $s0, 0
li $t5, 0
li $t9,0
# Code is used For reading 20 integers
li $t1,20
# Read integers and store to array
readArrayElement:
# Check condition If 20 elements read jump tp print the array
beq $t5,$t1,arrayPrint
# Print messageelement1
la $a0, messageelement1
li $v0, 4
syscall
# Next 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
# Check condition then take decision If 20 values not read jump to readArrayElement
j readArrayElement
# Printing array
arrayPrint:
# First of all 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, messageelement2
# Later Print the message
li $v0, 4
syscall
# Resultant output so 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:
# Code snippet for Resetting value of s4
li $s4,0
la $a0, enter
li $v0, 4
syscall
j loop
# Termination
end:
j end
3) Below one is code for Printing elements in reverse order:
# Data section
.data
# Allocate space for array
arr: .space 100
# Message to read integer
messageelement1: .asciiz "Enter integer "
# Message to print the array
messageelement2: .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
# First of all Initialize with 0
li $s0, 0
li $t5, 0
# For reading 20 integers
li $t1,20
# Next Read integers and store to array
readArrayElement:
# If 20 elements read jump tp print the array
beq $t5,$t1,arrayPrint
# Print messageelement1
la $a0, messageelement1
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
# check condition If 20 values not read jump to readArrayElement
j readArrayElement
# Printing array
arrayPrint:
# First of all Load the array base address
la $a1,arr
# Load the message
la $a0, messageelement2
# Print the message
li $v0, 4
syscall
# Print each value
write:
# If printing over go to termination
blez $s0, end
# Resultant values so Print those value
li $v0, 1
lw $a0, 76($a1)
syscall
# For better understanding Print the space
la $a0, spaces
li $v0, 4
syscall
addi $a1, $a1, -4
addi $s0, $s0, -4
# Check final condition If all the values not printed loop
j write
# Termination of loop
end:
j end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.