Code in assembly programming MIPS Write a program that reads
Code in assembly programming **MIPS**
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 in integers in a single line in the reverse order separated by spaces,
- You program requests entering a positive integer n <=20 and then prints the 20 integers, n integers (separated by space) per line.
I AM HALF WAY THROUGH THE PROGRAM WHERE IT CAN ACCPET INTEGERS. CAN YOU PRINT THE ARRAY ELEMENTS THIS AS IN THE QUIESTION?
.data
array: .space 80
newLine:.asciiz \"\ \" # I will use it to start a new line
space: .asciiz \" \" # I will use it to have a space
Prompt: .asciiz \"\ Enter an integer: \"
.globl main
.text
main:
li $t0,20 # $t0 keeps track of the number of integers to be read
la $t1,array # loading the starting address of an array
loopQ:
la $a0,Prompt
li $v0,4
syscall
li $v0,5 # reading an integer
syscall
sw $v0,0($t1) # storing the integer entered
add $t0,$t0,-1 # decrement the number of integers by one
add $t1,$t1,4 # load the address of the next integer
bgtz $t0,loopQ # branch to read and store the next integer
Solution
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
up vote
-1
down vote
.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
up vote
-1
down vote
.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
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





