flat assembler
Message board for the users of flat assembler.

Index > Main > Question about qsort

Author
Thread Post new topic Reply to topic
Ton



Joined: 06 Jan 2005
Posts: 22
Ton
If I take the qsort from http://board.flatassembler.net/topic.php?t=3048

format elf executable
entry main

main:
push [lo] [hi]
call quicksort
mov eax, 1
mov ebx, 0
int 80h

quicksort:
mov eax,[lo]
mov edx,[hi]

etc

I compile with fasm 1.60 (with FC2), but if I run the program I get a Segmentation fault.
As far as I can oversee the code is correct. What do I oversee ?


---
Best Regards,
Ton
Post 20 Mar 2005, 19:47
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17450
Location: In your JS exploiting you and your system
revolution
How did you define [lo] and [hi]?

I think your problem is because of stack overflow/underflow. The example your link points to uses the stack to pass parameters but you read [lo] and [hi] directly inside the quicksort procedure.
Post 21 Mar 2005, 02:22
View user's profile Send private message Visit poster's website Reply with quote
Ton



Joined: 06 Jan 2005
Posts: 22
Ton
I too think it is a stack problem

I tried with a small data set. Here you can also see how lo and hi are set.

align 4
a dd 3,6,1,8,2,5
lo dd 0
hi dd 5*4

I followed the sorting with ald, and indeed the data set is sorted. The problem occurs when the 'parent' qsort exits. I counted the pushes and pops and see no problem. Despite I also tried:

main:
mov edp, esp
sub esp,20 ; whatever
push [lo] [hi]
call quicksort
mov esp,edp

all to no avail.

--
Best Regards,
Ton
Post 21 Mar 2005, 07:46
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17450
Location: In your JS exploiting you and your system
revolution
Quote:

How did you define [lo] and [hi]?


Paste the whole thing, I can't help if you only post small parts the don't show all definitions and code!
Post 25 Mar 2005, 01:10
View user's profile Send private message Visit poster's website Reply with quote
Ton



Joined: 06 Jan 2005
Posts: 22
Ton
Fair enough. Here it is:

format elf executable
entry main

main:
push [lo] [hi]
call quicksort
mov eax, 1
mov ebx, 0
int 80h

quicksort:
mov eax,[lo]
mov edx,[hi]
lea ecx,[eax+edx]
shr ecx,3
shl ecx,2
mov ecx,[a+ecx]

.qc1:
cmp [a+eax],ecx
jnc .qc2
add eax,4
jmp .qc1
.qc2:
cmp ecx,[a+edx]
jnc .qc3
sub edx,4
jmp .qc2
.qc3:
cmp edx,eax
jc .qc4
push [a+eax] [a+edx]
pop [a+eax] [a+edx]
add eax,4
sub edx,4
cmp edx,eax
jnc .qc1
.qc4:
cmp [esp+8],edx
jnc .qc5
push dword [esp+8]
push edx
call quicksort
pop edx
pop dword [esp+12]
.qc5:
cmp eax,[esp+4]
jnc .qc6
push eax
push dword [esp+8]
call quicksort
pop dword [esp+12]
pop eax
.qc6:
ret

align 4
a dd 3,6,1,8,2,5
lo dd 0
hi dd 5*4
Post 25 Mar 2005, 19:49
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17450
Location: In your JS exploiting you and your system
revolution
There is a problem here

Code:
mov eax,[lo] 
mov edx,[hi] 
    


change to:

Code:
mov eax,[esp+8]
mov edx,[esp+4]
    
Post 28 Mar 2005, 01:23
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.