Odd/Even Number Identification and Sorting in MIPS Write a program in MIPS assem
ID: 671111 • Letter: O
Question
Odd/Even Number Identification and Sorting in MIPS
Write a program in MIPS assembly language that takes a set of signed 32-bit integers (words) as the input, classifies them into odd and even numbers, and then sorts the odd numbers and even numbers, respectively.
The input set integers are read from the console and the output sorted numbers are also in displayed in the console. For the input, a “special value” 9999 will be used to signify the end of the input sequence and this value is not to be considered part of the input data set. However, any value greater than 9999 that is entered prior to 9999 is considered as a valid input. Zero and negative values are also valid. All odd-number sets and all even-number sets are also valid.
The output sorting result should be in ascending order. For example,
With an input:
23, 34, 2, 57, -1, 64, 71 (9999)
the outputs should be
(Sorted even numbers) 2, 34, 64
(Sorted odd numbers) -1, 23, 57, 71
The input can be read in from the console one by one (with “ENTER” key after each input number) until “9999” is entered. The input set cannot be empty. You can use the following line to set up memory to hold the input:
.data
vals: .space 4000
.data
store: .space 4000
start: .asciiz "Welcome to number sorter, enter integers negative or postive, 9999 will terminate. "
instruc: .asciiz "Please enter number then press enter. "
od_ouput: .asciiz "There are odd numbers. "
ev_ouput: .asciiz "There are even numbers. "
nl: .asciiz " "
.text
our teacher expects us to use this format if you could assist it would be extrememly helpful!!! due in 14 hours!!!! thank you!!!!
Explanation / Answer
Program for Sorting:
.data
msg1: .asciiz " Enter values followed by return (9999 terminates input): "
msg2: .asciiz ","
msg3: .asciiz "Sort"
msg4: .asciiz "#########pass#########"
msg5: .asciiz " "
msg6: .asciiz " Number list has been sorted "
.text
.globl main
main:
move $s0,$gp #get the intial point to save array
addi $t0,1 # $t0 = 1
add $t1,$zero,$zero #
add $t2,$zero,$zero #
add $t3,$zero,$zero #
add $t6,$zero,$zero
add $t4,$zero,$zero
sub $t7,$zero,1 # terminate
li $v0,4 # system call to put the string
la $a0,msg1 #
syscall #
add $s1,$s0,$zero # copy the pointer to array in $s1
enter:
li $v0,5 # get the value in v0
syscall #
beq $v0,$t7,sort # end of string run to bubblesort
sb $v0,0($s1) # put the value at the position pointed by $s1
addi $s1,1 # move the $s1 pointer by one
add $t5,$s1,$zero # $t5 stores the end value
j enter
sort:
add $t4,$s0,$zero
addi $t6,1
#s1-1 -> s0
sub $s1,$s1,$t0
beq $s1,$s0,ending # we have sorted everything
#s0 -> s1
add $s2,$s0,$zero
inside:
lb $t1,0($s2) # first element
lb $t2,1($s2) # second element
slt $t3,$t2,$t1 #
beq $t3,$zero,temp #
sb $t2,0($s2) #
sb $t1,1($s2) #
temp:
addi $s2,1 #
bne $s2,$s1,inside #
li $v0,4 # system call to put the string
la $a0,msg5 #
syscall #
li $v0,4 # system call to put the string
la $a0,msg4 #
syscall #
li $v0,4 # system call to put the string
la $a0,msg5 #
syscall #
imp:
li $v0,1
lb $a0,0($t4)
syscall
li $v0,4
la $a0,msg2
syscall
addi $t4,1
bne $t4,$t5,imp
jal sort
ending:
li $v0,4 # system call to put the string
la $a0,msg6 #
syscall #
li $v0,5
syscall
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.