Skip to navigation

Maths (Arithmetic): GetPlayerEnergyBCD

Name: GetPlayerEnergyBCD [Show more] Type: Subroutine Category: Maths (Arithmetic) Summary: Fetch the player's energy in binary coded decimal (BCD)
Context: See this subroutine in context in the source code References: This subroutine is called as follows: * FinishLandscape calls GetPlayerEnergyBCD

Returns: A The player's energy in BCD
.GetPlayerEnergyBCD LDA #0 \ Set A = 0 to use as the player's energy in binary \ coded decimal (BCD) LDX playerEnergy \ Set X to the player's energy level BEQ plen2 \ If X = 0 then jump to plen2 to return A = 0 as the \ player's energy in BCD \ Otherwise we convert X into BCD by simply adding 1 to \ A and repeating this X times \ \ This works because we only call this routine when the \ D flag is set to switch arithmetic to BCD .plen1 CLC \ Increment the BCD number we are building in A ADC #1 DEX \ Decrement the player's energy level in S BNE plen1 \ Loop back until we have added 1 to A, X times, so A \ now contains the original value of X but in BCD .plen2 RTS \ Return from the subroutine