Snippet:Score

From veswiki
Revision as of 17:52, 13 June 2022 by E5frog (talk | contribs)
Jump to: navigation, search

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, which basically means you add 6 to the decimal number and use the interval 0x06-0x0F to represent 0-9.

In the example below the 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.add:
	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