PIC Microcontoller Math Method

Increment / Decrement

Increment / Decrement 32 bit address and detect zero

; given address of 32 bit little-endian counter in W,
; increment or decrement the counter and return Z set if zero

;Inc32z
;From Dmitry Kiryashov
inc32z:
	mov	FSR, W
	clrb	Z

	incsz	INDF
	ret

	incsz	FSR
	incsz	INDF
	ret

	incsz	FSR
	incsz	INDF
	ret

	incsz	FSR
	inc	INDF
	ret
;Dec32z
;From Rich Leggitt, Andrew Warren, and Dmitry Kiryashov
; 99.2% of the time, this takes 10 cycles w/call
dec32z:
	mov	FSR, W
	decsz	INDF
	jmp	dec32nz

	inc	FSR
	mov	W, INDF

	inc	FSR
	or	W, INDF

	inc	FSR
	or	W, INDF	;get _Z finally
	ret

dec32nz:
	clrb	Z	;set _Z=0
	movsz	W, ++INDF
	ret

	incsz	FSR	;doesn't corrupt _Z
	decsz	INDF
	movsz	W, ++INDF
	ret

	incsz	FSR	;...
	decsz	INDF
	movsz	W, ++INDF
	ret

	incsz	FSR	;...
	decsz	INDF
	ret
	ret

;for the 18cxxx instruction set.
;From Scott Dattalo
; 10-16 cycles


dec32:
	mov	FSR0L, W	; the fsr's (three of them) on the 18cxxx
	clr	FSR0H	; are 16bits wide

	dec	POSTINC0	;Indirect access that increments the fsr.

;*** WARNING: MPASM macro BNZ is not supported yet. Replace manually.
    bnz      dec32nz     ;this was bz

  ;The lsbyte is zero , so we don't need to propogate the borrow
  ;but we need to see if the higher bytes are zero.

	test	POSTINC0	;The first byte is zero
	or	POSTINC0, W	;check the next three
	or	POSTINC0, W

	ret

dec32nz
;	dec	on the 18cxxx parts affects C

	snb	C
	ret

	dec	POSINC0
	sb	C
	dec	POSTINC0

	sb	C
	dec	POSTINC0

	clrb	Z	;was setz
	ret