Binary coded decimal: Difference between revisions
Puppydrum64 (talk | contribs) No edit summary |
Puppydrum64 (talk | contribs) No edit summary |
||
Line 13: | Line 13: | ||
<br><br> |
<br><br> |
||
=={{header|6502 Assembly}}== |
|||
'''Doesn't work with:''' [[wp:Ricoh_2A03|Ricoh 2A03]] |
|||
The 6502 is a bit different in that it has a special operating mode where all addition and subtraction is handled as binary-coded decimal. Like the 68000, this must be invoked ahead of time, rather than using the Intel method of doing the math normally and then correcting it after the fact. (This special operating mode won't work on the aforementioned Ricoh 2A03, which performs math in "normal" mode even if the decimal flag is set.) |
|||
<lang 6502asm>sed ;set decimal flag; now all math is BCD |
|||
lda #$19 |
|||
clc |
|||
adc #1 |
|||
cld ;chances are, PrintHex won't work properly when in decimal mode. |
|||
JSR PrintHex ;unimplemented print routine |
|||
JSR NewLine |
|||
sed |
|||
lda #$30 |
|||
sec |
|||
sbc #1 |
|||
cld |
|||
jsr PrintHex |
|||
JSR NewLine |
|||
sed |
|||
lda #$99 |
|||
clc |
|||
adc #1 |
|||
pha |
|||
lda #0 |
|||
adc #0 ;adds the carry |
|||
cld |
|||
jsr PrintHex |
|||
pla |
|||
jsr PrintHex |
|||
jsr NewLine |
|||
rts ;return to basic</lang> |
|||
{{out}} |
|||
<pre>20 |
|||
29 |
|||
0100</pre> |
|||
=={{header|68000 Assembly}}== |
=={{header|68000 Assembly}}== |
||
The 68000 has special mathematics commands for binary-coded decimal. However, they only work at byte length, and cannot use immediate operands. Even adding by 1 this way requires you to load 1 into a register first. |
The 68000 has special mathematics commands for binary-coded decimal. However, they only work at byte length, and cannot use immediate operands. Even adding by 1 this way requires you to load 1 into a register first. |
Revision as of 01:47, 12 July 2022
Binary-Coded Decimal (or BCD for short) is a method of representing decimal numbers by storing what appears to be a decimal number but is actually stored as hexadecimal. Many CISC CPUs (e.g. X86 Assembly have special hardware routines for displaying these kinds of numbers.) On low-level hardware, such as 7-segment displays, binary-coded decimal is very important for outputting data in a format the end user can understand.
- Task
Use your language's built-in BCD functions, OR create your own conversion function, that converts an addition of hexadecimal numbers to binary-coded decimal. You should get the following results with these test cases:
- 0x19 + 1 = 0x20
- 0x30 - 1 = 0x29
- 0x99 + 1 = 0x100
- Bonus Points
Demonstrate the above test cases in both "packed BCD" (two digits per byte) and "unpacked BCD" (one digit per byte).
6502 Assembly
Doesn't work with: Ricoh 2A03
The 6502 is a bit different in that it has a special operating mode where all addition and subtraction is handled as binary-coded decimal. Like the 68000, this must be invoked ahead of time, rather than using the Intel method of doing the math normally and then correcting it after the fact. (This special operating mode won't work on the aforementioned Ricoh 2A03, which performs math in "normal" mode even if the decimal flag is set.)
<lang 6502asm>sed ;set decimal flag; now all math is BCD lda #$19 clc adc #1 cld ;chances are, PrintHex won't work properly when in decimal mode. JSR PrintHex ;unimplemented print routine JSR NewLine
sed lda #$30 sec sbc #1 cld jsr PrintHex JSR NewLine
sed lda #$99 clc adc #1 pha lda #0 adc #0 ;adds the carry cld jsr PrintHex pla jsr PrintHex jsr NewLine rts ;return to basic</lang>
- Output:
20 29 0100
68000 Assembly
The 68000 has special mathematics commands for binary-coded decimal. However, they only work at byte length, and cannot use immediate operands. Even adding by 1 this way requires you to load 1 into a register first. <lang 68000devpac> MOVEQ #$19,D0 MOVEQ #1,D1 MOVEQ #0,D2
ABCD D1,D0 JSR PrintHex JSR NewLine
MOVEQ #$30,D0 SBCD D1,D0 JSR PrintHex JSR NewLine
MOVE.B #$99,D0 ABCD D1,D0 ;D0 has rolled over to 00 and set both the extend and carry flags. ADDX D2,D2 ;add the extend flag which was set by the above operation ;this can't use immediate operands either so we're using D2 which we set to zero at the start.
MOVE.L D0,D3 ;back up the output since PrintHex takes D0 as its argument. MOVE.L D2,D0 ;print the 01 JSR PrintHex MOVE.L D3,D0 ;then the 00 JSR PrintHex
jmp *</lang>
- Output:
20 29 0100
Z80 Assembly
The DAA
function will convert an 8-bit hexadecimal value to BCD after an addition or subtraction is performed. The algorithm used is actually quite complex, but the Z80's dedicated hardware for it makes it all happen in 4 clock cycles, tied with the fastest instructions the CPU can perform.
<lang z80> PrintChar equ &BB5A ;Amstrad CPC kernel's print routine org &1000
ld a,&19 add 1 daa call ShowHex call NewLine
ld a,&30 sub 1 daa call ShowHex call NewLine
ld a,&99 add 1 daa
- this rolls over to 00 since DAA only works with the accumulator.
- But the carry is set by this operation, so we can work accordingly.
jr nc,continue ;this branch is never taken, it exists to demonstrate the concept of how DAA affects the carry flag. push af ld a,1 call ShowHex pop af continue: call ShowHex call NewLine ret ;return to basic
ShowHex: push af and %11110000 rrca rrca rrca rrca call PrintHexChar pop af and %00001111 ;call PrintHexChar ;execution flows into it naturally. PrintHexChar: ;this little trick converts hexadecimal or BCD to ASCII. or a ;Clear Carry Flag daa add a,&F0 adc a,&40 jp PrintChar</lang>
- Output:
20 29 0100