SX Microcontroller Math Method

Square 10 bits

;by John Payson

clrf DstH
	clr	DstM
	clr	DstL
	mov	W, SrcL
	and	W, #$0F
	sb	Src.4
	add	DstM, W
	rr	DstM
	rr	DstL
	sb	Src.5
	add	DstM, W
	rr	DstM
	rr	DstL
	sb	Src.6
	add	DstM, W
	rr	DstM
	rr	DstL
	sb	Src.7
	add	DstM, W
	call	Sqr4
	add	DstL, W
	swap	SrcL
	and	W, #$0F
	call	Sqr4
	add	DstM, W
; At this point, 16-bit result is in DstM:DstH
; 25 words of code prior to this point (plus a
; 17-word table-lookup). Total execution time:
; 35 cycles up to this point.
	sb	SrcH.0
	jmp	NoBit8
	mov	W, SrcL
	snb	C
	inc	DstH
	add	DstM, W
	snb	C
	inc	DstH
	inc	DstH
; Another 9 words for bit 8; 3 or 9 cycles to exec.
NoBit8:
	sb	SrcH.1
	jmp	NoBit9
	mov	W, #4
	sb	SrcH.0
	mov	W, #8
	add	DstH, W
	mov	W, <<SrcL
	snb	C
	inc	DstH
	snb	C
	inc	DstH
	add	DstM, W
	snb	C
	inc	DstH
	add	DstM, W
	snb	C
	inc	DstH
; Another 17 words for bit 9; 3 or 17 cycles to execute
; Total worst-case time: 35+26 = 61 cycles.
NoBit9:
	retw	#0	; All done!
Sqr4:
	add	PC, W
db 0,1,4,9,16,25,36,49,64,81,100,121,144,169,196,225

See also: