EmbeddedRelated.com
Forums

Newbie Tech: Hex to BCD conversion

Started by Jim Knight January 30, 2004
Having an old 8 bit ASM converted to newer (ST7) 8-bit ASM.  ST does not
have a BCD mode (which the older processor did), so I need to convert
arithmetic (mostly adds with carry) from hex to BCD, and back.

The BCD uses a 4 bit nibble that does not exceed 10 decimal.  In this
application, the BCD is primarily used for 7 segment LED displays (to keep
from trying to have an A-F put out to the LED drivers).

Question:

Is it difficult to explain how you would do this conversion in a system
(ST7) that only works in Hex?

Now that I have re-read this note, I realize acronyms were made to keep
people from communicating with each other ;-)

Jim Knight
remove NOSPAM from direct email responses



Jim Knight wrote:
> > Having an old 8 bit ASM converted to newer (ST7) 8-bit ASM. ST > does not have a BCD mode (which the older processor did), so I > need to convert arithmetic (mostly adds with carry) from hex to > BCD, and back. > > The BCD uses a 4 bit nibble that does not exceed 10 decimal. In > this application, the BCD is primarily used for 7 segment LED > displays (to keep from trying to have an A-F put out to the LED > drivers). > > Question: > > Is it difficult to explain how you would do this conversion in > a system (ST7) that only works in Hex?
This sounds as if you don't care what base values are displayed in. Conversion to BCD is isomorphic to conversion to an Ascii string. However you might consider Octal displays, which simply have to take 3 bits at a time, and use only digits 0 through 7. -- Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net) Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> USE worldnet address!
>Is it difficult to explain how you would do this conversion in a system >(ST7) that only works in Hex? >
No, not difficult. All you really need to do is to look at what happens to one nibble. This assumes you added two legal BCD values together into a sum. First, you need to detect when the nibble goes beyond 9. So, what do you do? Try an example: 1001 9 0001 1 1010 10(hex) Illegal BCD What we want is 10hex. To get this, we need to subtract 10(hex) from this nibble and add 1 to the next higher nibble. If we are looking at the least significant nibble of a byte, we see that this means adding 16 to increment the high nibble. So, +16-10 = +6. We come out with this general rule: 1. If the low 4 bits of the sum are > 9, add 6 to sum. But we have to take another case into account. What if the low nibble of the sum is legal BCD, but there was a carry from the low nibble to the high during our binary addition? Example: 01001 9 01001 9 10010 12(hex) = 18 decimal What we want is 18(hex). We need to add 6 to the sum to make it right. Which leads to our next rule: 2. If there was a carry from low nibble to high, add 6 to the sum. Fortunately, the ST7 has a carry bit to show a carry from the high nibble, and a half-carry bit to show a carry from the low nibble to the high nibble. I'll leave BCD subtraction to you, though you probably don't need that.
Hi, the simplest way is to use hex for all your operations. Convert to bcd only
for the led output.
Gary,
  Thanks.  Appreciate the clear instructions even someone like me can
understand.  Guess I should be able to write a routine that does this and
then just call the routine whenever the original code is operating in
Decimal mode.

  Again thanks.

Jim Knight
"Gary Kato" <garykato@aol.com> wrote in message
news:20040130234027.27149.00001096@mb-m11.aol.com...
> >Is it difficult to explain how you would do this conversion in a system > >(ST7) that only works in Hex? > > > > No, not difficult. All you really need to do is to look at what happens to
one
> nibble. This assumes you added two legal BCD values together into a sum. > > First, you need to detect when the nibble goes beyond 9. So, what do you
do?
> Try an example: > > 1001 9 > 0001 1 > 1010 10(hex) Illegal BCD > > What we want is 10hex. To get this, we need to subtract 10(hex) from this > nibble and add 1 to the next higher nibble. If we are looking at the least > significant nibble of a byte, we see that this means adding 16 to
increment the
> high nibble. So, +16-10 = +6. We come out with this general rule: > > 1. If the low 4 bits of the sum are > 9, add 6 to sum. > > But we have to take another case into account. What if the low nibble of
the
> sum is legal BCD, but there was a carry from the low nibble to the high
during
> our binary addition? Example: > > 01001 9 > 01001 9 > 10010 12(hex) = 18 decimal > > What we want is 18(hex). We need to add 6 to the sum to make it right.
Which
> leads to our next rule: > > 2. If there was a carry from low nibble to high, add 6 to the sum. > > Fortunately, the ST7 has a carry bit to show a carry from the high nibble,
and
> a half-carry bit to show a carry from the low nibble to the high nibble. > > I'll leave BCD subtraction to you, though you probably don't need that. > > >
Sorry if this is obvious

Don't know if this applies, but notice that if you're doing multiple
digit hex numbers, it's not enough to just convert the hex nibbles to
their BCD equvalents if what you want is to end up with actual BCD

Suppose you've got a two digit hex number.  You've got a 1's place
and a 16's place. If you simply convert that 16's place hex digit
to BCD you end up with (sort of) BCD coded hex.  You'll need to multiply
it by 16 to get the BCD.

     C0 in HEX which is 192 in decimal

if you only convert the high nibble to it's BCD equvalent you get

    120  in BCD (or "BCD coded hex")

You have to multiply that 12 by 16
    


"Jim Knight" <rottendog@NOSPAM.peoplepc.com> wrote in message news:<56adnSQxN_1ZKIbdRVn-jQ@comcast.com>...
> Gary, > Thanks. Appreciate the clear instructions even someone like me can > understand. Guess I should be able to write a routine that does this and > then just call the routine whenever the original code is operating in > Decimal mode. > > Again thanks. > > Jim Knight > "Gary Kato" <garykato@aol.com> wrote in message > news:20040130234027.27149.00001096@mb-m11.aol.com... > > >Is it difficult to explain how you would do this conversion in a system > > >(ST7) that only works in Hex? > > > > > > > No, not difficult. All you really need to do is to look at what happens to > one > > nibble. This assumes you added two legal BCD values together into a sum. > > > > First, you need to detect when the nibble goes beyond 9. So, what do you > do? > > Try an example: > > > > 1001 9 > > 0001 1 > > 1010 10(hex) Illegal BCD > > > > What we want is 10hex. To get this, we need to subtract 10(hex) from this > > nibble and add 1 to the next higher nibble. If we are looking at the least > > significant nibble of a byte, we see that this means adding 16 to > increment the > > high nibble. So, +16-10 = +6. We come out with this general rule: > > > > 1. If the low 4 bits of the sum are > 9, add 6 to sum. > > > > But we have to take another case into account. What if the low nibble of > the > > sum is legal BCD, but there was a carry from the low nibble to the high > during > > our binary addition? Example: > > > > 01001 9 > > 01001 9 > > 10010 12(hex) = 18 decimal > > > > What we want is 18(hex). We need to add 6 to the sum to make it right. > Which > > leads to our next rule: > > > > 2. If there was a carry from low nibble to high, add 6 to the sum. > > > > Fortunately, the ST7 has a carry bit to show a carry from the high nibble, > and > > a half-carry bit to show a carry from the low nibble to the high nibble. > > > > I'll leave BCD subtraction to you, though you probably don't need that. > > > > > >