#MIPS ASSEMBLY PROGRAM Write a program that reads an array of 20 integers with a
ID: 3586772 • Letter: #
Question
#MIPS ASSEMBLY PROGRAM
Write a program that reads an array of 20 integers with an appropriate prompt, stores it, and then prints in three formats:
- One integer per line
- All integers in a single line separated by spaces
- All integers in a single line in the reverse order separated by spaces
- Your program requests entering a positive integer n <= 20 and then prints the 20 integers, n integers (separated by space) per line.
Test your program for one set of 20 integers and for n = 5 and n = 6.
Explanation / Answer
CODE
ONE INTEGER PER LINE
.data
array: .space 80 #array will hold 20 integers
newSpace: .asciiz " " #use for adding a space
prompt: .asciiz " Please enter 20 integers one at a time: "
.text
main: li $t0, 4 #$t0 to jeep track of num of integers
la $t1, array #load the starting address of array
la $a1, prompt
syscall
loopP: li $v0, 5 #reading an integer
syscall
sw $v0, 0($t1) #store the integer entered
add $t0, $t0, -1 #decrement num of integers by 1
add $t1, $t1, 4 #load address of next integer
bgtz $t0, loopP #branch to read and store next integer
li $t0, 4
la $t1, array
loopQ: lw $a0, 0($t1) # load integer from memory location to $a0
li $v0, 1
syscall #print the integer
add $t0, $t0, -1
add $t1, $t1, 4
la $a0, space #add a space
li $v0, 4
syscall
bgtz $t0, loopQ
li $v0, 10 #terminate program run
syscall
b)all integers in a single line separated by spaces
.data
arr: .space 32
msg1: .asciiz "put in first integer"
msg2: .asciiz "put in second integer"
msg3: .asciiz "put in third integer"
msg4: .asciiz "put in fourth integer"
msg5: .asciiz "put in fifth integer"
msg20: .asciiz "put in twenty integer"
.globl main
.text
main:
la $s0, arr
li $v0, 4
la $a0, msg1
syscall
li $v0, 5
syscall
sw $v0, 28($s0)
li $v0, 4
la $a0, msg2
syscall
li $v0, 5
syscall
sw $v0, 24($s0)
li $v0, 4
la $a0, msg3
syscall
li $v0, 5
syscall
sw $v0, 20($s0)
li $v0, 4
la $a0, msg4
syscall
li $v0, 5
syscall
sw $v0, 16($s0)
li $v0, 4
la $a0, msg5
syscall
li $v0, 5
syscall
sw $v0, 12($s0)
li $v0, 4
la $a0, msg6
syscall
li $v0, 5
syscall
sw $v0, 8($s0)
li $v0, 4
la $a0, msg7
syscall
li $v0, 5
syscall
sw $v0, 4($s0)
li $v0, 4
la $a0, msg8
syscall
li $v0, 5
syscall
sw $v0, 0($s0)
till 20 msgs
li $v0, 1
lw $a0,0($s0)
syscall
li $v0, 1
lw $a0,4($s0)
syscall
li $v0, 1
lw $a0,8($s0)
syscall
li $v0, 1
lw $a0,12($s0)
syscall
li $v0, 1
lw $a0,16($s0)
syscall
li $v0, 1
lw $a0,20($s0)
syscall
li $v0, 1
lw $a0,24($s0)
syscall
li $v0, 1
lw $a0,28($s0)
syscall
li $v0, 10#terminate execution
syscall
c)all integers in a single line in reverse order separated by spaces
.data
arr: .space 32
msg1: .asciiz "put in first integer"
msg2: .asciiz "put in second integer"
msg3: .asciiz "put in third integer"
msg4: .asciiz "put in fourth integer"
msg5: .asciiz "put in fifth integer"
msg20: .asciiz "put in twenty integer"
.globl main
.text
main:
la $s0, arr
li $v0, 4
la $a0, msg1
syscall
li $v0, 5
syscall
sw $v0, 28($s0)
li $v0, 4
la $a0, msg2
syscall
li $v0, 5
syscall
sw $v0, 24($s0)
li $v0, 4
la $a0, msg3
syscall
li $v0, 5
syscall
sw $v0, 20($s0)
li $v0, 4
la $a0, msg4
syscall
li $v0, 5
syscall
sw $v0, 16($s0)
li $v0, 4
la $a0, msg5
syscall
li $v0, 5
syscall
sw $v0, 12($s0)
li $v0, 4
la $a0, msg6
syscall
li $v0, 5
syscall
sw $v0, 8($s0)
li $v0, 4
la $a0, msg7
syscall
li $v0, 5
syscall
sw $v0, 4($s0)
li $v0, 4
la $a0, msg8
syscall
li $v0, 5
syscall
sw $v0, 0($s0)
till 20 msgs
li $v0, 1
lw $a0,0($s0)
syscall
li $v0, 1
lw $a0,4($s0)
syscall
li $v0, 1
lw $a0,8($s0)
syscall
li $v0, 1
lw $a0,12($s0)
syscall
li $v0, 1
lw $a0,16($s0)
syscall
li $v0, 1
lw $a0,20($s0)
syscall
li $v0, 1
lw $a0,24($s0)
syscall
li $v0, 1
lw $a0,28($s0)
syscall
li $v0, 10#terminate execution
syscall
D) prints n integers per line
.data
array: .space 80 #array will hold 20 integers
newSpace: .asciiz " " #use for adding a space
prompt: .asciiz " Please enter 20 integers one at a time: "
.text
main: li $t0, 4 #$t0 to jeep track of num of integers
la $t1, array #load the starting address of array
la $a1, prompt
syscall
loopP: li $v0, 5 #reading an integer
syscall
sw $v0, 0($t1) #store the integer entered
add $t0, $t0, -1 #decrement num of integers by 1
add $t1, $t1, 4 #load address of next integer
bgtz $t0, loopP #branch to read and store next integer
li $t0, 4
la $t1, array
loopQ: lw $a0, 0($t1) # load integer from memory location to $a0
li $v0, 1
syscall #print the integer
add $t0, $t0, -1
add $t1, $t1, 4
la $a0, space #add a space
li $v0, 4
syscall
bgtz $t0, loopQ
li $v0, 10 #terminate program run
syscall
IF ANY QUERIES PLEASE GET BACK TO ME
THANK YOU
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.