It uses 3 zeropage variables: Number is the input $0000-$00FF only. Sqr is the output. Tempsq is temp storage.
This is the Square Routine:
Code: Select all
; How to calculate the 16-bit unsigned integer square of a signed 16-bit integer.
; By Lee Davison (leeedavison@googlemail.com), 19 October 2000.
; Modified for the 65Org16 By Sam Gaskill, 13 March 2013.
; Calculates the 16 bit unsigned integer square of the signed 16 bit integer in
; Number. The result is always in the range 0 to 65025 and is held in
; Sqr
;
; The maximum input range is only +/-255 and no checking is done to ensure that
; this is so.
;
; This routine is useful if you are trying to draw circles as for any circle
;
; x^2+y^2=r^2 where x and y are the co-ordinates of any point on the circle and
; r is the circle radius
;
; Destroys all registers
Square LDA #$00 ; clear A
STA Sqr ; clear square
LDA Number
NoNneg STA Tempsq ; save ABS(number)
LDX #$10 ; set bit count
Nextr2bit ASL Sqr ; low byte *2
ASL A ; shift number byte
BCC NoSqadd ; don't do add if C = 0
TAY ; save A
CLC ; clear carry for add
LDA Tempsq ; get number
ADC Sqr ; add number^2
STA Sqr ; save number^2
TYA ; get A back
NoSqadd DEX ; decrement bit count
BNE Nextr2bit ; go do next bit
RTS