1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

Newbie Tech: Hex to BCD conversion

Discussion in 'Embedded' started by Jim Knight, Jan 31, 2004.

  1. Jim Knight

    Jim Knight Guest

    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, Jan 31, 2004
    #1
    1. Advertising

  2. Jim Knight

    CBFalconer Guest

    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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Jan 31, 2004
    #2
    1. Advertising

  3. Jim Knight

    Gary Kato Guest

    >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.
    Gary Kato, Jan 31, 2004
    #3
  4. Jim Knight

    CBarn24050 Guest

    Hi, the simplest way is to use hex for all your operations. Convert to bcd only
    for the led output.
    CBarn24050, Jan 31, 2004
    #4
  5. Jim Knight

    Jim Knight Guest

    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" <> wrote in message
    news:...
    > >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.
    >
    >
    >
    Jim Knight, Jan 31, 2004
    #5
  6. Jim Knight

    bogax Guest

    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" <> wrote in message news:<>...
    > 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" <> wrote in message
    > news:...
    > > >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.
    > >
    > >
    > >
    bogax, Feb 1, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rolf R Bakke
    Replies:
    1
    Views:
    1,261
    Ulf Samuelsson
    Sep 16, 2003
  2. Rolf R Bakke
    Replies:
    0
    Views:
    588
    Rolf R Bakke
    Sep 15, 2003
  3. philblack

    fast binary to BCD source code

    philblack, Jun 30, 2004, in forum: Embedded
    Replies:
    4
    Views:
    1,667
    Keyser Soze
    Jul 6, 2004
  4. Replies:
    0
    Views:
    2,774
  5. mikeyhsd

    BCD limited to 2 entries

    mikeyhsd, Nov 21, 2007, in forum: Gigabyte
    Replies:
    0
    Views:
    641
    mikeyhsd
    Nov 21, 2007
Loading...

Share This Page