Code Examples

Index to Bitmask
	radix	dec
	include	"p16cxxx.inc"

	;      Input	  Output
	;
	;	 0	0b00000001
	;	 1	0b00000010
	;	 2	0b00000100
	;	 3	0b00001000
	;	 4	0b00010000
	;	 5	0b00100000
	;	 6	0b01000000
	;	 7	0b10000000

	global	index_to_bitmask

; W        -- [Input]  Index of bit.

; reg 0x70 -- [Temp]   Copy of input parameter. (Common RAM Area)
bit_index	equ	0x70

; reg 0x71 -- [Temp]   Temporary register. (Common RAM Area)
temp		equ	0x71

; W        -- [Output]

code_index_to_bitmask	code
index_to_bitmask:
	movwf	bit_index
	movlw	0x01
	btfsc	bit_index, 2		; Upper or lower half of the byte.
	movlw	0x10
	movwf	temp
	btfsc	bit_index, 0		; Even or odd bit.
	addwf	temp, f
	movf	temp, w
	btfss	bit_index, 1		; Upper or lower half of the half-byte.
	return
	addwf	temp, f
	rlf	temp, w
	return

	end
	radix	dec
	include	"p18cxxx.inc"

	;      Input	  Output
	;
	;	 0	0b00000001
	;	 1	0b00000010
	;	 2	0b00000100
	;	 3	0b00001000
	;	 4	0b00010000
	;	 5	0b00100000
	;	 6	0b01000000
	;	 7	0b10000000

	global	index_to_bitmask

; WREG  -- [Input]  Index of bit. (SFR Area)
; PRODL -- [Temp]   Temporary register. (SFR Area)

; WREG  -- [Output]

code_index_to_bitmask	code
index_to_bitmask:
	movwf	PRODL, a
	movlw	0x01
	btfsc	PRODL, 2, a		; Upper or lower half of the byte.
	movlw	0x10
	btfsc	PRODL, 0, a		; Even or odd bit.
	addwf	WREG, f, a
	btfss	PRODL, 1, a		; Upper or lower half of the half-byte.
	return
	addwf	WREG, f, a
	addwf	WREG, f, a
	return

	end
Index to Reverse Bitmask
	radix	dec
	include	"p16cxxx.inc"

	;      Input	  Output
	;
	;	 0	0b10000000
	;	 1	0b01000000
	;	 2	0b00100000
	;	 3	0b00010000
	;	 4	0b00001000
	;	 5	0b00000100
	;	 6	0b00000010
	;	 7	0b00000001

	global	index_to_reverse_bitmask

; W        -- [Input]  Index of bit.

; reg 0x70 -- [Temp]   Copy of input parameter. (Common RAM Area)
bit_index	equ	0x70

; reg 0x71 -- [Temp]   Temporary register. (Common RAM Area)
temp		equ	0x71

; W        -- [Output]

code_index_to_reverse_bitmask	code
index_to_reverse_bitmask:
	movwf	bit_index
	movlw	0x80
	movwf	temp
	btfsc	bit_index, 2		; Upper or lower half of the byte.
	swapf	temp, f
	bcf	STATUS, 0
	btfsc	bit_index, 0		; Even or odd bit.
	rrf	temp, f
	movf	temp, w
	btfss	bit_index, 1		; Upper or lower half of the half-byte.
	return
	rrf	temp, f
	rrf	temp, w
	andlw	0x3F
	return

	end
	radix	dec
	include	"p18cxxx.inc"

	;      Input	  Output
	;
	;	 0	0b10000000
	;	 1	0b01000000
	;	 2	0b00100000
	;	 3	0b00010000
	;	 4	0b00001000
	;	 5	0b00000100
	;	 6	0b00000010
	;	 7	0b00000001

	global	index_to_reverse_bitmask

; WREG  -- [Input]  Index of bit. (SFR Area)
; PRODL -- [Temp]   Temporary register. (SFR Area)

; WREG  -- [Output]

