;converting a value to it's ascii representation of binary


.486
.model flat,stdcall
option casemap:none

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


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


.data
;myarray db '0123456789ABCDEF',0
buff db 8 dup(?)
titlestring db "ascii2binary",0
szformat db "%u",0

.data?

.code
start:
xor eax,eax
xor ecx,ecx
;mov ecx,8
mov eax,42 ;hex value for ascii character A
do_again:
shr eax,1

jne once_more
mov [buff+ecx],'0'
inc ecx
cmp ecx,8
jc done
jmp do_again

once_more:
mov [buff+ecx],'1'
inc ecx

;jnc done
jmp do_again
done:
invoke wsprintf,ADDR buff,ADDR szformat,eax
invoke MessageBox,0,ADDR buff,ADDR titlestring,MB_OK
invoke ExitProcess,0
end start

it gives me only decimal numbers why is that?
Posted on 2004-04-07 02:38:09 by Tweak
Because you're overwriting it right afterwards with wsprintf?
Anyway, that code won't do much good. Try this instead.


.486
.model flat
.includelib \masm32\lib\user32.lib
.includelib \masm32\lib\kernel32.lib
.code
titlestring db "ascii2binary",0
start:
mov al,42
mov edi,buff
push 0
push addr titlestring
push edi
bswap eax
push 8
pop ecx
@@:mov al,24
rol eax,1
stosb
loop @B
push ecx
call MessageBox
push 0
call ExitProcess
end start
Posted on 2004-04-07 06:03:08 by Sephiroth3
Only other way, with more data :D (dunno if is correct ...)




ascii0 db '0000',0
ascii1 db '0001',0
..
asciiF '1111',0

arrayOfAddr dd ascii0, ascii1,..., asciiF

xor eax, eax
mov al, value
mov ecx, eax
shr ecx, 4
mov eax, [arrayOfAddr+eax*4]
invk strcpy, buffer, eax
lea eax, [bufferOfAddr+4]
mov ecx, [array+ecx*4]
invk strcpy, eax, ecx



???


Have a nice day or night.
Posted on 2004-04-07 11:28:46 by rea
Here's another :

DW2Bin FRAME pOutput,dwValue

uses edi

mov edi,[pOutput]
mov eax,[dwValue]
mov B[edi+32],0
mov ecx,31
L1:
ror eax,1
setc dl
or dl,30H
mov [edi+ecx],dl
dec ecx
jns L1
RET
ENDF
Posted on 2004-04-07 12:01:21 by donkey
Here's how I did figure it out after playing with it last night


;converting a value to it's ascii representation of binary

.486
.model flat,stdcall
option casemap:none

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


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


.data
hex_chars db '0123456789ABCDEF',0
buff db 8 dup(?)
titlestring db "ascii2binary",0
szformat db "%u",0

.data?

.code
start:
xor eax,eax
xor ecx,ecx
xor ebx,ebx
lea esi,hex_chars
lea edi,buff

mov eax,42 ;value
push eax ;save this value
and al,0F0h ;isolate upper nibble
shr eax,4 ;adjust the upper nibble so it is in bits 0-3
mov dl,[esi+eax]
mov [edi],dl
inc edi
pop eax
and al, 0Fh ;isolate lower nibble
;shr eax,4
mov dl,[esi+eax]
mov [edi],dl
inc edi
mov byte ptr [edi],0

;invoke wsprintf,ADDR buff,ADDR szformat,edi
invoke MessageBox,0,ADDR buff,ADDR titlestring,MB_OK
invoke ExitProcess,0
end start

thanxs for your replies
Posted on 2004-04-07 12:12:48 by Tweak
Oh, I misunderstood. You weren't looking for binary you were looking for HEX. Totally different, ignore my post.
Posted on 2004-04-07 12:22:23 by donkey
Blah, why did you write binary then? Lol.

If you want to convert to hex, then you might as well do it this way:


.486
.model flat
.includelib \masm32\lib\user32.lib
.includelib \masm32\lib\kernel32.lib
.code
titlestring "ascii2binary",0
start:
mov edi,addr buff
push 0
push addr titlestring
push edi
mov al,42
bswap eax
push 2
pop ecx
@@:
rol eax,4
and al,15
daa
cmp al,16
sbb al,207
stosb
loop @B
mov [edi],cl
push ecx
call [__imp__MessageBox@16]
push 0
call [__imp__ExitProcess@4]
end start
Posted on 2004-04-07 13:12:21 by Sephiroth3
Also I misunderstood like donkey... maybe is because I read "ascii2binary"? is not bintoHex in ascii?


