The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: daniek91 on February 24, 2012, 11:10:20 AM

Title: Binary to Decimal problem
Post by: daniek91 on February 24, 2012, 11:10:20 AM
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

Title: Re: Binary to Decimal problem
Post by: ninjarider on February 24, 2012, 11:31:15 AM
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.
Title: Re: Binary to Decimal problem
Post by: daniek91 on February 24, 2012, 11:58:56 AM
Yes, but then I use WsprintfA to get the right output :)
Title: Re: Binary to Decimal problem
Post by: dedndave on February 24, 2012, 12:03:01 PM
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 ?
Title: Re: Binary to Decimal problem
Post by: dedndave on February 24, 2012, 12:09:15 PM
i have to ask - do you have the masm32 package installed ?
Title: Re: Binary to Decimal problem
Post by: dedndave on February 24, 2012, 12:17:12 PM
the way it is written, you are getting 2 x (deisred result) + 1
maybe that will help debug it   :P
Title: Re: Binary to Decimal problem
Post by: daniek91 on February 24, 2012, 12:22:44 PM
Thanks for such fast reply  :bg
to be honest I have no idea why would it work like this  :(
Title: Re: Binary to Decimal problem
Post by: dedndave on February 24, 2012, 12:32:39 PM
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
Title: Re: Binary to Decimal problem
Post by: daniek91 on February 24, 2012, 12:34:59 PM
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:
Title: Re: Binary to Decimal problem
Post by: daniek91 on February 24, 2012, 12:36:08 PM
Quotethe way it is written, you are getting 2 x (deisred result) + 1

that's what helped me the most :)
Title: Re: Binary to Decimal problem
Post by: 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
Title: Re: Binary to Decimal problem
Post by: daniek91 on February 24, 2012, 12:49:13 PM
i thought that it appends CR and 0 as termination mark (no idea what's 3rd)..
anyways : Thank You Very Much  :cheekygreen:
Title: Re: Binary to Decimal problem
Post by: jj2007 on February 24, 2012, 02:27:55 PM
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.
Title: Re: Binary to Decimal problem
Post by: dedndave on February 24, 2012, 05:37:55 PM
i thought ReadFile appended 2 bytes
let me play....
Title: Re: Binary to Decimal problem
Post by: baltoro on February 24, 2012, 06:14:06 PM
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,...
Title: Re: Binary to Decimal problem
Post by: dedndave on February 24, 2012, 06:15:22 PM
it appears that they both append Carriage Return and Line Feed - no Null terminator
oh yah, i forgot - you have to terminate it yourself   :P
Title: Re: Binary to Decimal problem
Post by: dedndave on February 24, 2012, 06:24:15 PM
thanks, baltoro   :bg

Laughlin is more fun than Vegas - lol
what we need is an "Annual Masm32 Get-Together" in Laughlin NV
all expenses paid by Hutch, of course - then we can nominate him for the award   :bdg
(included are free "Masm32: Assembly Programmers Do It Faster" T-Shirts)
he probably already deserves the award
Title: Re: Binary to Decimal problem
Post by: jj2007 on February 24, 2012, 06:59:48 PM
Quote from: dedndave on February 24, 2012, 06:15:22 PM
it appears that they both append Carriage Return and Line Feed - no Null terminator
oh yah, i forgot - you have to terminate it yourself   :P

Here is one to play. For friends of Olly, I have assembled it with symbols, and it will stop right before the ReadFile call with an int 3 :bg

include \masm32\MasmBasic\MasmBasic.inc   ; download (http://www.masm32.com/board/index.php?topic=12460)
   Init
   mov f2sOlly, 111   ; a hack
   Let esi=Input$("What's your name again? ", "Dave")
   Inkey "My name is ", esi
   Exit
end start