Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 25th, 2005, 1:11 PM   #1
oxyi
Newbie
 
Join Date: Jan 2005
Posts: 17
Rep Power: 0 oxyi is on a distinguished road
Could anyone please convert this into assembly ?

Could someone please convert this code into assembly for me to see ?
and btw is the only way to convert HL lanuage into assembly is by using linux ?


void quickSort(int numbers[], int array_size)

{

  q_sort(numbers, 0, array_size - 1);

}

 

 

void q_sort(int numbers[], int left, int right)

{

  int pivot, l_hold, r_hold;

 

  l_hold = left;

  r_hold = right;

  pivot = numbers[left];

  while (left < right)

  {

    while ((numbers[right] >= pivot) && (left < right))

      right--;

    if (left != right)

    {

      numbers[left] = numbers[right];

      left++;

    }

    while ((numbers[left] <= pivot) && (left < right))

      left++;

    if (left != right)

    {

      numbers[right] = numbers[left];

      right--;

    }

  }

  numbers[left] = pivot;

  pivot = left;

  left = l_hold;

  right = r_hold;

  if (left < pivot)

    q_sort(numbers, left, pivot-1);

  if (right > pivot)

    q_sort(numbers, pivot+1, right);

}
oxyi is offline   Reply With Quote
Old Feb 27th, 2005, 6:34 PM   #2
lepricaun
Hobbyist Programmer
 
lepricaun's Avatar
 
Join Date: Aug 2004
Location: The Netherlands
Posts: 111
Rep Power: 5 lepricaun is on a distinguished road
i suppose you want it on windows?

i would do this:

compile the program and open the exe with a disassembler like ollydbg or win32dasm. now you will have the assembly code of the program. if you know assembly well enough then it shouldn't be much of a problem reading it and writing it in an .asm file.
__________________
http://www.white-scorpion.nl
lepricaun is offline   Reply With Quote
Old Feb 27th, 2005, 6:55 PM   #3
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 708
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
there are ways to do it with a compiler switch. Do some research. Come back if you need more help.
thechristelegacy is offline   Reply With Quote
Old Mar 2nd, 2005, 4:04 PM   #4
oxyi
Newbie
 
Join Date: Jan 2005
Posts: 17
Rep Power: 0 oxyi is on a distinguished road
a complier switch, so i can basically stick in like visual.net but with a different switch ?

btw ,
how would i pass the function parameters using stack call, instead of register or memory ? if it was written in x86 asm already.
oxyi is offline   Reply With Quote
Old Mar 8th, 2005, 4:54 AM   #5
oxyi
Newbie
 
Join Date: Jan 2005
Posts: 17
Rep Power: 0 oxyi is on a distinguished road
so no one can convert it for me ? :\
oxyi is offline   Reply With Quote
Old Mar 11th, 2005, 6:16 AM   #6
liquidsilver
Newbie
 
liquidsilver's Avatar
 
Join Date: Mar 2005
Location: South Africa
Posts: 21
Rep Power: 0 liquidsilver is on a distinguished road
I'll do it, just wait till I get a chance. Probably by Monday or Tuesday.
__________________
Small is beautiful

Last edited by liquidsilver; Mar 11th, 2005 at 6:18 AM. Reason: mistake made
liquidsilver is offline   Reply With Quote
Old Mar 11th, 2005, 2:16 PM   #7
liquidsilver
Newbie
 
liquidsilver's Avatar
 
Join Date: Mar 2005
Location: South Africa
Posts: 21
Rep Power: 0 liquidsilver is on a distinguished road
I had some time so here we go:

ArraySize db 4 ;largest entry (number of entries - 1)
Array			db 00h,02h,01h,03h,04h
Left			db ?
Right			db ?
Pivot			db ?
lHold			db ?
rHold			db ?

