News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Bubble Sort in Masm32

Started by ultima160, November 08, 2007, 02:29:42 AM

Previous topic - Next topic

ultima160

Hi everyone,

I'm new to assembler and i've been trying to get my program working for days now. It's really testing my patience.
So anyways I'm, trying to write a program that does a bubblesort. And it works. The problem is it only sorts the first 7 numbers.
After that the rest get printed out in the order they were typing in.

Could someone please tell me why this is happening! Thanks.



Sort proc uses edi esi ecx edx eax ebx
local count:sdword
local mySize:sdword
local index:sdword
local temp:sdword
local currIndex:sdword
local nextIndex:sdword
local number:sdword

local quantity:sdword
mov quantity, ebx

mov count, 0 ; initialise count to one
@@loop_i:
mov index, 0 ; initalise index to zero
@@loop_j:

; Get value at array[index], store into edx
mov ebx, index                      ; copy index to eax
mul elementSize                     ; multiply eax by the element size to get the array offset
mov esi, offset [array]             ; move the array address to esi
add esi, ebx                        ; add the array address to the array offset

mov ebx, sdword ptr [esi]           ; copy the value at the array address to edx
mov currIndex, ebx
mov ebx, sdword ptr [esi+4]          ; copy the value at the array address to edx
mov nextIndex, ebx

mov ebx, nextIndex
; if current index > next index, then swap...
cmp currIndex, ebx ; compare the two values
jnge @@no_swap ; jump if less than or equal (signed) to no_swap

; SWAP
mov ebx, nextIndex

mov number, ebx                     ; copy the input value to number
mov ebx, index                      ; copy arrayPosition (index) to eax
mov esi, offset [array]             ; move the array address to esi
add esi, ebx                        ; add the array address to the array offset
mov ebx, number                     ; place the number in edx
mov sdword ptr [esi], ebx            ; copy edx to the current array position

mov eax, currIndex
mov number, eax                     ; copy the input value to number
mov eax, index                      ; copy arrayPosition (index) to eax
mov eax, number                     ; place the number in edx
mov sdword ptr [esi+4], eax          ; copy edx to the next array position

@@no_swap:
inc index ; increment index
cmp index, length array ; compare the array length to index
jne @@loop_j ; jumps to top of loop if not equal
;(8) ENDFOR
inc count ; increment count
mov eax, quantity
cmp count, eax ; compare the array length to quanity
jne @@loop_i ; jumps to top of loop if not equal
; (9) ENDFOR

ret
Sort endp


Shell

mov ebx, index                      ; copy index to eax
mul elementSize                     ; multiply eax by the element size to get the array offset


Make sure you follow what the comments say TO THE LETTER. Comment is correct, code is wrong :P

Nice homework btw  :bg

hutch--

ultima160,

Knowing how a bubble sort works will probably give you the method of debugging your code. A bubble sort is an "exchange" sort that works on adjoining pairs of values to see if they are ordered one higher than the other. if the sort is ascending, then a pair of numbers 125 and 107 side by side must be swapped. A bubble sort starts at the beginning of the numbers and tests pairs at a time swapping them if they are the wrong way around. A bubble sort will keep passing through the same set of numbers until there are no numbers left to swap, then the numbers are sorted.

Its virtue is it is very simple, its vice is that it is very slow alongside many other sort designs. It is also memory efficient in that it is an "in place" sort and does not require any extra memory to be allocated. If the data is nearly sorted it is very fast but very slow otherwise.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

ultima160

Quote from: Shell on November 08, 2007, 10:50:08 AM
mov ebx, index                      ; copy index to eax
mul elementSize                     ; multiply eax by the element size to get the array offset


Make sure you follow what the comments say TO THE LETTER. Comment is correct, code is wrong :P

Nice homework btw  :bg


Well gee thanks the smiles and everything are REALLY helpful, but if i fully understood the code I wouldnt be here asking for help.

How's about a NON-ELITIST and helpful answer?

hutch--

ultima160,

Instead of giving lip to members look in the other bubble sort thread for a working example that explains how it works. then you can write your own.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

ultima160

Quote from: hutch-- on November 08, 2007, 01:23:58 PM
ultima160,

Instead of giving lip to members look in the other bubble sort thread for a working example that explains how it works. then you can write your own.

OKay...

I fixed up the code a bit more. I know how the bubblesort algorithm works, its:

for i= 1 to count-1

   for j = 1 to count-1

       if array[j]>array[j+1]
             swap values


This is the fixed code:

Sort proc uses edi esi ebx eax
local outerCount:sdword
local innerCount:sdword
local arrayIndex:sdword

mov outerCount, 0 ; initialise outerCount to one
@@loop_i:
mov innerCount, 0 ; initalise innerCount to zero
@@loop_j:
mov ebx, innerCount                 ; copy innerCount to ebx
mov esi, offset [array]             ; move the array address to esi
add esi, ebx                        ; add the array address to the array offset

mov ebx, sdword ptr [esi]      ; copy the value at the array address to ebx
mov arrayIndex, ebx ; copy the array address to ebx

mov ebx, [esi+4] ; copy next innerCount to ebx
cmp [esi], ebx ; compare the two values
jne @@no_swap ; jump if less than or equal (signed) to no_swap

; Swap
mov eax, arrayIndex      ; copy next arrayPosition to eax
xchg eax, [esi+4]
mov [esi], eax          ; copy eax to the next array position

; Dont swap
@@no_swap:
inc innerCount ; increment innerCount
mov eax, arrayElements
dec eax
cmp innerCount, eax ; compare the array length to innerCount
jne @@loop_j ; jumps to top of loop if not equal
inc outerCount
mov eax, arrayElements
cmp outerCount, eax ; compare the array length to quanity
jne @@loop_i ; jumps to top of loop if not equal

ret
Sort endp




Mark Jones

Hi,

jne @@no_swap  ; jump if less than or equal (signed) to no_swap

Are you sure you want to Jump if Not Equal here? Just an exercise in pedantics as the comment and code differ.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08