Write a program in MIPS assembly language that takes a set of signed 32-bit inte
ID: 670310 • Letter: W
Question
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.
Example:
If the user inputs the values 15, 2, -9, 76, 45, 9999
The output in the console will be:
(Sorted even numbers) 2, 76
(Sorted odd numbers) -9, 15, 45
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
Explanation / Answer
.Ltext0:
.section ;This is the array variable declare
.rodata ; This is the variable that used in the looping
.LC0:
.string "Enter the numbers "
.LC1:
.string "%d"
.text
.globl main
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $64, %rsp
movl $0, -52(%rbp)
movl $.LC0, %edi
movl $0, %eax
call printf
movl $0, -52(%rbp)
jmp .L2
.L3:
addl $1, -52(%rbp)
leaq -48(%rbp), %rax
movl -52(%rbp), %edx
movslq %edx, %rdx
salq $2, %rdx
addq %rdx, %rax
movq %rax, %rsi
movl $.LC1, %edi
movl $0, %eax
call __isoc99_scanf
.L2:
cmpl $9, -52(%rbp)
jle .L3
movl $0, -52(%rbp)
jmp .L4
.L7:
movl -52(%rbp), %eax
cltq
movl -48(%rbp,%rax,4), %eax
cmpl $9999, %eax
jne .L5
jmp .L1
.L5:
addl $1, -52(%rbp)
leaq -48(%rbp), %rax
movq %rax, %rdi
call sort
.L4:
cmpl $9, -52(%rbp)
jle .L7
.L1:
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.section
.rodata
.LC2:
.string "Sorted Even Numbers %d"
.LC3:
.string "Sorted Odd Numbers %d"
.text
.globl sort
sort:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movq %rdi, -24(%rbp)
movl $0, -16(%rbp)
movl $0, -12(%rbp)
movl $10, -8(%rbp)
movl $0, -4(%rbp)
movl $0, -16(%rbp)
jmp .L9
.L13:
movl $0, -12(%rbp)
jmp .L10
.L12:
movl -12(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %edx
movl -12(%rbp), %eax
cltq
addq $1, %rax
leaq 0(,%rax,4), %rcx
movq -24(%rbp), %rax
addq %rcx, %rax
movl (%rax), %eax
cmpl %eax, %edx
jle .L11
movl -12(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
movl %eax, -4(%rbp)
movl -12(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rax, %rdx
movl -12(%rbp), %eax
cltq
addq $1, %rax
leaq 0(,%rax,4), %rcx
movq -24(%rbp), %rax
addq %rcx, %rax
movl (%rax), %eax
movl %eax, (%rdx)
movl -12(%rbp), %eax
cltq
addq $1, %rax
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rax, %rdx
movl -4(%rbp), %eax
movl %eax, (%rdx)
.L11:
addl $1, -12(%rbp)
.L10:
addl $1, -16(%rbp)
movl -16(%rbp), %eax
movl -8(%rbp), %edx
subl %eax, %edx
movl %edx, %eax
subl $1, %eax
cmpl -12(%rbp), %eax
jg .L12
.L9:
movl -8(%rbp), %eax
subl $1, %eax
cmpl -16(%rbp), %eax
jg .L13
movl $0, -16(%rbp)
jmp .L14
.L17:
movl -16(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
andl $1, %eax
testl %eax, %eax
jne .L15
movl -16(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
movl %eax, %esi
movl $.LC2, %edi
movl $0, %eax
call printf
jmp .L16
.L15:
movl -16(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
movl %eax, %esi
movl $.LC3, %edi
movl $0, %eax
call printf
.L16:
addl $1, -16(%rbp)
.L14:
cmpl $9, -16(%rbp)
jle .L17
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
.Letext0:
There are various registers that have been used to compute the above functionality and one can hence check for the detailed analysis for the code provided above at (assembly.ynh.io)
Each and every step have been defined clearly and hence output will also be displayed.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.