In the Name of Allah, the Most Gracious, the Most Merciful!

All the praises and thanks be to Allah, who gave us the life and knowledge and
guided us to the Straight Way, because without His Mercy we would surely fail!
Peace be upon Muhammed, the last messenger of Allah!


Since the old messageboard is gone, I feel free to post this article again here,
so we can try to work further on the string-parsing algorithm.

I won't be too long. What follows is bAZiK's code which was posted at the beginning
of the message thread "Can someone optimize this algorithm" on the old
messageboard:



; #########################################################################

.386
.model flat, stdcall
option casemap :none

; #########################################################################

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

; #########################################################################

.data
szTest db "Test1:1234", 0, "Test2:5678", 0,
"Test3:4321", 0,"Test4:8765", 0, 0
szFile db 512 dup (0)
szSize db 512 dup (0)

; #########################################################################

.code

start:
lea eax, szTest
jmp @1
@@:
inc eax
cmp byte ptr [eax], 0
jne @B
inc eax
cmp byte ptr [eax], 0
je @F
@1:
push eax
pop edi
push eax
lea esi, szFile
@2:
mov al, [edi]
cmp al, ':'
je @3
mov [esi], al
inc edi
inc esi
jmp @2
@3:
inc edi
lea esi, szSize
@4:
mov al, [edi]
mov [esi], al
inc edi
inc esi
or al, al
jnz @4
invoke MessageBox, 0, addr szFile, addr szSize, MB_OK
pop eax
jmp @B
@@:
invoke ExitProcess, 0

; #########################################################################

end start


