This is a piece of code I found in the web, and it works well.
But the DWORD var X define as "X dword ****b", when I Input a bin number into a Edittext and use GetDlgItemInt or GetDlgItemText to get the number it does not end with b, How can I do that?(just as I define the DWORD var X without b ended). THX.
.386
.model flat,stdcall
option casemap:none

includelib msvcrt.lib
printf PROTO C :dword,:vararg

.data
X dword 00011000111011110001001101100101b

dStr byte 10 dup(?),0     
szFmt  byte 'dec format = %sd',0ah   

.code
start:

mov ecx,10       
mov eax,X       
xor edx,edx         
mov ebx,10

f70:
    div ebx         
add dl,'0'         
mov dStr,dl 
xor edx,edx         
    loop f70           

invoke printf,offset szFmt,\
  offset dStr

ret
end start
Posted on 2006-04-10 20:42:09 by Eric4ever
Well, GetDlgItemInt is definitely not what you want, it will ignore the b and treat the digits as a decimal number. GetDlgitemText should work, make sure that the buffer you allocate is large enough to hold all of the digits, the b and the terminating NULL.
Posted on 2006-04-10 20:48:41 by donkey

Well, GetDlgItemInt is definitely not what you want, it will ignore the b and treat the digits as a decimal number. GetDlgitemText should work, make sure that the buffer you allocate is large enough to hold all of the digits, the b and the terminating NULL.


Hi Donkey?

How can I allocate the buffer ended with b and the terminating NULL?
I try to define the "X    byte  4  dup(?),'b',0" but it works wrong.
Posted on 2006-04-11 02:46:35 by Eric4ever
Hi Eric4Ever,

I think I have misunderstood what you want, do you want the user to type in a string like "010011b". In that case you must allocate a buffer of 8 bytes, 6 for the digit, 1 for the "b" and 1 for the NULL...

xxx DB 8 DUP (?)

The buffer size should be at least 1 character larger than the maximum number of characters you expect the user to type.
Posted on 2006-04-11 07:22:38 by donkey

Hi Eric4Ever,

I think I have misunderstood what you want, do you want the user to type in a string like "010011b". In that case you must allocate a buffer of 8 bytes, 6 for the digit, 1 for the "b" and 1 for the NULL...

xxx DB 8 DUP (?)

The buffer size should be at least 1 character larger than the maximum number of characters you expect the user to type.


Hi Donkey,

I just want user to type the 0/1 numbers(max 32 bit - a 8 bin hex) without the terminating 'b' in the edittext and I've no idea how to do it.
THX.

Eric
Posted on 2006-04-11 20:30:54 by Eric4ever
Eric4ever,
If you want the user to just enter the binary digits you can
make the data string large enoug to hold the digits you expect +
the b and the null.

then just use the GetDlgItemText to get the text into the
data string then get the string length using lstrlen and
add that value to a pointer that points to the data string
then add the 'b' and null to the end of the string or use
lstrcat to add just a 'b' which will also add your NULL for you.

Zcoder....
Posted on 2006-04-11 20:50:12 by Zcoder

Eric4ever,
If you want the user to just enter the binary digits you can
make the data string large enoug to hold the digits you expect +
the b and the null.

then just use the GetDlgItemText to get the text into the
data string then get the string length using lstrlen and
add that value to a pointer that points to the data string
then add the 'b' and null to the end of the string or use
lstrcat to add just a 'b' which will also add your NULL for you.

Zcoder....


Zcoder,

I get the method but how can I put the string ended with 'b' and null into eax, there is something wrong with my code:
    .data
XBuf  db  32 DUP (?),0
    .const
szb  db  'b',0
    .code
invoke    GetDlgItemText,hWinMain,IDC_BIN,addr XBuf,sizeof XBuf
invoke    lstrcat,addr XBuf,addr szb
;_DebugString "Display mem string",addr XBuf      ; e.g.: Input:1010101, here is 1010101b
xor eax,eax
mov    eax,dword ptr XBuf    ;  eax = 30313031H, it should be 00000055H


but the code in the first posted is OK:
    .data
X dword 1010101b
    .code
start:
mov ecx,10       
mov eax,X    ; hera eax = 00000055H
....
Posted on 2006-04-12 02:13:19 by Eric4ever
Eric4ever,
Are you wanting the string converted to a dword?

Zcoder....
Posted on 2006-04-12 03:29:37 by Zcoder

Eric4ever,
Are you wanting the string converted to a dword?

Zcoder....


Yes, bin2dec, just the topic name..
Posted on 2006-04-12 07:07:30 by Eric4ever
What's wrong with it? Any answers would be appreciated
    .data
XBuf  db  32 DUP (?),0
    .const
szb  db  'b',0
    .code
invoke    GetDlgItemText,hWinMain,IDC_BIN,addr XBuf,sizeof XBuf
invoke    lstrcat,addr XBuf,addr szb
;_DebugString "Display mem string",addr XBuf      ; e.g.: Input:1010101, here is 1010101b
xor eax,eax
mov    eax,dword ptr XBuf    ;  eax = 30313031H, it should be 00000055H


but the code in the first posted is OK:

Code:
    .data
X dword 1010101b
    .code
start:
mov ecx,10       
mov eax,X    ; hera eax = 00000055H
....
Posted on 2006-04-13 22:38:14 by Eric4ever
Eric, GetDlgItemText retrieves the user input as an ASCII string. You can't simply read this as a DWORD value, you need to convert it. This involves iterating over each character in the string, determining if it's a correct value ('0' or '1'), et cetera.

The algorithm is simple, the bit shift instructions (SHL or SHR, depending on how you do stuff) and bitwise OR are your friends :)

EDIT: try this on for size
EDIT-2: when entering, ECX must point to the string. String must consists of 0 and 1 chars only, and should not end with a 'b'.


xor ebx, ebx ; output number
xor eax, eax ; temporary value thing

@@loop:
mov al,
test al, al ; terminating 0-byte?
jz @@done ; yup, we're done

shl ebx, 1 ; "add room for another digit"

sub al, '0' ; convert ASCII to 0-or-1
cmp al, 1 ; is decimal value > 1?
jg @@bad ; if yes, bad user input, bail out

or ebx, eax ; "add" digit to result
inc ecx ; point at next input char
jmp @@loop
@@done:

; at this point, ebx is the machine representation of the user
; binary string
invoke ExitProcess, 0

@@bad:
; show error
invoke ExitProcess, 1

Posted on 2006-04-13 23:00:10 by f0dder