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

Assignment Description: An array of integers can be assigned to a memory address

ID: 3876115 • Letter: A

Question

Assignment Description:

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:

# MIPS program to sort numbers based on user input

.data # Data segment

numbers_len: .word 12

numbers: .word -42, 11, 24, 3, -6, 14, -18, 45, 12, -27, 35, -28

num: .space 4

prompt1: .asciiz "Enter an Integer: "

nl: .asciiz " "

# Main segment

.globl main

.text

main:

# prompting user for input

la $a0, prompt1

li $v0, 4

syscall

# reading Integer from user

li $v0, 5

syscall

sw $v0, num

# initializing temporary variables for iteration

# NOTE: temp variables will be increment by 4 (bytes) on purpose, to point to next memory location (element of array)

li $t0, -4

li $t1, 0

sort:

# load required numbers into registers

lw $t2, numbers($t1)

lw $t3, num

lw $t5, numbers_len

mul $t5, $t5, 4

# compare the number in array with user input. Skip if greater.

bge $t2, $t3, skip

# sort if lesser

# iterating to first location

add $t0, $t0, 4

# swapping numbers

lw $t4, numbers($t0)

sw $t2, numbers($t0)

sw $t4, numbers($t1)

skip:

# incrementing loop variable

add $t1, $t1, 4

# iterating through array

bne $t1, $t5, sort

# initializing temp variable to iterate and print

li $t0, 0

print:

# load element of array and print

lw $a0, numbers($t0)

li $v0, 1

syscall

# print new line

la $a0, nl

li $v0, 4

syscall

# increment pointer and iterate

add $t0, $t0, 4

bne $t0, $t5, print

# exit the program

li $v0, 10

syscall

OUTPUT:

Enter an Integer:
5
-42
3
-6
-18
-27
-28
11
45
12
24
35
14

-- program is finished running --


Reset: reset completed.

Enter an Integer:
-20
-42
-27
-28
3
-6
14
-18
45
12
11
35
24

-- program is finished running --

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