Category Archives: Uncategorized

Updated Bubble Sort in x86 ASM

So, after another week of studying assembly, I went back and rewrote my bubble sort algorithm.  I’m feeling a lot better about this implementation.  I’m sure it could could be even better, but I’m pretty pleased with it this time.

Here is the code:

 

; Author J. Logiodice
; Date: 05/28/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 db ?

section ‘.text’ code readable executable

start:

; Set up the loop variables
mov ecx, dword TOTAL_NUMS
mov esi, dword TOTAL_NUMS ;ecx
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Read in one number at a time, for TOTAL_NUMS numbers, store them in the bytes that
; start with array_numbers
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

loopRead:

; have to use esi because read_hex uses edi
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

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; 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, dword TOTAL_NUMS – 1
; set the swapped variable to false
mov [boolSwapped], byte 0 ;dword 0

lea esi, [array_numbers]

loopSort:

; jump to the loop exit if ecx is 0
; this will occur if we are at the end of the counter, or if the
; initial array was of size 0 bytes
jecxz loopExit
; load the first number in array_numbers into eax
lodsd
; after lodsd, edi is pointing to edi – dword
; if we compare the value at edi at this point it is effectively the value below eax
; compare eax to the number below it in the array
cmp eax, [esi] ;, eax
; if [esi] > eax, then no ZF or CF set, so jump below or equal
jbe noSwap
; if here then eax is lower, swap eax and the higher number in memory
mov ebx, dword [esi]
mov [esi], dword eax
mov dword [esi-4], ebx

; if a swap occured, set boolSwapped = 1

mov [boolSwapped], 1b
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 ecx, TOTAL_NUMS
lea esi, [array_numbers]

loopPrint:

jecxz loopExit
lodsd
call print_eax

loop loopPrint
exit_prog:
push 0
call [ExitProcess]
include ‘training.inc’

I must go down to the sea again

There is something indescribable and wondrous about the vast and endless sea.  The call of the ocean, echoing in history, throughout the future.  I cannot recall physically going into the ocean for the past 30 years.  It is an amazing feeling, a feeling of both insignificance and of fortitude: insignificance in the realization of how tiny and insubstantial I am, but strength in realizing that of all creatures in creation this world was made for me.

I, as human, represent the pinnacle of crowning achievement for creation; the most complex, the most intriguing of all creatures with my abilities to think and love and reason in unique ways: being granted the blessing (or curse) of being one of the few known reasoning creatures that will spend most of my lifetime contemplating my own mortality.

I started this weak on Cocoa Beach officiating the wedding of my brother-in-law, and new sister-in-law: Jeremy and Rebecca Jewers.  It was an honor and a privilege to be asked to perform the ceremony, it is the second time in my life I have been called upon to do such an amazing thing.  Marriage is as wonderful as birth, and baptism; both representing a transition, a newness, a transformation from old to new; from form to form.

2014 Cocoa beach

2014 Cocoa beach

The wedding started with the scene from the Princess Bride:  Mawage, Mawage is what bwings us togethwer today.  It was the perfect fit, a perfect couple: a farm boy turned pirate and a princess.  Two people, that were meant to be together.

Cocoa beach wedding 2014

Cocoa beach wedding 2014

On our last night on the beach, Amanda and I were taking a walk down the beach in the moonlight and we ran into a majestic but ominous looking foot long crab.  We were in awe at his size and amazed at his beauty, until we saw that he held in his claw a baby hatchling loggerhead turtle.

We immediately went into rescue mode.  I took on the crab (and he was vicious!) and encouraged him to drop the turtle (ok, I might have kicked him in the rear with my bare foot while Amanda kept his attention). Then, while I kept the crab occupied (he continued coming after me), Amanda guarded the baby turtle as it made it to the water.

In the end, we both were able to watch the turtle swim out to sea, and we then returned to the crab to take a picture of him.  He belongs on the wall of shame! This was one of the most amazing things I have experienced in nature.

Being saved from Crab

Being saved from Monster Crab

Attempting to eat a Loggerhead

Wall of Shame: Attempting to eat a baby Loggerhead

 

 

 

 

 

 

 

 

We are now preparing, after a full week of God’s beautiful nature to head back to the city of San Antonio.  We miss friends there, but honestly, we are not overly excited to go back; we miss the nature, the openness, the sea breeze of the east coast.  It’s hard to say what the future will hold for us, but one thing for sure.  Home is where the heart is, and there is no place like home.

Amanda and Jediah August 2014

Amanda and Jediah August 2014