hi,

i?m trying to translate a macro made in TASM to NASM syntax, but i?m having problems.



hash_string macro s
hash = 0
len = 0
irpc c, <s>
len = len + 1
endm
i = 0
irpc c, <s>
if i ne 0
if i ne (len-1)
hash = ((hash shl 7) and 0FFFFFFFFh) or (hash shr (32-7))
hash = hash xor '&c'
endif
endif
i = i + 1
endm
endm


my nasm code is looking this way:



%macro hash32_str_c 1
%assign hash 0
%strlen len %1
%assign i 0
%rep len
%ifeq i != 0
%ifeq i != (len-1)
%assign hash ((hash shl 7) and 0FFFFFFFFh) or (hash shr (32-7)) ;***
%assign hash (hash^%1[i]) ;***
%endif
%endif
%assign i (i+1)
%endrep
%endmacro


needless to say that my macro dont compile. the main problem is the 2 innermost lines(marked with ;***), where i try to modify hash and access each character of string.

anybody know how this can be fixed?

ancev
Posted on 2004-01-21 19:03:50 by ancev
I nice, what are you doing?, by the way, I look at your linker ;)... not have much time for go in dep :S



I provide you with links to specific sections in the nasm docs, also I cant handle %ifeq, that is not a preprocessor directive that is acepted by nasm. also thing in what is expressed in the line....

%ifeq i != 0 ... you are saying or expressing some like if equal i diferent from 0, I understand equal and diferent at the same time....., also I am thinking that maybe you have some hll macros??? (nagoa?), they have some contructs like this, but they are macros, and a macro dont need %, this hlls macros are used for generate jumps and coparisions.. etc, dunno for what you dont get a error there, but anyway %ifequ is not a directive.....

Ok, here the links:

Expressions
substr Extract a char from string
%if


That is suficient for not get errors, for example, you can use understanding the above: hash32_str_c "hola"

and no errors

Also if you will need a type of label, or some... dunno, is night here, but look at:
Macro Local-labels
Context Locals Labels

Also you can question any that you whant, but I let you with this, hope this help you.


Have a nice day.
Posted on 2004-01-22 01:30:38 by rea
humm, going to do imports by hash instead of name? :)

Can't help you with the macro, sorry - I usually use some tools to generate those hashes. Perhaps Jibz can give a hand, he's done fancy nasm macro stuff before :)
Posted on 2004-01-22 03:29:00 by f0dder
f0dder,

you?re right :) i want to import by hash

hgb,

thanks for the tips. i fixed the %if structures. it was a error. the code now look like this:



%macro hash32_str_c 1
%assign hash 0
%strlen len %1
%assign i 0
%rep len
%if i <> 0
%if i <> (len-1)
[B] %assign hash ((hash shl 7) and 0FFFFFFFFh) or (hash shr (32-7))[/B]
[B] %assign hash (hash^%1[i])[/B]
%endif
%endif
%assign i (i+1)
%endrep
%endmacro


the problem seens to be in the math operations i do over 'hash' variable, and the single char access i try to do to the macro param (%1). i?m afraid that NASM dont allow the later.

ancev

ps: this macro is used like that



hash32_str_c "MyString"
dd hash


or


hash32_str_c "MyString"
cmp eax, hash
Posted on 2004-01-22 11:31:49 by ancev
Hm, I dunno if you can do single-element access like that, but it should definitely be possible to extract a single element from the input... perhaps something like masm's irp. Can't remember details though, it's quite a while since I did nasm macro stuff.

Also, isn't it possible to "return" a value from a macro, so you don't have to do "dd hash" afterwards, but can do dd hash32_str_c('bleh') ?
Posted on 2004-01-22 11:43:34 by f0dder
Jibz had a go at it :)



[20:30:51] <Jibz> %macro hash32_str_c 1
[20:30:51] <Jibz> %assign hash 0
[20:30:52] <Jibz> %strlen %%len %1
[20:30:52] <Jibz> %assign %%i 1
[20:30:52] <Jibz> %rep %%len
[20:30:52] <Jibz> %assign hash ((hash << 7) & 0FFFFFFFFh) | (hash >> (32-7))
[20:30:54] <Jibz> %substr %%ch %1 %%i
[20:30:54] <Jibz> %assign hash (hash ^ %%ch)
[20:30:56] <Jibz> %assign %%i (%%i+1)
[20:30:56] <Jibz> %endrep
[20:30:58] <Jibz> %endmacro


- he mentioned that it seems like the original doesn't include first and last char of the string in the hash... accident or on purpose? There's still improvements to be made, like figuring out how to make it work like "blahblah dd hash32_str_c("woot!")". Enjoy :)
Posted on 2004-01-22 13:57:29 by f0dder
f0dder,

it work perfect. thanks very much, and please thanks jibz for me :)

these bytes where skipped on porpouse ;) they where used as flags

ancev
Posted on 2004-01-22 17:45:24 by ancev
oki doki :)

I really do want a version where the macro returns a value, if possible in nasm. I'll thank jibz for you - have fun, and use the code responsibly.
Posted on 2004-01-22 17:48:10 by f0dder
%1 that is not aceptablein nasm, that is for what I put: substr Extract a char from string.
Also you can use %error hash after do your hash for watch the value.

I will explain some... strlen return the strlen ;) (like suposed to do ;))... ,then %strlen %1, and if you pass hashit "hola", you have in len the strlen, this is a 4, but if you extract the first character, is not the h, is the ' ;), this is for what in the loop, you check that is not equal to zero the i index, because extract this will cause try hash the symbol ', also by this, %rep len dont have the "normal" way of check alll the string... this is because for check all the string you need a loop that check len+1, also you try another aproach, check this next out:




%macro hash32_str_c 1
%assign hash 0
%strlen len %1
%error >>> %1 len
%assign len len+1
%assign i 0
%rep len
%error ---------------- i
%if i > 0
%if i < (len)
%assign hash ((hash << 7) & 0FFFFFFFFh) | (hash >> (32-7))
%substr char %1 i
%assign hash (hash^char)
%error [ i ] = char hash
%endif
%endif
%assign i i+1
%endrep
%error ==== i
%endmacro

%macro hashit 1
%assign hash 0
%strlen len %1
%assign i 1
%rep len
%assign hash ((hash << 7) & 0FFFFFFFFh) | (hash >> (32-7))
%substr char %1 i
%assign hash (hash^char)
%error < i > = char hash
%assign i i+1
%endrep
%endmacro

hash32_str_c "hola"
%error -------------------------------
hashit "hola"



see that hashit output the same values ;), also the symbol hash is accesible elsewhere, this is, if you trymake a macro with name hash you will have some troubles..., because hash will by replaced by the last value of hash ;), also you can make

Also I think no include the last character is a error.. of when was doit the other macro,for not check this thing, about the strlen, and initialize the first character at 1 and not 0.


After watch at the output, you can comment or delete the lines with %error, for dont watch this... ;)

By the way, this last to calculate the hash for all the letters in the string, also the hashit is the same that the anterior, but with a little cleaning.

Have a nice day or night.
Posted on 2004-01-22 22:44:58 by rea