News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Binary to Decimal problem

Started by daniek91, February 24, 2012, 11:10:20 AM

Previous topic - Next topic

daniek91

Hi !
My task is to convert binary data written by user in console to decimal number.
I thought I'll save user input to "binary DB 128 dup(0)" and then read it backwards checking if i get 48 (ascii code for 0).
If I don't get 48 I wanted to add to my decimal the right multiplication of 2.
I want my program to do this until user writes 0.
Sadly it gives me wrong values..
That's what I've done so far :

.586P
.MODEL flat, STDCALL

STD_INPUT_HANDLE                     equ -10
STD_OUTPUT_HANDLE                    equ -11

GetStdHandle PROTO :DWORD
ReadConsoleA PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
WriteConsoleA PROTO :DWORD,:DWORD,:DWORD,:DWORD,:DWORD
ExitProcess PROTO :DWORD
wsprintfA PROTO C :VARARG


includelib .\lib\user32.lib
includelib .\lib\kernel32.lib

_DATA SEGMENT

new_line DB 0Dh,0Ah,0

hout DD ?
hin DD ?
rout DD 0
rinp DD 0
bufor DB 128 dup(0)
rbuf DD 128

writeInt DB " %d ",0
binary DB 128 dup(0)
binLength DD 0
decimal DD 0
square DD 1

_DATA ENDS

_TEXT SEGMENT

start:

invoke GetStdHandle,-11
    mov hout,EAX
    invoke GetStdHandle,-10
    mov hin,EAX

NEXT_NUMBER:

lea   EBX,binary
mov   ECX,rbuf
@@: mov   BYTE PTR [EBX+ECX-1],0
loop  @B

mov decimal,0
mov square,1
mov binLength,0

invoke ReadConsoleA,hin,OFFSET binary,rbuf,OFFSET binLength,0
dec binLength

GO_BACK:

dec binLength

xor eax,eax
mov EBX,binLength
mov AL,binary[ebx]
cmp AL,48
je SKIP

mov EAX,decimal
add EAX,square
mov decimal,EAX

SKIP:

mov EAX,square
mov EBX,2
mul EBX
mov square,EAX

cmp binLength,0
jne GO_BACK

lea   EBX,bufor
mov   ECX,rbuf
@@: mov   BYTE PTR [EBX+ECX-1],0
loop  @B

invoke WsprintfA,OFFSET bufor,OFFSET writeInt,decimal

mov rinp, EAX
invoke WriteConsoleA,hout,OFFSET bufor,rinp,OFFSET rout,0
invoke WriteConsoleA,hout,OFFSET new_line,2,OFFSET rout,0
invoke WriteConsoleA,hout,OFFSET new_line,2,OFFSET rout,0

cmp decimal,0
jne NEXT_NUMBER


push 0
call ExitProcess
_TEXT ENDS
END start


help me, please..
I've spend hours looking for errors..

Best regards,
Daniel


ninjarider

ummmmm. i'm having a problem converting hex to dec myself right now. but i noticed your not using a text string. from the looks of it your going from text binary to actualy binary.

daniek91

Yes, but then I use WsprintfA to get the right output :)

dedndave

QuoteMy task is to convert binary data written by user in console to decimal number.
I thought I'll save user input to "binary DB 128 dup(0)" and then read it backwards checking if i get 48 (ascii code for 0).
If I don't get 48 I wanted to add to my decimal the right multiplication of 2.
I want my program to do this until user writes 0.

that makes no sense
the user will enter ASCII "0"'s and "1"'s
ASCII "0" = 48, or 30h
ASCII "1" = 49, or 31h

so - subtract 30h from the ASCII character - it will give you binary 1 or 0
with each binary digit, you multiply the accumulated binary value by 2, then add the new digit
that yields a binary value - now, you want to convert binary to decimal, then to ASCII again

are you trying to work with decimal directly ?

dedndave

i have to ask - do you have the masm32 package installed ?

dedndave

the way it is written, you are getting 2 x (deisred result) + 1
maybe that will help debug it   :P

daniek91

Thanks for such fast reply  :bg
to be honest I have no idea why would it work like this  :(

dedndave

ok - the program is strangely written   :P
but - it's a good effort for a newbee   :U

i managed to make it work like so...
        invoke  ReadConsoleA,hin,OFFSET binary,rbuf,OFFSET binLength,0
        dec     binLength
        dec     binLength

GO_BACK:
        dec     binLength

daniek91

YES ! I WAS GOING TO POST THE EXACT SAME THING !!  :clap: :dance:

Seems that ReadConsoleA appends 3 signs at the end of string !

THANK YOU  :clap:

daniek91

Quotethe way it is written, you are getting 2 x (deisred result) + 1

that's what helped me the most :)

dedndave

i think it appends the Carriage Return and a NULL terminator
not sure - i usually use ReadFile instead of ReadConsole

daniek91

i thought that it appends CR and 0 as termination mark (no idea what's 3rd)..
anyways : Thank You Very Much  :cheekygreen:

jj2007

Quote from: dedndave on February 24, 2012, 12:39:43 PM
i think it appends the Carriage Return and a NULL terminator
not sure - i usually use ReadFile instead of ReadConsole

They both write Useful text, CR, LF, 0 to your buffer, and return the # of bytes read in eax. If edi points to your buffer, use mov byte ptr [edi+eax-2], 0 to set the zero delimiter.

dedndave

i thought ReadFile appended 2 bytes
let me play....

baltoro

DAVE !!!
I think we here at the MASM Forum should have a cash award for the most altruistic and helpful person.
...Maybe, even,...a Super-PAC,... :eek
I nominate you for this prestigious honor.
...So, it's either an all-expense trip to Las Vegas, or, free dental care for a year,...
Baltoro