Quantcast
Channel: x86 Assembly - Programmers Heaven
Viewing all articles
Browse latest Browse all 152

bubble sort

$
0
0
Hi everyone, I'm trying to implement a function in x86 assembly that sorts a given array of characters in descending order according to its ASCII code. The inputs are an array of characters and the size of the array.

In my case, the given array contains "cOmPuTeR" so the result should be "umecTRPO"

This is the pseudocode that I'm trying to follow:
[code]
/* bubble sort pseudocode
for(i=(arraySize - 1); i>=0; i--)
for(j=1; j<=1; j++)
if(arrayOfLetters[j-1] < arrayOfLetters[j])
{
temp = arrayOfLetters[j-1]
arrayOfLetters[j-1] = arrayOfLetters[j]
arrayOfLetters[j] = temp
}
*/
[/code]

Here is my code, when I try to run it I just get the original array back and the sort is not working at all.

[code]
int descending_sort( char arrayOfLetters[], int arraySize )
{
char temp;
__asm{
push eax
push ebx
push ecx
push edx
push esi
push edi

mov ebx, arrayOfLetters //initialize array
mov esi, arraySize //i
dec esi //i-1
mov edi, 1 //j
mov ecx, 0

OUTER_LOOP:
cmp esi, ecx
jl END_OUTER
jge INNER_LOOP
dec esi
jmp OUTER_LOOP

INNER_LOOP:
cmp edi, esi
jg OUTER_LOOP
jle COMPARE
inc edi
jmp INNER_LOOP

COMPARE:
mov al, byte ptr[ebx + edi * 1 - 1]
mov dl, byte ptr[ebx + edi * 1]
cmp al, dl
jl SWAP

SWAP:
mov temp, al
mov al, dl
mov dl, temp

END_OUTER:

pop edi
pop esi
pop edx
pop ecx
pop ebx
pop eax
}
}
[/code]

I appreciate any suggestions, thanks for your time.

Viewing all articles
Browse latest Browse all 152

Trending Articles