Binary coded decimal: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 13:
 
<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}}==
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.
1,489

edits