And below is the code which is about 12,2% faster than the bAZiK's one.
It is a little bit size-optimized, so some speed has been lost. At the end of my
post I will also (in sha' Allah) present the code which is about 15,5% faster.
So, here it is. Enjoy it!



; #########################################################################

.386
.model flat, stdcall
option casemap :none

; #########################################################################

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

; #########################################################################

.data
szTest db "Test1:1234", 0, "Test2:5678", 0,
"Test3:4321", 0, "Test4:8765", 0, 0
szFile db 512 dup (0)
szSize db 512 dup (0)

; #########################################################################

.code

start:
lea esi,szTest-1 ; here we get the first pointer

nextone:
lea edi,szFile-1 ; and destination as well
inc edi ; next char at destination
inc esi ; next char at source

doitagain:
mov al,[esi] ; get the char and ...
mov [edi],al ; put it here
or al,al ; is it zero?
jz zerofound ; if so, it is the end of string
cmp al,':' ; the separator?
jz sepfound ; so let's do something
jmp doitagain ; loop

sepfound:
xor [edi],al ; put zero to destination
lea edi,szSize-1 ; and now comes the size ...
jmp nextone ; go for it!

zerofound:
invoke MessageBox,0,addr szFile,addr szSize,MB_OK

cmp byte ptr [esi+1],0 ; is it the end?
jnz nextone ; if not, so get the next one
invoke ExitProcess,0 ; THE END

; #########################################################################

end start


And now comes the other algo, which is, as already said, about 15,5% faster than
the bAZiK's one. This is the result I got while testing it on my machine. You can
test it using the GetTickCount method and a large number of iterations (Don't
forget to remove the MessageBox!).
I took 0x00FFFFFF iterations and have got the following results:


bAZiK's code 0x2DFA ms
My code 0x27C9 ms

So, just devide these two numbers, and you'll see that bAZiK's algo needs about
15,56% more time to execute. I am sorry, but I haven't still tested Hutch's algo.
If I get some free time in the future, I may do it, OK!
Here is the promised source:



; #########################################################################

.386
.model flat, stdcall
option casemap :none

; #########################################################################

include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib

; #########################################################################

.data
szTest db "Test1:1234", 0, "Test2:5678", 0,
"Test3:4321", 0, "Test4:8765", 0, 0
szFile db 512 dup (0)
szSize db 512 dup (0)

; #########################################################################

.code

start:
lea esi,szTest ; here we get the first pointer

nextone:
lea edi,szFile ; and destination as well

doitagain:
mov al,[esi] ; get the char and ...
mov [edi],al ; put it here
or al,al ; is it zero?
jz zerofound ; if so, it is the end of string
cmp al,':' ; the separator?
jz sepfound ; so let's do something
inc edi ; next char at destination
donext:
inc esi ; next char at source
jmp doitagain ; loop

sepfound:
xor [edi],al ; put zero to destination
lea edi,szSize ; and now comes the size ...
jmp donext ; go for it!

zerofound:
invoke MessageBox,0,addr szFile,addr szSize,MB_OK

inc esi ; prepare for the next char
inc edi ; destination too
cmp byte ptr [esi],0 ; is it the end?
jnz nextone ; if not, so get the next one
invoke ExitProcess,0 ; THE END

; #########################################################################

end start


All the praises and thanks be to Allah, the Lord of all that exists!


written by
Beg
Posted on 2001-07-21 08:10:48 by Beg
Beg, is GetTickCount accurate for measuring code??
Posted on 2001-07-21 08:26:08 by disease_2000
Hello disease_2000!

I am sorry if I wasn't clear enough! It wasn't my intention to write anyhing unreadable.
I think my code is readable enough, because I tried to comment almost every line.
The other things just belong to my life. I am a Muslim, and I am proud of it. A great thing about Islam is that you are not forced to belive. You are expected to use your own brain to think and understand and if you get it yourself - you belive, if not, so it is your problem.
Its primary source is very comprehensible and readable - it is the Quran - just read it, and you will understand! It is much more readable than our beloved programming language.
Enough now!

Allah = The Name of the One and Only God
The Mahala Hackers = The name of my programming group

Greetings and sorry again for misunderstanding!

Beg
Posted on 2001-07-21 08:40:09 by Beg
actually, it's my fault. i shouldn't have said that in such forum. things like that i should have keep it to myself. it is only after i posted that - i then realized it... forgive me.

i'm still curious about the GetTickCount - is it accurate for measuring code speed?
Posted on 2001-07-21 08:42:17 by disease_2000
Yes, I know GetTickCount is not very accurate, but when you take a large number of itterations, you can just use it to compare algorithms. I didn't use it to measure the speed of my code, but only to compare the algos!

Beg
Posted on 2001-07-21 08:48:36 by Beg
i see. very interesting. i'll try that. thank for the reply.
Posted on 2001-07-21 08:49:55 by disease_2000
Hello again!

I think we should talk about algorithms here, how to optimize them, where to use them, how to make code faster and smaller.
I am here for studying and trying to help other people to solve their problems.
We at the Mahala Hackers are trying to get some free time to set up our site. It is still pretty empty. And when we get it, I hope there will be interesting things to find ...

Greetings!

Beg
Posted on 2001-07-21 09:59:48 by Beg
To accurately time the code, an understanding of the algorithm needs to be incorporated into the test. Have you tried buliaNaza's code? (it's below) Can you retest the algorithms with the szTest that I have provided - it will give a more accurate view of this algorithms proformance in the general case. This doesn't matter if the string data is always going to be in the format that you have listed above (namely: "AAAAA:nnnn"), but isn't the string a more general format ("{A}:{n}", {}=many)?

;Even distribution of the colon/zero placement in a DWORD!

szTest:
db "abcd:ab",0, "abc:ab",0, "abc:ab",0, "abc:ab",0
db 0
Pars proc ;
push esi ;
push edi ;
push ebx ;
push ebp ;
lea edi, szSize ; edi -> szSize
lea ebp, szFile ; ebp -> szFile
lea esi, szTest ; esi -> szTest
Next1: ;
mov edx, edi ; Update edx -> szSize
mov ecx, 3Ah ; ecx = 3Ah = ":"
mov edi, ebp ; ebp -> szFile
loop1: ;
mov eax, [esi] ; esi -> szTest
add esi,4 ;
cmp al, cl ; cl = ":" or zero
jz L_one ;
cmp ah, cl ; cl = ":" or zero
jz L_two ;
mov ebx, eax ;
shr eax, 16 ;
cmp al, cl ; cl = ":" or zero
jz L_three ;
mov [edi], ebx ; Copy to destination
add edi, 4 ;
cmp ah, cl ; cl = ":" or zero
jnz loop1 ;
dec edi ;
jmp Next ;
Next2: ; ecx = ":"
mov ebp, edi ; Update ebp -> szFile
xor ecx, ecx ; ecx =0
mov edi, edx ; edx -> szSize
jmp loop1 ;
L_three: ;
mov [edi], bx ; Copy to destination
dec esi ;
add edi, 2 ;
jmp Next ;
L_two: ;
mov [edi], al ; Copy to destination
inc esi ;
inc edi ;
L_one: ;
sub esi,3 ;
Next: ;
test ecx, ecx ; ecx = ":" or Zero
jnz Next2 ;
cmp [esi], cl ; ecx=0 ; a check for second zero
jnz Next1 ;
mov [ebp], cl ; zero end
mov [edi], cl ; zero end
pop ebp ;
pop ebx ;
pop edi ;
pop esi ;
ret ;
Pars endp ;
Posted on 2001-07-21 13:14:12 by bitRAKE
In the Name of Allah!
Peace be upon Muhammed, the last messenger of Allah!


Hello everybody!

buliaNaza's code is very fast, much faster than the one of mine. Anyway, it should be taken in consideration that it uses the DWORD approach in contrast to bAZiK's algo and my two algos, which all use the BYTE approach. So, from that point of view, our algorithms can't be compared, but in general, we all want the final product, which is - THE FASTEST ALGORITHM. Congratulations buliaNaza!!!
The fact that reading a DWORD from a memory location takes the same time as reading a BYTE makes it possible to write much faster algorithms.
This leads us to the conclusion, that we should avoid the BYTE approach when developping such algos and force ourselves to fight with the unsuitable Little Endian.

Greetings!


All the praises and thanks be to Allah, the Lord of all that exists!

Beg

Posted on 2001-07-22 11:39:58 by Beg
Beg, to be honest with you, being in a hacker community took away your common sense. but nevertheless, things like this can be change. i don't mind you say "Ala, ala, ala!" once a week, but everytime you include that in your post, it might annoy some... and i'm not saying it wil. it depends on their mood... hope you know what i mean. (just a tip... cause i don't want others to attack you later on. and also, hutch have told everyone that religion should keep it to oneself. and i'm not attacking you, just giving a friendly tip :alright: )

and about dword, that's what we all should target. actually, that's what we all try to target! but it's not always possible... sometime, you just have to go for byte ptr.
Posted on 2001-07-22 11:47:45 by disease_2000
disease_2000, first of all I thank you for your friendly tip.
This is the second time you are giving me a friendly tip. And there is no other person in this community, who gave me such a tip. Seems like you are the only one who cares about what other people write in the headers and footers of their messages.
If you are interested in learning only, you can easily step over the header and start reading.
I have been trying all the time to be useful, to contribute, design, test, or do any other thing which may help other people in this community.
So, I will not reply to such posts anymore (in sha' Allah!). Only concrete discussions on the topic ALGORITHMS will be considered...

Beg
Posted on 2001-07-22 16:48:27 by Beg
I am annoyed by religious stuff. I'm sorry, I know it's not very
rational, but that's the way it is. Beg, I don't think you're writing
the Allah stuff to offend anybody, but... you are :(. Please, let's
keep religion far far *FAR* away from this board, as religion tends
to make people irrational.

Again, I'm truly sorry that I can't be more tolerant that I am, but
I guess it has to do with things I've seen, beatings I have taken,
et cetera.

Peace.
Posted on 2001-07-22 16:50:56 by f0dder
Being aithest (and not able to spell the word I know) I'm probably not the most qualified to comment but I loved all that religious stuff.

I my opinion once your not out to recruit people to your religion anything else you have to say about would be encouraged by me. However people should never ever use religion as an excuse for anyhting, even holidays.

Plus I never knew the following was true:
"A great thing about Islam is that you are not forced to belive. You are expected to use your own brain to think and understand and if you get it yourself - you belive, if not, so it is your problem."

I though Islam was that religion where its members believed that once someone is born into theres no getting out. I'm glad to hear thats not the case.

Long live free will. If only people had the intelligence to use it properly.

Heres where most would say Just my two cents... But in my case thats pence.

Good Luck.
Posted on 2001-07-22 17:48:19 by Eóin
Beg, i wish you understand me. I was a christian once and now aithest (i'm just like you zadkiel, can't even spell the name of who i am). normally, i don't scream at people when i talk, or even shout at them. and the reason why i'm giving you that friendly tip is because i have a vision that things will get worse if that text exist in all your header. if i didn't posted and say what i have to say, then someone else will. and when someone else will, it will be even worse than what i had said. i've been in the old forum and i've seen things... and final words is: you can say whatever you want - i'm just worry that others might attack you. that's all.

and when comes to religious stuff, i do get annoy sometime. reason? well. i follow god for 5 living years and found out that all this was fake by the time i was 14 (i question many things and found no answer. because i'm curious). that's when i start searching more in life, trying to find the meaning of life and be who i am and learn to think for myself. instead of believing the thing we don't see. (reality is my god! and it consist of space, time and thought. it's the only god that will ever exist in me and of course, some of you who read this might think i'm crazy. fact is, you can't exist without space. and things can't happen without time. and you can't move an object without being there! thought is what control things (triggers your hand, or your body)) therefore, my formula for god is: r = (s,t,o) and it's the only god that doesn't require you to pray, or goto church every sunday. whenever you make mistake, just know that you sin and try not to sin again. no need to jump off the bridge or ground yourself. no need to be kind or nice. just be who you are. cause once you die, it's emptiness.

sorry f0dder. i know it's annoying. but this reply is not for you.








farewell.
Posted on 2001-07-23 08:59:33 by disease_2000
disease_2000, sounds like your preaching there. :)
Posted on 2001-07-23 09:37:18 by bitRAKE
sorry rake. i'm abit (or very) rude when comes to religion... (rake, you now know what make me mad. :) )
Posted on 2001-07-23 09:39:09 by disease_2000
I'm not mad. I love almost all forms of expression. Nothing gives me more pleasure than to try and understand how someones life experiences has influenced their thought process, and how that relates to my own. We all wish to understand ourselves, but that can only be achieved through the understanding of another. For it is only yourself that you can ever really see. When you look outside yourself, you can see within clearer.

Posted on 2001-07-23 09:57:20 by bitRAKE
you're right rake. here's my quote from last year that describle what you mean: "You will never know who you are, if you are who you are for the rest of your life." silly quote. but it does describle your main point. my believe is that inorder to understand yourself better, you have to try and be someone else, it is by then that you'll have a full view of who you really are. but that just can't happen. you can't be someone else... you, however can with common sense. you can PRETEND that you're someone else. you can act different, talk different, dress different. that's part of being someone else. such as eating the food you don't like, talking to people you don't like, doing anything that you dislike. that's being someone else.

sure, i don't need to be someone else just to understand who i am. i already know what people think of me after all this post. Rake, have you heard of an IQ called "People-smart"? some people have that iq (i don't have this iq. but i develop them). they can sense your thought and feeling without you telling them! (scary? no. it's a combination of common sense and experience) right now, i have your thought, but not feeling (why not feeling? well, inorder for that to happen, i have to be there seeing you. but fact is, in rare case, people can capture your feeling) it's very hard to explain...
Posted on 2001-07-23 10:33:06 by disease_2000
Guys,

Lets keep in mind that this is a forum that has people from around the world come to it and it is important that everyone continues to feel welcome here.

From around the world there are many different cultures that are not always sympathetic to each others view so for very sensible reasons, we must avoid things that will cause offence to people from other cultures.

Beg,

I live in Australia which is becoming a highly multicultural society and among my friends and neighbours are people of your faith so I have some understanding of your enthusiasm for what you believe but to avoid any offence to others who are of a different faith or way of thinking, I would ask you as a favour to restrict your expression of your enthusiasm about the Prophet so that others are not challenged to retaliate with their own views.

This is a forum for assembler language programmers and we must all keep it this way, a forum of differing religious and cultural beliefs would fail and many good people would be offended.

This way we can all respect the views that others have and deal with each other in a friendly and open way.

Regards,

hutch@pbq.com.au
Posted on 2001-07-24 19:37:40 by hutch--
i missed this thread. now you know why i delete msg uh? i understand my stupidity.
Posted on 2001-07-29 13:37:22 by disease_2000