Prev: 26524 Up: Map Next: 26641
26546: Add To Score
Converts a binary score value in A into three decimal digits and adds them to the current score. The digits are stored in the score buffer at CurrentScore. The updated score is then printed to the screen.
Input
A Score value to add
Convert the binary value in A into three decimal digits (hundreds, tens, units) stored at 26667-26673.
Extract the hundreds digit.
AddToScore 26546 LD HL,26673 Point HL at 26673 (the end of ScoreBuffer).
26549 LD E,0 Initialise E to 0 to count "hundreds".
AddToScore_Hundreds_Loop 26551 CP 100 Jump to AddToScore_Tens if the score to add is less than a hundred.
26553 JR C,AddToScore_Tens
26555 SUB 100 Subtract one hundred from the score to add.
26557 INC E Increment the hundreds digit.
26558 JR AddToScore_Hundreds_Loop Jump back to AddToScore_Hundreds_Loop.
Store the hundreds digit and extract the tens digit.
AddToScore_Tens 26560 LD (HL),E Write the hundreds digit to the hundreds digit of the score buffer.
26561 LD E,0 Reset E back to 0 to now count "tens".
26563 DEC HL Move to the tens digit position.
AddToScore_Tens_Loop 26564 CP 10 Jump to AddToScore_Units if the score to add is less than ten.
26566 JR C,AddToScore_Units
26568 SUB 10 Subtract ten from the score to add.
26570 INC E Increment the tens digit.
26571 JR AddToScore_Tens_Loop Jump back to AddToScore_Tens_Loop.
Store the tens digit and extract the units digit.
AddToScore_Units 26573 LD (HL),E Write the tens digit to the tens digit of the score buffer.
26574 LD E,0 Reset E back to 0 to now count "units".
26576 DEC HL Move to the units digit position.
AddToScore_Units_Loop 26577 OR A Jump to AddToScore_StoreUnits if the score to add is now zero.
26578 JR Z,AddToScore_StoreUnits
26580 INC E Increment the units digit.
26581 DEC A Decrement the score to add by one.
26582 JR AddToScore_Units_Loop Jump back to AddToScore_Units_Loop.
Store the units digit.
AddToScore_StoreUnits 26584 LD (HL),E Write the units digit to *HL.
Add the three expanded digits to the current score stored at CurrentScore.
26585 LD DE,26661 DE=CurrentScore.
26588 LD B,3 Set a counter in B for 3 digits to add.
AddToScore_AddLoop 26590 LD A,(DE) Fetch the current score digit.
26591 ADD A,(HL) Add the converted digit from *HL.
26592 CP 10 Is the result 10 or greater/ is there any decimal carry?
26594 PUSH DE Stash the score and conversion pointers on the stack.
26595 PUSH HL
26596 JR NC,AddToScore_Carry Jump to AddToScore_Carry if a decimal carry is needed.
There is no carry so store the result and move to the next digit.
26598 POP HL Restore the score and conversion pointers from the stack.
26599 POP DE
26600 LD (DE),A Write the updated digit back to the current score.
AddToScore_NextDigit 26601 INC DE Advance both pointers to the next digit.
26602 INC HL
26603 DJNZ AddToScore_AddLoop Decrease the counter by one and loop back to AddToScore_AddLoop until all 3 digits are added.
26605 JR AddToScore_Print Jump to AddToScore_Print.
Handle decimal carry so subtract 10 from the current digit and propagate the carry to higher digits.
AddToScore_Carry 26607 EX DE,HL Exchange DE and HL so HL points to the score digit.
26608 SUB 10 Subtract 10 from the digit.
26610 LD (HL),A Store the wrapped digit.
AddToScore_CarryPropagate 26611 INC HL Move to the next higher score digit.
26612 INC (HL) Increment it by one.
26613 LD A,(HL) A=the incremented digit.
26614 CP 10 Has this digit also reached 10?
26616 JR NZ,AddToScore_CarryDone Jump to AddToScore_CarryDone if no further carry is needed.
This digit also overflowed so wrap it to 0 and continue propagating.
26618 LD (HL),0 Write 0 to *HL.
26620 JR AddToScore_CarryPropagate Jump to AddToScore_CarryPropagate to propagate carry to the next digit.
AddToScore_CarryDone 26622 POP HL Restore the conversion and score pointers from the stack.
26623 POP DE
26624 JR AddToScore_NextDigit Jump to AddToScore_NextDigit to continue with the next digit.
Print the updated score to the screen.
AddToScore_Print 26626 LD HL,20733 HL=20733 (screen buffer location).
26629 PUSH IX Stash IX on the stack temporarily.
26631 LD IX,26661 IX=CurrentScore.
26635 CALL PrintScore Call PrintScore.
26638 POP IX Restore IX from the stack.
26640 RET Return.
Prev: 26524 Up: Map Next: 26641