[[NOTE: For a more efficient way to implement the bubble sort, see my later post]]
Why? I have no idea. It’s funny how many times I set off my AV scanner trying to compile and run my PE. That brings back some great memories with the VCL.
I’m sure there are cleaner ways to do it – but right now, I’m just worried about making it work. 😉
NOTE: Written in FASM, and the training.inc can be found over at xorpd on git.
; Author J. Logiodice
; Date: 05/22/2016
; Purpose: Bubble SOrt
; This method will read in a series of TOTAL_NUMS numbers
; And bubble sort them, then print them out in sorted order to the screen
format PE console
entry start
include ‘win32a.inc’
TOTAL_NUMS = 10 ;10
section ‘.bss’ data readable writeable
array_numbers dd TOTAL_NUMS dup (?)
nMinus1Mem dd ?
boolSwapped dd ?
section ‘.text’ code readable executable
start:
; Set up the loop variables
mov ecx, TOTAL_NUMS
mov esi, ecx
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Read in one number at a time, for TOTAL_NUMS numbers, store them in the bytes that
; start with array_numbers
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
loopRead:
dec esi
; read input into eax but first clear out eax
xor eax, eax
call read_hex
; move value into memory offset (reverse order)
mov dword [array_numbers + esi * 4], eax
loop loopRead
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; I am sure there is a more effecient way of doing this
; I will probably try and clean it up later, but for now it works.
; loop iteratively over the array until no more swapping occurs,
; and the highest number ends up in the lowest part of the array (lowest to highest)
; start with array_numbers
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Set back up the loop variables
restartLoop:
mov ecx, TOTAL_NUMS
mov esi, ecx
; set the swapped variable to false
mov [boolSwapped], dword 0
loopSort:
dec esi
mov eax, esi
sub eax,1d
test esi, esi
jbe loopExit
mov edx, dword [array_numbers + eax * 4]
cmp edx, dword [array_numbers + esi * 4]
jbe noSwap
; if we get into this section, then swapping needs to occur
; set the boolSwapped to true
mov [boolSwapped], dword 1
; need to swap the two numbers
mov [nMinus1Mem], dword edx
mov edx, dword [array_numbers + esi * 4]
mov [array_numbers + eax * 4], dword edx
mov edx, dword [nMinus1Mem]
mov [array_numbers + esi * 4], dword edx
noSwap:
; jump here if no swapping needs to occur, but we’re still in the loop
loop loopSort
loopExit:
; if boolSwapped isn’t false, then we’ve swapped at least one
; during the iteration, let’s go through it one more time to make sure
; that we don’t have any more to swap
cmp [boolSwapped], 1b
jae restartLoop
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Print each number back out to the screen for unmodified numbers
; we will loop from the lowest to highest part of the array – which is the largest to smallest number
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;mov edi, array_numbers
mov ecx, TOTAL_NUMS
mov esi, ecx
loopPrint:
dec esi
mov eax, [array_numbers + esi * 4]
call print_eax
loop loopPrint
exit_prog:
push 0
call [ExitProcess]
include ‘training.inc’