Have a nice day or night.
Posted on 2004-04-07 22:38:24 by rea
No, you understood me right that was my
intentions. But later out of frusation I wrote it that way. Forgive me so bascially I could shift the bits off to get it's binary value huh?:confused:
Posted on 2004-04-08 01:42:44 by Tweak

No, you understood me right that was my
intentions. But later out of frusation I wrote it that way. Forgive me so bascially I could shift the bits off to get it's binary value huh?:confused:


Yes, that's right. My routine isn't really optimized yet. It takes about 118 clocks for a 32bit number but I haven't tried to make it fast yet. I changed the SHR eax,1 to ROR eax,1 this will cause it to return the value passed to it, might be more convenient that way.
Posted on 2004-04-08 02:24:26 by donkey

Here's how I did figure it out after playing with it last night


;converting a value to it's ascii representation of binary

.486
.model flat,stdcall
option casemap:none

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


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


.data
hex_chars db '0123456789ABCDEF',0
buff db 8 dup(?)
titlestring db "ascii2binary",0
szformat db "%u",0

.data?

.code
start:
xor eax,eax
xor ecx,ecx
xor ebx,ebx
lea esi,hex_chars
lea edi,buff

mov eax,42 ;value
push eax ;save this value
and al,0F0h ;isolate upper nibble
shr eax,4 ;adjust the upper nibble so it is in bits 0-3
mov dl,[esi+eax]
mov [edi],dl
inc edi
pop eax
and al, 0Fh ;isolate lower nibble
;shr eax,4
mov dl,[esi+eax]
mov [edi],dl
inc edi
mov byte ptr [edi],0

;invoke wsprintf,ADDR buff,ADDR szformat,edi
invoke MessageBox,0,ADDR buff,ADDR titlestring,MB_OK
invoke ExitProcess,0
end start


How can I make this a function, instead of a macro the programmer's ref for MASM
did not talk about writing procs
Posted on 2004-04-13 15:05:05 by Tweak
Hi tweak,

That function only does one byte. If you want to do a dword you should look at biterider's dw2hex :

dw2hex FRAME dwValue, lpBuffer

uses edi

; By Biterider

mov edx, [dwValue]
mov ecx, edx
shr edx, 4
and edx, 0F0F0F0Fh
and ecx, 0F0F0F0Fh

mov eax, edx
mov edi, ecx

add edx, 80808080h - 0A0A0A0Ah
add ecx, 80808080h - 0A0A0A0Ah
shr edx, 4
shr ecx, 4
not edx
not ecx
and edx, 07070707h
and ecx, 07070707h
add edx, eax
add ecx, edi
add edx, 30303030h
add ecx, 30303030h

mov edi, [lpBuffer]
mov B[edi + 7], cl
mov B[edi + 6], dl
mov B[edi + 5], ch
mov B[edi + 4], dh
shr ecx, 16
shr edx, 16
mov B[edi + 3], cl
mov B[edi + 2], dl
mov B[edi + 1], ch
mov B[edi + 0], dh
mov B[edi + 8], 0

ret
ENDF


You can just change the FRAME to PROC ect... to convert it to masm, there is no assembler specific syntax in it.
Posted on 2004-04-13 15:20:07 by donkey
The one that you put is not exaclty a macro, is more a function because it have a entry point and exit point(some functions have more that one exit point) and a portion of code (or data that will be readed as code ) that will be executed, sure that the function dont have a name, the start is only marked with start:


A function is that, make operations and return some, stricly a function is diferent from a procedure but normally I dont diferentiate them (in programming because I see that a procedure is at the end like a hided function... I am navigating you with my opinions no??), ok.


A function is basically a block of code for do some, like donkey say, between assemblers is not so widely used only one way for mark a block of code that mean a function.



When a function is writed, you only need to aproach the function objetive, you dont care exactly on how the arguments are passed to the function but more how they are manipulated inside and what they are or mean.

Because a function is a block of code with specific meaning (or objetive), you need mark this block in some way and give this block a name inside your code (and in that way, you can call it with that name). Maybe there are two way in general for mark the block.


functionNAME markOfStartFunction
BLOCK
markOfEndFunction

markOfStartFunction functionNAME
BLOCK
markOfEndFunction


Also you will only mark blocks of code, and normally not data ;), because data mean for you some, but if the computer try execute it, maybe it will cause some errors, and yes code is also data only when is executed (marked with eip) have meaning of code, see that this diference is only at level of computer, because for us data is data, and code is code :D.


Have a nice day or night.
Posted on 2004-04-14 10:14:49 by rea