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.
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
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.
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.
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.
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 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
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....
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....
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
....
Eric4ever,
Are you wanting the string converted to a dword?
Zcoder....
Are you wanting the string converted to a dword?
Zcoder....
Eric4ever,
Are you wanting the string converted to a dword?
Zcoder....
Yes, bin2dec, just the topic name..
What's wrong with it? Any answers would be appreciated
but the code in the first posted is OK:
.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
....
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'.
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