QSort proc

	mov lHold,0
	mov al,Arraysize
	mov rHold,al
	mov al,Array
	mov Pivot,al

	StrWhile1:
		mov al,Left
		cmp al,Right
		jge EndWhile1
		
		StrWhile2:
			mov di,offset Array
			xor bx,bx
			mov bl,Right
			mov al,[di+bx]
			cmp al,Pivot
			jl EndWhile2
			
			mov al,Left
			cmp al,Right
			jge EndWhile2
			
			mov al,Right
			dec al
			mov Right,al
		EndWhile2:
		
		mov al,Left
		cmp al,Right
		jz EndIf1
			mov di,offset Array
			xor bx,bx
			mov bl,Right
			mov al,[di+bx]
			
			mov di,offset Array
			xor bx,bx
			mov bl,Left
			mov [di+bx],al
			
			mov al,Left
			inc al
			mov Left,al
		EndIf1:
		
		StrWhile3:
			mov di,offset Array
			xor bx,bx
			mov bl,Left
			mov al,[di+bx]
			cmp al,Pivot
			jg EndWhile3
			
			mov al,Left
			cmp al,Right
			jge EndWhile3
			
			mov al,Left
			dec al
			mov Left,al
		EndWhile3:
		
		mov al,Left
		cmp al,Right
		jz EndIf2
			mov di,offset Array
			xor bx,bx
			mov bl,Left
			mov al,[di+bx]
			
			mov di,offset Array
			xor bx,bx
			mov bl,Right
			mov [di+bx],al
			
			mov al,Right
			dec al
			mov Right,al
		EndIf2:
	EndWhile1:
	
	mov al,Pivot
	mov di,offset Array
	xor bx,bx
	mov bl,Left
	mov [di+bx],al
	
	mov al,Left
	mov Pivot,al
	
	mov al,lHold
	mov Left,al
	
	mov al,rHold
	mov Right,al
	
	mov al,Left
	cmp al,Pivot
	jge EndIf3
		mov al,Pivot
		dec al
		mov Right,al
		call QSort
	EndIf3:
	
	mov al,Right
	cmp al,Pivot
	jle EndIf4
		mov al,Pivot
		inc al
		mov Left,al
		call QSort
	EndIf4:
	
	ret
QSort endp

I haven't checked this for errors or done any optimization, you can do that. This will be easier to read this way. I haven't even passed variables to the function, I've just created some globals, I hope that doesn't mess up the recursion that takes place.

Unless I'm horribly mistaken, you're trying to do a simple sort. Search the net for a more optimized version and in future first search the net, then you can find exactly what you're looking for.
__________________
Small is beautiful
liquidsilver is offline   Reply With Quote
Old Mar 12th, 2005, 1:35 AM   #8
oxyi
Newbie
 
Join Date: Jan 2005
Posts: 17
Rep Power: 0 oxyi is on a distinguished road
Wow thanks so much Liquidsilver !!
I kinda know what you are doing, but kinda not too ...
not sure if it is the format problem, sorry im a newbie..

void q_sort();

void quickSort(int numbers[], int array_size)
{
   __asm
  {
	push eax
	push ebx
	push ecx
	push edx
	push edi
	push esi
//START
.......
//END
	pop esi
	pop edi
	pop edx
	pop ecx
	pop ebx
	pop eax
  }
}

__declspec(naked)
void q_sort()
{
	 __asm
  {
//START
........
//END  }
}

that's frame that I suppose to code this quicksort into, basically I have pass the function parameters using stack call ?? (Which I have no clue how.. )

It seem like you just have a whole function without calling the other function.
Again, I am newbie, can't really understand it that well, espeically when you do the xor bx,bx.. that was really confusing

And yea, i did search the net, but the way they do thier asm is like linux or something, not like what I learned, but I did find this site
http://cs.hofstra.edu/~cscccl/csc111/qsort.asm
which is pretty close to what I am trying to do, but still, he uses
;; [ebp+8] is start address, [ebp+12] is end address of partition
and i don't know how would i make the frame thats given to me with the start address, and the end address, so im stucked here again.

Sorry for such a long reply, you are very helpful ! thanks

Last edited by oxyi; Mar 12th, 2005 at 1:38 AM.
oxyi is offline   Reply With Quote
Old Mar 12th, 2005, 12:29 PM   #9
liquidsilver
Newbie
 
liquidsilver's Avatar
 
Join Date: Mar 2005
Location: South Africa
Posts: 21
Rep Power: 0 liquidsilver is on a distinguished road
I can't help you much more, I suck at C, but I think I can help a bit.

I thought you wanted it for use in pure assembly, I don't know to call it from C. Also, my function refers to global variables for the arguments, this isn't a good way, but I did that as it's easier to understand.

Yes I do only have one function. And if you xor a value by itself then it becomes 0.

In that file you found they used the stack for the variables, this is the better way, but I didn't do it as I thought you wouldn't understand. The arguments are pushed onto the stack and then the function refers to that memory instead.

I can't help you call the function from C, I don't know how, sorry. I suggest you type the whole program in assembly or C, I can't help with mixing the two.
__________________
Small is beautiful
liquidsilver is offline   Reply With Quote
Old Mar 13th, 2005, 5:37 AM   #10
oxyi
Newbie
 
Join Date: Jan 2005
Posts: 17
Rep Power: 0 oxyi is on a distinguished road
Oh, it's okay , it has nothing to do with C (i think )
but it is just asssembly embedded in C, and your explantion did clear up some
of my problem too ;] thank you so much for that !
I will try to code it tomorrow see how it goes ;] ty
oxyi is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:33 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC