## BCD Assembly

22 posts / 0 new
Author
Message

Could i have assembly codes about  1) BCD to binary
(Atmega 32A AVR studio 4.19)        2) binary to BCD
3) addition and subtraction of numbers in BCD code

Last Edited: Mon. Jan 22, 2018 - 04:14 PM

Yes.

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...

1) you do realise that's just "split the nibbles" and multiply the high one by 10 don't you?

2) that one is a bit more complex as it involves a divide by 10 but I'm pretty sure Atmel have an app note for that (maybe the entire bin->BCD thing)

3) Again that's just separate the nibbles and do it just like you did in school:

```  37
+ 54
==```

In that you add the 7 and the 4 in the right columns and if there is a carry (there is) you add that in along with the 3 and 5 in the right most columns. Again it's just a question of splitting 37 into 3 and 7 and 54 into 5 and 4.

Presumably this is a school assignment? Didn't your course tutor already explain all about separating the digits? At the very least (though there may be more efficient ways) the lower (right) nibble can be extracted with "AND 0x0F" and the upper nibble with 4 logical shifts to the right.

clawson wrote:
I'm pretty sure Atmel have an app note for that

Yes, AVR204 with code example in assembly!

Jim

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

When sample codes are discussed, will we finally see a useful use for the H bit in SREG?

x86 had/has the seldom-used AAM opcode, right?

Is this one of those "representation" threads, where OP has e.g. an AVR register and wants to see the value in different ways?  Or really a BCD number, as an RTC chip might return?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

If sum is 9 or less, for example:

```  0x05
+ 0x02
--------
0x07```

Result is already correct BCD, you don't need to do anything;

If result is > 9, for example:

```  0x08
+ 0x09
--------
0x11```

You need to add 6 to convert to BCD, and a carry results to add to the next nibble.

Useful instructions that you'll need: swap, andi.

edit: about the H flag, it might be useful for more optimized code, perhaps. But the OP will probably want to keep it simple, after all you should not present code you don't really understand.

Last Edited: Mon. Jan 22, 2018 - 07:20 PM

theusch wrote:
x86 had/has the seldom-used AAM opcode, right?
Which stems from 8080 and Z80 which had DAA (Decimal Adjust Accumulator). I guess this is the kind of thing that differentiates CISC from RISC?

The 6502 could do all arithmetic in BCD. Very seldom used. It worked with the same number of cycles as binary arithmetic.

david.prentice wrote:
The 6502 could do all arithmetic in BCD. Very seldom used. It worked with the same number of cycles as binary arithmetic.

The Burroughs "Medium Systems" mainframes were [almost?] completely decimal machines.  In fact IIRC it was only in the x7xx and later that there were binary accumulators.  PIC 99V99 COMP. anyone?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

From distant memory, BCD was useful for some calculations. But you certainly did not want to use it in address lookup. You would need the binary value in X or Y registers for doing indexed addressing.

Don't (or didn't) most calculators (used to) work internally in BCD ... ?

Top Tips:

1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...

Indeed - I'm pretty sure that's why your 6502's and Z80s etc. principally had these DAA type opcodes to ease the implementation of BCD arithmetic.

A 6502 (at least the original one) don't have BCD instructions, it can have carry set at 100 or 256 (0) and that is the only one I remember.

Yes it did. The arithmetic was done in BCD.

The 6502 had the instructions CLD - Clear Decimal Mode and SED - Set Decimal Mode which only cleared or set the D flag in the Status (P register) and affected no other flags or registers other than an increment of the Program Counter (PC) .

The D flag determined the mode, binary or decimal,  of the instructions ADC - Add Memory with Carry to Accumulator and SBC - Subtract Memory from Accumulator with Borrow.

Loved using the 6502, it was my first learned microprocessor.

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

I stay corrected I only remembered that the carry came at 100.

And the 6502 computers I have had (PET, oric and c64) it was a no go with the decimal flag set.

From memory,  the PET would CLD when entering an ISR().   And the RTI would restore P afterwards.

In other words there was no problem with BCD mode.   But as I said earlier,   few people used it.

The processor technology BASIC interpreter, that I have been playing about with has an extended floating point package that is BCD based.  For 40 years I have wanted to know how this works.  The code is pure 8080 as Z80 opcodes are verboten in the emulator.

IEE-754 includes a BCD format.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

I believe that the only place that BCD is seen today is in the time-keeping registers of the RTC chips: DS1307 and DS3231.   That, and homework assignments.