Jump to content

Binary coded decimal: Difference between revisions

no edit summary
(Created page with "{{draft task}} Binary-Coded Decimal (or BCD for short) is a method of representing decimal numbers by storing what appears to be a decimal number...")
 
No edit summary
Line 13:
 
<br><br>
=={{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.
<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>
{{out}}
<pre>20
29
0100</pre>
=={{header|Z80 Assembly}}==
The <code>DAA</code> 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.
1,489

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.