# Snippet:Score

When coding a game you usually want to add some kind of scoring. This is an example of an eight digit scoring method using decimal add. Decimal add is done using Binary coded decimal (BCD). If adding a hexadecimal number you first need to add $66 before using the ASD op-code.

In the example below the BCD score is held in scratchpad registers from 37 down to 34 going backwards and adding up from least significant to most significant. Score is added using the 16-bit Q and K registers, which are 32 bits in total and we store two decimal digits per byte, which means - using decimal mode, we get a score register that can hold 99'999'999 points.

Score: lisu 3 ; Set scratchpad register lisl 7 lr A, QL ; Copy least significant digit (holds 1's and 10's) to A from QL asd S ; Decimal add scratchpad value from the set 'O'37 lr D, A ; Copy result to scratchpad and then decrease it to 'O'36 ; Two digits done lr A, QU ; Copy next score byte (100's and 1000's)from QU into A lnk ; LNK adds carry bit from previous ASD operation asd S ; Decimal add to scratchpad 'O'36 lr D, A ; Copy result to scratchpad and then decrease it to 'O'35 ; Four digits done lr A, KL ; Copy next byte from KL (10'000 and 100'000) lnk ; Add carry from previous operation asd S ; Decimal add scratchpad 'O'35 lr D, A ; Copy result to scratchpad and then decrease it to 'O'34 ; Six digts done lr A, KU ; Load next byte (1'000'000 and 10'000'000) lnk ; Add carry from previous operation asd S ; Decimal add scratchpad lr D, A ; Store result ; 8 digits ALL done pop

As mentioned, if you want to add a hexadecimal number to a BCD, first make it BCD encoded by adding $66:

... lr A, 2 ; Fetch hexadecimal number from register 2 to be added ai $66 ; Add $66 to make it BCD compatible asd S ; Use the decimal add op-code, adding register S lr S, A ; Store BCD sum in register S ...

The sum stored in "register S" is then visible as a plain decimal number (in a memory debug window). "10" means decimal 10, "29" means decimal 29. It's very handy if then plotting the digits as a decimal number as they can easily be masked with SL and SR without the need for a conversion.