Hello Everybody

Do anyone know how to convert a 4 BYTE STRING that is in a 5 BYTE Buffer TO a DWORD...I looked in the M32.lib but it do the opposite, like DWORD to Hex. I did an search but could not find anything about it.... Is it possible.

Thanks in advance.
Posted on 2002-05-29 23:36:32 by cmax
Umm...
far as I know, the MASM32 lib has functions to go back and forth.

there's dwtoa to create a string from a dword (which is what I assume you are talking about), but there's also atodw which creates a dword from an ascii (decimal) string.

just invoke atodw,addr stringname and use return value.

Alternatively, I would just write the loop:



lea esi,string
xor eax,eax
xor edx,edx
dec esi
@@: inc esi
mov dl, BYTE PTR [esi]
test dl,dl ;assuming ASCIIZ
jz @F
sub dl,30h
lea eax,[4*eax+eax] ;mul eax by 10
add eax,eax
add eax,edx
jmp @B
@@: whatever


I think that'll do it. You might wanna do some range checking to make sure dl is in [0,9] otherwise you'll get some weird stuff.

You can prolly shrink that down too....

--Chorus
Posted on 2002-05-29 23:55:08 by chorus
I am trying convert 4 numbers to a DWORD value and i just saw this in M32.lib, atodw just a minute ago also... I am going to see what i can do with it.

Thanks chorus
Posted on 2002-05-30 00:10:22 by cmax
I been working with them since i last post. Both of them works, but i have one problem... I tried to save it to a DWORD buffer in the .data? section and it will not do it. I push and pop it into EAX than try to save it and it still did not work, i get page fault... It don't seem to want to come out of the edi....I think your save it to the buffer but it went in word form, but that could have been my imagination i was trying so hard.

Is there something special that you got to do to be able to make a DWORD buffer.
Posted on 2002-05-30 02:34:48 by cmax
cmax you need to remember that a DWORD is simply 4 BYTEs. Due to the endian-ness of the intel processors, the bytes are technically reversed in memory so:
044332211h in memory is stored as 011h, 022h, 033h, 044h as consecutive bytes.

This will not matter if picking up and dropping strings, as they are reversed as they are fetched to a register, and reversed back when they are stored in memory. This reversal only comes in to play when you need to access the nth byte of a string when its been fetched into a DWORD register.

How do you want your conversion to take place?
Sum the 4 bytes in to a single DWORD integer?
Or just have a string in a DWORD value?

If you want to create a string of DWORDs, it will be exactly the same as a string of bytes, except the length will be a multiple of 4 bytes rather than a multiple of 1.

bytebuf db 32 dup(0)
dwordbuf dd 8 dup(0)

These are identical, except the internal type checking in MASM will require a DWORD PTR, or BYTE PTR to access as a DWORD or BYTE respectivly.

Mirno
Posted on 2002-05-30 04:16:02 by Mirno
cmax are you looking for:

bswap eax
Posted on 2002-05-30 06:54:01 by bdjames
bSwap...Well i guest back to the books for me... There enough post to read about bit operations ( roll, poll and Boul ) and all of those Opt Wars... I will find it all inside bitRAKE, Svin and Maverick posts. Now i know some of what they was taking about so now i can pay more attention... I did not expect to go that deep....But you GOT TO because its always something.

Thanks Guys
Posted on 2002-05-30 14:08:04 by cmax
Hi cmax





number db '1234',0

...

lea esi,number
lodsd ; eax = 31323334
bswap eax
xor eax,30303030h
xor ebx,ebx
mov bl,al
shr eax,4
add al,bl
mov bl,al
rcr eax,8
shr al,4
add ah,al
mov bh,ah
xchg eax,ebx ; eax = 00001234



have nice days!
Posted on 2002-05-30 16:28:30 by CYDONIA
here's my take at it. it should work with any 4 digit decimal number string and convert it hex. i left the number in hex cause i'm lazy.



number db "1234",0
...

lea eax, number
mov eax, [eax] ;eax = 34333231
bswap eax ;eax = 31323334
lea eax, [eax-30303030h] ;eax = 01020304
mov ecx, eax ;ecx = 01020304
ror ecx, 4 ;ecx = 40102030
or eax, ecx ;eax = 41122334
mov ecx, eax ;ecx = 41122334
shr ecx, 8 ;ecx = 00411223
and ecx, 0FF00h ;ecx = 00001200
and eax, 000FFh ;eax = 00000034
or eax, ecx ;eax = 00001234


ran 12 ticks on my k6-2
Posted on 2002-05-30 22:31:11 by Sloat