code_index_to_reverse_bitmask	code
index_to_reverse_bitmask:
	movwf	PRODL, a
	movlw	0x01
	btfss	PRODL, 2, a		; Upper or lower half of the byte.
	movlw	0x10
	btfss	PRODL, 0, a		; Even or odd bit.
	addwf	WREG, f, a
	btfsc	PRODL, 1, a		; Upper or lower half of the half-byte.
	return
	addwf	WREG, f, a
	addwf	WREG, f, a
	return

	end
Divide by 10 (only PIC18)
	radix	dec
	include	"p18cxxx.inc"

	; return (((Number * 0xCD) >> 8) >> 3);

	global	byte_divide_10

; WREG  -- [Input]    Dividend number. (SFR Area)
; PRODH -- [Internal] Upper byte of product arithmetical. (SFR Area)

; WREG  -- [Output]   result

code_byte_divide_10	code
byte_divide_10:
	mullw	0xCD
	swapf	PRODH, w, a
	rlncf	WREG, f, a
	andlw	0x1F
	return

	end
Binary -> Hex converter (only PIC18)
	radix	dec
	include	"p18cxxx.inc"

	;	Input	Output
	;
	;	 0-9	'0' .. '9'
	;	 a-f	'A' .. 'F'

	global	bin_to_hex

; WREG  -- [Input]  A number. (SFR Area)

; WREG  -- [Output] Character

code_bin_to_hex code
bin_to_hex:
	andlw	0x0F			; Only the lower half-byte are needed.
	addlw	0x90
	daw
	btfsc	STATUS, C, a
	addlw	0x01
	addlw	0x40
	daw
	return

	end
isdigit
	radix	dec
	include	"p16cxxx.inc"

	;	Input	  Output
	;
	;	'0'-'9'	  1
	;	other	  0

	global	isdigit

; W  -- [Input]  A character.

; W  -- [Output] Logical result.

code_isdigit code
isdigit:
	addlw	(0x100 - '0')			; 0x100 - '0' = 0xD0
	  ; 0x00-0x2F -- 0xD0-0xFF
	  ; '0' - '9' -- 0x00-0x09
	  ; 0x3A-0xFF -- 0x0A-0xCF
	addlw	(0xFF - ('9' - '0'))		; 0xFF - ('9' - '0') = 0xF6
	  ; 0x00-0x2F -- 0xC6-0xF5 -- C = 1
	  ; '0' - '9' -- 0xF6-0xFF -- C = 0
	  ; 0x3A-0xFF -- 0x00-0xC5 -- C = 1
	btfss	STATUS, C
	retlw	1
	retlw	0

	end
	radix	dec
	include	"p18cxxx.inc"

	;	Input	  Output
	;
	;	'0'-'9'	  1
	;	other	  0

	global	isdigit

; WREG  -- [Input]  A character. (SFR Area)

; WREG  -- [Output] Logical result.

code_isdigit code
isdigit:
	addlw	(0x100 - '0')			; 0x100 - '0' = 0xD0
	  ; 0x00-0x2F -- 0xD0-0xFF
	  ; '0' - '9' -- 0x00-0x09
	  ; 0x3A-0xFF -- 0x0A-0xCF
	addlw	(0xFF - ('9' - '0'))		; 0xFF - ('9' - '0') = 0xF6
	  ; 0x00-0x2F -- 0xC6-0xF5 -- C = 1
	  ; '0' - '9' -- 0xF6-0xFF -- C = 0
	  ; 0x3A-0xFF -- 0x00-0xC5 -- C = 1
	btfss	STATUS, C, a
	retlw	1
	retlw	0

	end
isxdigit
	radix	dec
	include	"p16cxxx.inc"

	;	Input	  Output
	;
	;	'0'-'9'	  1
	;	other	  0

	global	isxdigit

; WREG  -- [Input]  A character. (SFR Area)

; WREG  -- [Output] Logical result.

