I changed the loop completely so its more understandable, and more easily debugged.
So this version works....some of the time. I do not get why it only works some of the time. But when it works it does it perfectly! example
give it -1,-34,45,67 then 0
will give you
-34,-1,45,67
give it -1,-23,-34,-45,-56,-67,-87,56,67,789,34545,0
it gives me a segmentation fault. Damnit. Its acceptable, but I really want it to work all the time.
%include "Along32.inc"
section .data ;data section
; Start Main Method
section .bss
array resd 100;
section .code ;code section
global _start
_start: ; main program
mov ecx,0;
call readInts ;
; methods
;this one works, it reads in the ints and places them into a array.
greaterThan:
xchg [esi+ecx*4],ebx ;
mov [esi+edx*4],ebx ;
ret ;
readInts:
call ReadInt ;
push ecx ;
mov ecx, eax ;
jecxz sortSetUp ;
pop ecx ;
call WriteInt ;
call Crlf ;
mov [array+ecx*4],eax ;
inc ecx ;
call readInts ;
;this saves values, subtracts one from ecx( ecx ends up being one over because arrays start at the index of 0) it then grabs the last value, and
;places it in ebx
sortSetUp:
mov esi, array ;
pop ecx ;
mov eax,ecx ;
mov edx,ecx ;
dec ecx ;
call sortNext ;
;this is the method that will sort the array. It does so by comparing ebx(containing last value) to every other value. If the value ebx is being
;compared to is //larger it exchanges places with the two different numbers.
sortNext:
push ecx ;
dec edx ;
jecxz printAllSetUp ;
mov ebx,[esi+edx*4] ;
mov ecx,edx ;
call sort ;
pop ecx ;
loop sortNext;
sort:
cmp [esi+ecx*4],ebx ;
JG greaterThan ;
loop sort ;
ret ;
;This number will first exchange the larger number with ebx(ebx now holds the larger number) It then copies the larger value to the last slot in the
;array as to put them in descending order. This will find the largest number and place it in the last slot after comparing all values.
;Subsequent loops will then not compare values to the last value. Which will allow the program to run slightly faster, but more importantly
;it will not copy the largest value over and over again.
;as stated int the previous explanation, this method decrements edx so we wont grab the last value on the next go through, and it will continue
;until we reach the last value, and since it is the only value left we know it is in the right place so it then jumps to the end of the
;program(the method that prints out all of the values)
;we have this method so we can restore ecx to its original glory.
printAllSetUp:
mov edx,0 ;
mov ecx,eax ;
call printAll ;
;this method basically just uses edx(which equals 0) and moves it up by one each go around to simply write out the ints
printAll:
mov eax,[esi+edx*4] ;
inc edx ;
call WriteInt ;
jecxz exit ;
loop printAll ;
exit: ;exit function
call Crlf ;
mov eax,1 ;mov 1 eax
int 0x80 ; ask kernal to exit