Author Topic: convert binary number to decimal  (Read 9977 times)

Offline talor115

  • New Member
  • Posts: 1
convert binary number to decimal
« on: June 16, 2015, 10:32:48 AM »
Hello, I need help with my code.
The user enters a binary number (1 and 0 only) the output is the decimal value of the input.
if the user enters a digit between 2-9 it goes to error label.
Here is the problems:
When I enter more than 10 digits it goes to error label. I need to enter 32 digits.
Also when I enter a number starting with zero (like 011) it goes to error label.
---------------------------------------------------------------------------------------------------------------
here is the code:
Code: [Select]
%include "asm_io.inc"

segment .data
msg1 db "Enter a number: ",0
msg2 db "Bad Number! try again. ",0
msg3 db "Enter Decimal value is: ",0

segment .bss
mask resd 1

segment .text
global main

main:
enter 0,0
pusha

mov dword[mask],1
mov esi,10
mov ebx,0


input:
mov eax,msg1
call print_string
call read_int
mov ecx,eax

next:
cmp eax,0
je continue
mov edx,0
div esi
cmp edx,1
ja error
jmp next

error:
mov eax,msg2
call print_string
call print_nl
jmp input


continue: mov eax,ecx

convert:
cmp eax,0
je print
mov edx,0
div esi
cmp edx,0
je shift_1
or ebx,[mask]

shift_1:
shl dword[mask],1
jmp convert
 
print:
mov eax,msg3
call print_string
mov eax,ebx
call print_int
call print_nl

        popa
    leave
    ret
« Last Edit: June 16, 2015, 03:57:11 PM by Frank Kotler »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: convert binary number to decimal
« Reply #1 on: June 16, 2015, 06:26:26 PM »
Well, that's interesting, talor115!

I ASSume that "asm_io.inc" is Dr. Carter's stuff. I imagine you know that "read_int" expects to read a decimal representation of a number, and you probably know that it depends on "scanf". IMHO, "scanf" is not libc's finest hour! That may just be because I don't understand it very well...

Entering more than 10 digits, interpreted as a decimal representation of a number, will cause an overflow. I'm not too surprised that doesn't work. Entering a number starting with "0" is more of a surprise. Apparently, "scanf" is interpreting this as an octal representation of a number! That's the way C does things, I guess, but it was a surprise to me!

In short, I don't think you're going to be able to use "read_int" for this. Bummer, 'cause I thought it was pretty clever the way you were doing it! I think you're going to have to start with "read_string" and parse the string as a binary representation of a number yourself. This shouldn't be too difficult - give a yell if you have trouble with it. (I may give it a shot if I'm in the mood...)

Best,
Frank


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: convert binary number to decimal
« Reply #2 on: June 17, 2015, 09:08:19 AM »
Well, I see that knm241 has given you an answer over on SO. Are you getting anywhere with his code?

There's a slight problem with my idea of using Dr. Carter's "read_string" - he does not provide such a function. There's a "read_char" that we could use. We could just use "gets()", but that's too horrible to contemplate. I guess "fgets()" would be the way to go, but it would involve declaring it... and "stdin". I'm trying to stay within what "asm_io.inc" provides. "read_int" is just a wrapper around "getchar()"...

knm241 mentions that input from console/terminal is "cooked". What that means, for our purposes, is that nothing gets returned until the user hits "enter". This works "almost" as if we were getting characters one at a time and evaluating them, but in the event of invalid input, sometimes there's more cruft "in the queue" that messes up the error reporting. I'm not quite happy with it yet.

I'm allowing the full 32 digits. However, Dr. Carter's "print_int" works with signed integers. If we use the full 32 characters, this will push the number into a range that will be interpreted as a negative number. This is "correct" enough, but may not be the "expected" result. We can display an unsigned integer... but not using "print_int"... What's the "customer specification" on that?

Well, I've got a "kinda working" version I can show you, but let us know how you're doing with your attempts to solve it.

Best,
Frank