Full Version: bcd to binary
sharath411
Jun 18 2011, 4:15 AM
Hi friends

how to convert a 24 bit bcd to binary for 8051 in assembly. Ex: 100000 to 186AD

Thank You,
Sharath

majoka
Jun 18 2011, 4:42 AM
@ sharath411
ru see this
also this one

Attachment

sharath411
Jun 18 2011, 5:03 AM

The link you provided is for binary to bcd, but i want bcd to binary

If you know please tell me how to do that

Thanks

Ajay Bhargav
Jun 20 2011, 7:39 PM
its really simple...
lets say you have 0x29 as BCD so to convert it to binary.. just do this..

```//this is a pseudo code... kind of..
binary = (x & 0x0F) + (((x>
>
4) &0x0F) * 10);
//here x is BCD number...
```
sharath411
Jun 22 2011, 9:28 AM
Thanks Ajay,

But i want to convert 6 digit decimal to binary, what you provided is only for 2 digit..

DavesGarage
Jun 22 2011, 11:20 PM

Ajay has demonstrated the technique. Binary Coded Decimal numbers are coded such that each nibble (which means 4 bits) has a value in it that represents the ones, tens, hundreds, thousands, etc... place in a decimal number.

He has shown how to shift the second value (the "tens" place) and multiply it by 10.
He has shown how to isolate the first value ( the "units" place) and add it to the shifted value...

Can you see how you might get the thousands place? That's right: you shift it to the right 8 bits...
And what about the 10-thousands value? Right again: you shift it to the right 12 bits...
and so on, and so on...
once you have each value isolated, you multiply it by the corresponding value it represents, like in the above example.

Then you add them all together, and tah dah! You have your value...

It's really quite easy, but you must understand the concept BEFORE you attempt to code it yourself...
sharath411
Jun 23 2011, 6:52 AM
Thank You DAVE,

Could you explain me in Assembly language because am getting very much confusion there...

Sharath
majoka
Jun 23 2011, 12:28 PM
@ sharath411
logic is same as in asm as well as in c
DavesGarage
Jun 23 2011, 3:30 PM
Yes, the logic is the same. Also, if it's the syntax of assembly that you are having a problem with, you can always write it in C, and then compile it, and have a look at the assembly file that is generated by the compiler to see exactly how it's done in assembly... just a suggestion.
ExperimenterUK
Jun 23 2011, 8:00 PM
Doing this in 8051 assembly is actually quite complicated and tedious.
Are you sure you have to do it all ,what is it for ?

Basically you need to split the 3 bcd bytes into 6 single bytes containing 0-9
Create a 3 byte final total.
Multiply the 6 bytes by an increasing power of ten 1,10,100,1000,10000,100000
Add the result of each multiplication to the final total.

The heart of your routine will be a subroutine that can multiply an 8 bit value
by a 16 or 24 bit value.

Obviously you don't actually multiply by 1, and by multiplying by ten first you can
get away with 8 bit by 16 bit multiplication.
DavesGarage
Jun 24 2011, 6:08 PM
@Sharath, No - but you can use this C code to learn the method, and then convert it to assembly yourself. (note: This code will convert 8 digits, and does not contain any error checking - you should add that to your code to be sure )

I've separated out the individual steps into i1, i2, and i3 so you can see the values in your debugger...

```void BCD32BIN32( uint32_t *in, uint32_t *out )
{
uint8_t i;
uint32_t i1, i2, i3, mask = 0xF0000000;

*out = 0x00000000;

// 32 bits = 8 possible BCD values...
for( i=0; i<8; i++ )
{
i2 = (7-i)*4;
i3 = i1>
>
i2;
*out += i3;
// no need to multiply on units digit...
if( i == 7 )
break;
*out *= 10;
>
4;
}
}

void main( void )
{
uint32_t TestValue = 0x12345678;
uint32_t ReturnValue;

// TestValue has the BCD value in it...
// Return Value is the binary equivalent...
// -----------------------------------------
BCD32BIN32( &TestValue, &ReturnValue );
while(1);
}
```

Hope this helps,