code_isxdigit code
isxdigit:
	addlw	(0x100 - '0')			; 0x100 - '0' = 0xD0
	  ; 0x00-0x2F -- 0xD0-0xFF
	  ;  '0'-'9'  -- 0x00-0x09
	  ; 0x3A-0x40 -- 0x0A-0x10
	  ;  'A'-'F'  -- 0x11-0x16
	  ; 0x47-0x60 -- 0x17-0x30
	  ;  'a'-'f'  -- 0x31-0x36
	  ; 0x67-0xFF -- 0x37-0xCF
	addlw	(0xFF - ('9' - '0'))		; 0xFF - ('9' - '0') = 0xF6
	  ; 0x00-0x2F -- 0xC6-0xF5 -- C = 1
	  ;  '0'-'9'  -- 0xF6-0xFF -- C = 0
	  ; 0x3A-0x40 -- 0x00-0x06 -- C = 1
	  ;  'A'-'F'  -- 0x07-0x0C -- C = 1	The next offset value: 0x07
	  ; 0x47-0x60 -- 0x0D-0x26 -- C = 1
	  ;  'a'-'f'  -- 0x27-0x2C -- C = 1
	  ; 0x67-0xFF -- 0x2D-0xC5 -- C = 1
	btfss	STATUS, C
	retlw	1

	addlw	(0x100 - 0x07)			; 0x100 - 0x07 = 0xF9
	  ; 0x00-0x2F -- 0xBF-0xEE
	  ;  '0'-'9'  -- 0xEF-0xF8
	  ; 0x3A-0x40 -- 0xF9-0xFF
	  ;  'A'-'F'  -- 0x00-0x05
	  ; 0x47-0x60 -- 0x06-0x1F
	  ;  'a'-'f'  -- 0x20-0x25
	  ; 0x67-0xFF -- 0x26-0xBE
	addlw	(0xFF - ('F' - 'A'))		; 0xFF - ('F' - 'A') = 0xFA
	  ; 0x00-0x2F -- 0xB9-0xE8 -- C = 1
	  ;  '0'-'9'  -- 0xE9-0xF2 -- C = 1
	  ; 0x3A-0x40 -- 0xF3-0xF9 -- C = 1
	  ;  'A'-'F'  -- 0xFA-0xFF -- C = 0
	  ; 0x47-0x60 -- 0x00-0x19 -- C = 1
	  ;  'a'-'f'  -- 0x1A-0x1F -- C = 1	The next offset value: 0x1A
	  ; 0x67-0xFF -- 0x20-0xB8 -- C = 1
	btfss	STATUS, C
	retlw	1

	addlw	(0x100 - 0x1A)			; 0x100 - 0x1A = 0xE6
	  ; 0x00-0x2F -- 0x9F-0xCE
	  ;  '0'-'9'  -- 0xCF-0xD8
	  ; 0x3A-0x40 -- 0xD9-0xDF
	  ;  'A'-'F'  -- 0xE0-0xE5
	  ; 0x47-0x60 -- 0xE6-0xFF
	  ;  'a'-'f'  -- 0x00-0x05
	  ; 0x67-0xFF -- 0x06-0x9E
	addlw	(0xFF - ('f' - 'a'))		; 0xFF - ('f' - 'a') = 0xFA
	  ; 0x00-0x2F -- 0x99-0xC8 -- C = 1
	  ;  '0'-'9'  -- 0xC9-0xD2 -- C = 1
	  ; 0x3A-0x40 -- 0xD3-0xD9 -- C = 1
	  ;  'A'-'F'  -- 0xDA-0xDF -- C = 1
	  ; 0x47-0x60 -- 0xE0-0xF9 -- C = 1
	  ;  'a'-'f'  -- 0xFA-0xFF -- C = 0
	  ; 0x67-0xFF -- 0x00-0x98 -- C = 1
	btfss	STATUS, C
	retlw	1
	retlw	0

	end
	radix	dec
	include	"p18cxxx.inc"

	;	Input	  Output
	;
	;	'0'-'9'	  1
	;	other	  0

	global	isxdigit

; WREG  -- [Input]  A character. (SFR Area)

