Difference between revisions of "Binary coded decimal"
Line 19: | Line 19: | ||
− | Here's a recommended way to use BCD and the ASD instructions, taken from Pac-Man. | + | Here's a recommended way to use BCD and the ASD instructions, taken from Pac-Man.<br> |
Note the use of the important lnk instruction before asd: | Note the use of the important lnk instruction before asd: | ||
Line 49: | Line 49: | ||
lr A, Ku ; load next byte | lr A, Ku ; load next byte | ||
lnk ; add carry from previous operation | lnk ; add carry from previous operation | ||
− | asd S ; decimal add scratchpad | + | asd S ; decimal add scratchpad (byte 4) |
− | lr | + | lr S, A ; store result, don't need to decrease ISAR |
; done | ; done | ||
</pre> | </pre> |
Revision as of 01:48, 1 July 2018
Binary coded decimal (BCD) is a method by which 4-bit numbers are represented in digits 0-9, not 0-F. In the F8, BCD numbers can be added together using ASD or from memory using AMD (there is, however, no AID opcode, BCD Add Immediate). In a single byte, two digits can be stored, and the appropriate carry flags set if the number rolls over 99. Each digit is coded as the number + 6, so 0 should be loaded as 6, 1 as 7, 2 as 8, etc. This table shows the encoding for individual digits:
Digit | 4-bit Value ------------------- 0 | %0110 1 | %0111 2 | %1000 3 | %1001 4 | %1010 5 | %1011 6 | %1100 7 | %1101 8 | %1110 9 | %1111
It is very handy to use BCD when you need to present understandable numbers on screen like a score or similar data for the user/player.
Here's a recommended way to use BCD and the ASD instructions, taken from Pac-Man.
Note the use of the important lnk instruction before asd:
;--------------------------------------------------------------------------- ; Add score ;--------------------------------------------------------------------------- ; Adds a 4-byte decimal adjusted number to the score ; stored in r12 (Ku), r13 (Kl), r14 (Qu), r15 (Ql) ; Maximum score with four bytes is 100,000,000-1 (Stored as $FFFFFFFF) ; Score register in this example is 'O'24-'0'27 score.add: lisu 2 ; ISAR set to O27 lisl 7 lr A, Ql ; load least significant score byte asd S ; decimal add scratchpad (score_reg) lr D, A ; save result and then decrease scratchpad lr A, Qu ; load next byte into A lnk ; add carry from previous operation asd S ; decimal add scratchpad (byte 2) lr D, A ; store result lr A, Kl ; load next byte lnk ; add carry from previous operation asd S ; decimal add scratchpad (byte 3) lr D, A ; store result lr A, Ku ; load next byte lnk ; add carry from previous operation asd S ; decimal add scratchpad (byte 4) lr S, A ; store result, don't need to decrease ISAR ; done