ArithmeticMean: PHA TYA PHA ;push accumulator and Y register onto stack LDA #0 STA Temp STA Temp+1 ;temporary 16-bit storage for total LDY NumberInts BEQ Done ;if NumberInts = 0 then return an average of zero DEY ;start with NumberInts-1 AddLoop: LDA (ArrayPtr),Y CLC ADC Temp STA Temp LDA Temp+1 ADC #0 STA Temp+1 DEY CPY #255 BNE AddLoop LDY #-1 DivideLoop: LDA Temp SEC SBC NumberInts STA Temp LDA Temp+1 SBC #0 STA Temp+1 INY BCS DivideLoop Done: STY ArithMean ;store result here PLA ;restore accumulator and Y register from stack TAY PLA RTS ;return from routine