hello all ? ;)

long time no see....
for some reasons im forced to head back ? :lol:

i have this loop :
``@_Loop:	mov eax,dword ptr		cmp eax,"1111"	je @_1111	cmp eax,"2111"	je @_1112	cmp eax,"3111"	je @_1113	cmp eax,"4111"	je @_1114	cmp eax,"2222"	je @_2222	@_Next:``

i have a lots of functions 1111,1112,1113,1114....etc
each one makes something.. they are functions ... theres why the JE

i was wondering.. for my needs, i need to speed the process up!
is there another way to make that?
another way to go arround all those CMP ???

thanks a lot
Posted on 2006-05-23 18:26:24 by GR33d
Hi

Depending on the range of data in EAX, if the only difference in the strings is the lower 2 bytes (in decimal) then you can have a maximum of 99 possible combinations. In order to make use of this you create an array of 99 dwords containing the addresses to your functions then use this...

``array11 dd offset 1111array12 dd offset 1112...array99 dd offset 1199.lop  and eax,0FFh  sub ax, "00"  mov eax,   test eax,eax ; test for empty value  jnz eaxjmp .lop``
Posted on 2006-05-23 18:36:43 by donkey
Sorry,

Brain fart, you're going to have to convert the value in AX from BCD in order for it to work properly. I don't have the refs at work but I think AAD might do it so it would be

``array11 dd offset 1111array12 dd offset 1112...array99 dd offset 1199.lop  and eax,0FFh  sub ax, "00"  aad  mov eax,   test eax,eax ; test for empty value  jnz eaxjmp .lop``
Posted on 2006-05-23 18:44:54 by donkey
The basic first step would be to analyze your program and put the most frequently used compares at the top of the list.

The next basic step, if your list is big enough, is to split the list of compares in halves (and maybe even quarters), in order to speed things up a bit.

Here is a basic example assuming you have multiple functions that start

``@_Loop:	mov eax,dword ptrcmp eax,"0002"jge @check_2000		cmp eax,"1111"	je @_1111	cmp eax,"2111"	je @_1112	cmp eax,"3111"	je @_1113	cmp eax,"4111"	je @_1114check_2000:	cmp eax,"2222"	je @_2222; and so on	@_Next:``

A round-about way is to setup a linked-list of function pointers and use a little bit of math to navigate them, though this could be messy considering the gap you have between 1114 and 2222. Short example (Please forgive me as I favor NASM over MASM and my "grammar" knowledge of MASM is quite weak these days)...

``list_of_functions:  .1111 DWORD @_1111  .1112 DWORD @_1112  .1113 DWORD @_1113  .1114 DWORD @_1114  .2222 DWORD @_2222@_Loop:  mov eax, dword ptr  sub eax,1111  shl eax,2  mov ebx, addr list_of_functions  mov edx, dword ptr  jmp edx@_Next:``

This would, of course, break as soon as you hit .2222, but I think you get the general idea.

You could make this for the entire list of functions, but... Space, Speed, Size... pick any 2 :)

Edit: I see donkey beat me to the punch, twice over, hope I was somewhat insightful ;)
Posted on 2006-05-23 18:45:48 by SpooK
hey

thats a help :)

someone could translate that from NASM to MASM :P?  dunno how to MASM it

oh...
and donkey ...

oh and when u mean:
``array11 dd offset 1111``

u mean :
``array11 dd offset @_1111``
???? the label or the value "1111"

can u explain that a bit more ?

cause ... the user will input "1111" in string witch means : 31313131 on EAX
any way faster i appreciate

thanks indeed
Posted on 2006-05-23 20:05:50 by GR33d
"offset" suggestions the location in memory of label "1111".

This is a major difference between MASM and NASM. MASM assumes you are addressing a value in memory, instead of a memory location. NASM defaults to memory locations of labels, and you have to specify if you are accessing the value at that memory location. Example...

``;Setting a Variable to a Label's Memory Locationarray11 DWORD addr @_1111   ;MASMarray11 DD  ._1111          ;NASM;Loading/Storing Variablesmov eax, array11            ;MASMmov eax, DWORD     ;NASM;Loading Variable Memory Locations (labels)mov eax, addr @_1111     ;MASMmov eax, ._1111          ;NASM``
Posted on 2006-05-23 20:31:51 by SpooK
and what abou the BCD and AAD stuf?!?!

what that means?
Posted on 2006-05-24 07:38:16 by GR33d

and what abou the BCD and AAD stuf?!?!

what that means?

Shooting from the hip...

BCD = Binary Coded Decimal (not an actual instruction)
Posted on 2006-05-24 07:44:49 by SpooK
Hi,

