In: Computer Science
ASSEMBLY PROGRAM!!! QtSpim
Sorting Data
Add the Bubble Sort to minMaxArray.asm to sort the array into ascending order. Use the Bubble Sort algorithm from the lecture. You can use either Base Addressing or Indexed Addressing for the arrays.
For this assignment, make sure you prompt the user for the numbers. Do not hard-code them in the data section.
NOTE: Declare the array last in the Data section.
The solution to given question:
The following program takes the following inputs:
Output:
The program prints the elements of Sorted Array.
Program to implement bubble sort (optimized solution):
======================================================
.LC0:
.string "Sorted Array: "
.LC1:
.string "%d "
bubbleSort:
push rbp
mov rbp, rsp
sub rsp, 32
mov QWORD PTR [rbp-24], rdi
mov DWORD PTR [rbp-28], esi
mov DWORD PTR [rbp-12], 0
mov DWORD PTR [rbp-4], 0
jmp .L2
.L8:
mov DWORD PTR [rbp-8], 0
jmp .L3
.L5:
mov eax, DWORD PTR [rbp-8]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov edx, DWORD PTR [rax]
mov eax, DWORD PTR [rbp-8]
cdqe
add rax, 1
lea rcx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rcx
mov eax, DWORD PTR [rax]
cmp edx, eax
jle .L4
mov eax, DWORD PTR [rbp-8]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov eax, DWORD PTR [rax]
mov DWORD PTR [rbp-16], eax
mov eax, DWORD PTR [rbp-8]
cdqe
add rax, 1
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov edx, DWORD PTR [rbp-8]
movsx rdx, edx
lea rcx, [0+rdx*4]
mov rdx, QWORD PTR [rbp-24]
add rdx, rcx
mov eax, DWORD PTR [rax]
mov DWORD PTR [rdx], eax
mov eax, DWORD PTR [rbp-8]
cdqe
add rax, 1
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rdx, rax
mov eax, DWORD PTR [rbp-16]
mov DWORD PTR [rdx], eax
mov DWORD PTR [rbp-12], 1
.L4:
add DWORD PTR [rbp-8], 1
.L3:
mov eax, DWORD PTR [rbp-28]
sub eax, DWORD PTR [rbp-4]
sub eax, 1
cmp DWORD PTR [rbp-8], eax
jl .L5
cmp DWORD PTR [rbp-12], 0
je .L11
add DWORD PTR [rbp-4], 1
.L2:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-28]
jl .L8
jmp .L7
.L11:
nop
.L7:
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
mov DWORD PTR [rbp-4], 0
jmp .L9
.L10:
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov eax, DWORD PTR [rax]
mov esi, eax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call printf
add DWORD PTR [rbp-4], 1
.L9:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-28]
jl .L10
nop
nop
leave
ret
.LC2:
.string "Enter the number of elements to be sorted: "
.LC3:
.string "%d"
.LC4:
.string "Enter element no. %d: "
main:
push rbp
mov rbp, rsp
sub rsp, 432
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
lea rax, [rbp-420]
mov rsi, rax
mov edi, OFFSET FLAT:.LC3
mov eax, 0
call __isoc99_scanf
mov DWORD PTR [rbp-4], 0
jmp .L13
.L14:
mov eax, DWORD PTR [rbp-4]
add eax, 1
mov esi, eax
mov edi, OFFSET FLAT:.LC4
mov eax, 0
call printf
lea rdx, [rbp-416]
mov eax, DWORD PTR [rbp-4]
cdqe
sal rax, 2
add rax, rdx
mov rsi, rax
mov edi, OFFSET FLAT:.LC3
mov eax, 0
call __isoc99_scanf
add DWORD PTR [rbp-4], 1
.L13:
mov eax, DWORD PTR [rbp-420]
cmp DWORD PTR [rbp-4], eax
jl .L14
mov edx, DWORD PTR [rbp-420]
lea rax, [rbp-416]
mov esi, edx
mov rdi, rax
call bubbleSort
mov eax, 0
leave
ret