; WREG  -- [Output] Logical result.

code_isxdigit code
isxdigit:
	addlw	(0x100 - '0')			; 0x100 - '0' = 0xD0
	  ; 0x00-0x2F -- 0xD0-0xFF
	  ;  '0'-'9'  -- 0x00-0x09
	  ; 0x3A-0x40 -- 0x0A-0x10
	  ;  'A'-'F'  -- 0x11-0x16
	  ; 0x47-0x60 -- 0x17-0x30
	  ;  'a'-'f'  -- 0x31-0x36
	  ; 0x67-0xFF -- 0x37-0xCF
	addlw	(0xFF - ('9' - '0'))		; 0xFF - ('9' - '0') = 0xF6
	  ; 0x00-0x2F -- 0xC6-0xF5 -- C = 1
	  ;  '0'-'9'  -- 0xF6-0xFF -- C = 0
	  ; 0x3A-0x40 -- 0x00-0x06 -- C = 1
	  ;  'A'-'F'  -- 0x07-0x0C -- C = 1	The next offset value: 0x07
	  ; 0x47-0x60 -- 0x0D-0x26 -- C = 1
	  ;  'a'-'f'  -- 0x27-0x2C -- C = 1
	  ; 0x67-0xFF -- 0x2D-0xC5 -- C = 1
	btfss	STATUS, C, a
	retlw	1

	addlw	(0x100 - 0x07)			; 0x100 - 0x07 = 0xF9
	  ; 0x00-0x2F -- 0xBF-0xEE
	  ;  '0'-'9'  -- 0xEF-0xF8
	  ; 0x3A-0x40 -- 0xF9-0xFF
	  ;  'A'-'F'  -- 0x00-0x05
	  ; 0x47-0x60 -- 0x06-0x1F
	  ;  'a'-'f'  -- 0x20-0x25
	  ; 0x67-0xFF -- 0x26-0xBE
	addlw	(0xFF - ('F' - 'A'))		; 0xFF - ('F' - 'A') = 0xFA
	  ; 0x00-0x2F -- 0xB9-0xE8 -- C = 1
	  ;  '0'-'9'  -- 0xE9-0xF2 -- C = 1
	  ; 0x3A-0x40 -- 0xF3-0xF9 -- C = 1
	  ;  'A'-'F'  -- 0xFA-0xFF -- C = 0
	  ; 0x47-0x60 -- 0x00-0x19 -- C = 1
	  ;  'a'-'f'  -- 0x1A-0x1F -- C = 1	The next offset value: 0x1A
	  ; 0x67-0xFF -- 0x20-0xB8 -- C = 1
	btfss	STATUS, C, a
	retlw	1

	addlw	(0x100 - 0x1A)			; 0x100 - 0x1A = 0xE6
	  ; 0x00-0x2F -- 0x9F-0xCE
	  ;  '0'-'9'  -- 0xCF-0xD8
	  ; 0x3A-0x40 -- 0xD9-0xDF
	  ;  'A'-'F'  -- 0xE0-0xE5
	  ; 0x47-0x60 -- 0xE6-0xFF
	  ;  'a'-'f'  -- 0x00-0x05
	  ; 0x67-0xFF -- 0x06-0x9E
	addlw	(0xFF - ('f' - 'a'))		; 0xFF - ('f' - 'a') = 0xFA
	  ; 0x00-0x2F -- 0x99-0xC8 -- C = 1
	  ;  '0'-'9'  -- 0xC9-0xD2 -- C = 1
	  ; 0x3A-0x40 -- 0xD3-0xD9 -- C = 1
	  ;  'A'-'F'  -- 0xDA-0xDF -- C = 1
	  ; 0x47-0x60 -- 0xE0-0xF9 -- C = 1
	  ;  'a'-'f'  -- 0xFA-0xFF -- C = 0
	  ; 0x67-0xFF -- 0x00-0x98 -- C = 1
	btfss	STATUS, C, a
	retlw	1
	retlw	0

	end