SpooK is right BCD is Binary Coded Decimal which is a representation of numbers in binary format where each byte has a range of 0..9. AAD is an instruction that will multiply AH by 10 and add it to AL which in effect converts AX from BCD to binary.

BTW as an added bonus there is an undocumented behaviour to AAD which allows you to multiply by any radix, the encoding for AAD is:

D5 0A

If you manually change the 0A to a different radix (say 08) you can convert to a different number base (octal for 08).
Posted on 2006-05-24 08:14:47 by donkey
but .... errmmm

sorry my supidness

cos im not an real expert

but

whats all that has to do with my cmps ?
is there any good thing in using that?
Posted on 2006-05-24 09:23:29 by GR33d
If you do everything right, you can convert a BCD-encoded parameter into an offset to function.

LIKE:

1) ax= "11" (031h, 031h)
2) sub ax, "00"    ; now ax == 00101h, because "00" == 03030h
3) aad                ; now ax == 00Bh

Now you do some magic (like adding, shifting, or whatever) to create an offset from this 00Bh. Donkey showed you how you can do it.
Posted on 2006-05-24 11:01:56 by ti_mo_n
Hi GR33d

There are several ways to store a number in memory. Two of the most common are binary and BCD (though there are others such as EBCIDIC,SyncSafe etc...). In BCD numbers are stored as a human would read them, for example 99 would be 09,09 in the low order 2 bytes. In binary 99 would be 063h.

The CPU does not work directly with BCD numbers so they must be converted to binary before using them for addressing, to do this we use the AAD instruction, it is designed to convert packed BCD to binary. If we did not convert to binary the CPU would assume that we actually mean 0909h which is 2312 and our resulting displacement would be wrong. Obviously, BCD is very wasteful so it is rarely used except when necessary.

Donkey
Posted on 2006-05-24 12:32:15 by donkey
ohhhhhh

so thats veeeryyy nice
cause
i think this is one of my other big problems

there are the functions "1111" "1112" ...etc ( this was resolved .. thx guys )

and now i have address in decimal like .. the user will input "00001000" ( only 0..9.. dec as i said )

with the AAD and BCD stuff can i get the real address in HEX ?
or i need to make an algo witch converts from the string "00001000" to a values in hex on eax,ebx.. etc?

thx

Posted on 2006-05-24 13:53:21 by GR33d
If you have an ascii string like "0124362", you'll need to convert it to binary computer representation before you can make computations with the number. If you use hutch's MASM32 package, one of the safe-to-use routines from the m32lib would be "a2dw" or "atodw" or something similar. Or you could check ParseRadix from http://srcvault.scali.eu.org/ .

How to progress from there depends a bit on your source. How many functions like "1111", "1112" etc. are there? Are they contiguous, or are there "holes"? Etc.
Posted on 2006-05-24 14:03:01 by f0dder
thx f0dder
i will be checking hutchs packages and functions...

well
im working in a procject with a friend of mine but
there would be like ... thousands ... 1000~+ functions
theres why the need for a speeded process

thx
Posted on 2006-05-24 14:12:10 by GR33d
Can you give a few more details?

If you have functions like 1000-5000 (or a similar "closed range"), a jump table would probably be a good way to handle branching. If there's multiple ranges but without too large gaps, a jump table can still be a good solution. If there's multiple closed ranges with large gaps, a series of jump tables could be used. If there's a lot of numbers with a lot of "holes", a binary search could be applied, etc.

PS: not to diss the masm32 project, a great deal of work has gone into it, just be advised that some of the m32lib routines are bugged and the code isn't always top-notch quality. It's often a good starting point, though.
Posted on 2006-05-24 14:30:44 by f0dder
there will be close ranged functions
about 5000 functions in the end of the project...

my friend just let me know that we can use hex as the function ids ..
insted of user input "1111" we can use "1F32" or something like that...

the user will input string "2F3E" and i need to goto that function in asm code....

is there another way to make the process/jump table with that?

thx
Posted on 2006-05-24 19:01:15 by GR33d
1) Convert this hex string to a dword value (ascii 2 dword)
2) use the result from point 1 as your index in a jump table.
Posted on 2006-05-24 19:21:46 by ti_mo_n
that seems to work

dunno if im making this of a big cause but my only intention is to understand everything very well

convert the value string "FE3A" to dword is easy
but about the jump tables ?

can someone make a small example of a indexed jump table like  ti_mo_n said?

thanks
Posted on 2006-05-25 07:10:20 by GR33d
My first post has an indexed jump table.
Posted on 2006-05-25 08:03:09 by donkey