An array of integers can be assigned to a memory address in the .data section of
ID: 3876204 • Letter: A
Question
An array of integers can be assigned to a memory address in the .data section of a MIPS assembly language program as show below. Here the length of the array is stored first, and then the elements of the array numbers next. You are given the following C program that will ask a user to enter one integer and it will filter all integers in the array into the ones that are less than or equals to the entered integer and the ones that are greater. Implement a MIPS assembly language program to perform the functionality of the following C program and print the updated array content, by listing each integer in it.
For instance, if a user enters 5, then the output will be the following:
-42
3
-6
-18
-27
-28
11
45
12
24
35
14
i.e., the number that are less than 5,
(-42, 3, -6, -18, -27, -28) are swapped so that they are located towards the beginning of the array,
and the number that are greater than 5,
(11, 45, 12, 24, 35, 14) are located towards the end of the array.
If your program causes an infinite loop, press Control and 'C' keys at the same time to stop it. Name your source code file assignment5.s.
.data
numbers_len: .word 12
numbers: .word -42, 11, 24, 3, -6, 14, -18, 45, 12, -27, 35, -28
The following shows how it looks like in a C program:
The following is a sample output (user input is in bold):
Enter an integer:
5
-42
3
-6
-18
-27
-28
11
45
12
24
35
14
--------------------------------------------------
The following is another sample output:
--------------------------------------------------
Enter an integer:
-20
-42
-27
-28
3
-6
14
-18
45
12
11
35
24
--------------------------------------------------
Explanation / Answer
CODE
#data section
.data
numbers_len: .word 12
numbers: .word -42, 11, 24, 3, -6, 14, -18, 45, 12, -27, 35, -28
#declare strings
msg1: .asciiz "Enter an integer: "
newLine: .asciiz " "
#Text section
.text
main:
#display prompt
la $a0, msg1
li $v0, 4
syscall
#Read integer
li $v0, 5
syscall
move $t0, $v0
#load values
lw $a0, numbers_len
la $a1, numbers
li $t1, 0
li $t2, 0
#loop to arrange array
loop:
#Check condition
beq $t1, $a0, PrintLoop
#Load current array value
lw $t3, 0($a1)
#Check array value < user value
#then go to swap
blt $t3, $t0, swap
#if not, move to next array element
addi $t1, $t1, 1
addi $a1, $a1, 4
#jump to loop
j loop
#swap section
swap:
#Get the index
add $t5, $t2, $t2
add $t5, $t5, $t5
#Load array base address
la $t4, numbers
#Find the correct position
add $t6, $t4, $t5
#Take the element from the position
lw $t7, 0($t6)
#Insert the element present in $t3
sw $t3, 0($t6)
#Set the values
li $t5, 0
li $t6, 0
#Find the index
add $t5, $t1, $t1
add $t5, $t5, $t5
#Find the address
add $t6, $t4, $t5
#place element
sw $t7, 0($t6)
#Increase $t1
addi $t1, $t1,1
#Increase $t2
addi $t2, $t2, 1
#Move to next array element
addi $a1, $a1, 4
#jump to loop
j loop
#prints the array
PrintLoop:
#Load the base array address
la $a1, numbers
#Load number of elements
lw $t0, numbers_len
#Set $t1 to 0
li $t1, 0
#loop
loop2:
#Check condition
beq $t1, $t0, ExitPro
#Load current array element
lw $a0, 0($a1)
#Print array element
li $v0,1
syscall
#Print newline
la $a0, newLine
li $v0, 4
syscall
#move to next array element
addi $a1, $a1, 4
addi $t1, $t1, 1
#jump to loop2
j loop2
#End program
ExitPro:
li $v0, 10
syscall
WHEN WE RUNNED THE PROGRAM THE DESIRED OUTPUT CAN GET
THANK YOU
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.