include "p16c5x.inc"
include "P16C5X.INC"

startup code
        org 0
start   nop
        org 10

        CLRF FSR        ;clearing FSR register
	CLRF INDF       ;clearing INDF register
	CLRW            ;clearing W register

	COMF INDF,0     ;complimenting INDF register
	TRIS PORTA	;setting PORTA in input mode

	BTFSS PORTA,0	;0 for Down counting & 1 for Up counting

FALSE	CALL D_CNT      ;call D_CNT subroutine
TRUE	CALL UP_CNT     ;call UP_CNT subroutine

;UP COUNTING STARTS HERE		
UP_CNT	CLRF FSR        ;clearing FSR register
	CLRF INDF       ;clearing INDF register
	CLRW            ;clearing W register
		
	TRIS PORTB      ;setting PORTB in output mode

	MOVLW 0X00      ;moving literal constant 0x00 to W
	MOVWF 08H       ;moving the content of W to 08H
REP1
	MOVWF PORTB     ;moving the content of W through PORTB
	INCF 08H,1      ;incrementing the content of 08H
	MOVF 08H,0      ;moving the content of 08H to W
	GOTO REP1       ;repeating the loop REP1
;UP COUNTING ENDS HERE		


;DOWN COUNTING STARTS HERE		
D_CNT	CLRF FSR        ;clearing FSR register
	CLRF INDF       ;clearing INDF register
	CLRW            ;clearing W register
		
	TRIS PORTB	;setting PORTB in output mode
        MOVLW 0XFF	;setting the start value
	MOVWF 08H       ;moving the content of W to 08H
REP2
	MOVWF PORTB     ;moving the content of W through PORTB
	DECF 08H,1      ;decrementing the value of 08H
	MOVF 08H,0      ;moving the content of 08H to W
	GOTO REP2       ;repeating the loop REP2
;DOWN COUNTING ENDS HERE
        RETLW start
	END             ; directive 'end of program'

