
#include <p16c5x.inc>         ; processor specific variable definitions

;***** VARIABLE DEFINITIONS
TEMP_VAR    UDATA
bcd1		res	1     ;bcd1 variable declaration
bcd2		res	1     ;bcd2 variable declaration
sum		res	1     ;sum variable declaration
carry		res	1     ;carry variable declaration
sum_lsb		res	1     ;sum_lsb variable declaration
sum_msb		res	1     ;sum_msb variable declaration
dig0            res	1     ;dig0 variable declaration
dig1            res	1     ;dig1 variable declaration
dig2            res	1     ;dig2 variable declaration
dig3            res	1     ;dig3 variable declaration
control         res	1     ;control variable declaration


;**********************************************************************

RESET_VECTOR     CODE    0x1FF        ; processor reset vector
                 goto    start

MAIN             CODE    0x000
start	

; remaining code goes here


;port reading


                 clrw                  ;clear accumulator
                 tris   PORTA          ;Set port A as output
;set 74245 as output mode
                 movlw   0x00          ;move literal value to accumulator
                 movwf   PORTA         ;move accumulator to port A

                 movlw   0xff          ;move literal value to accumulator
                 tris    PORTB         ;Set port B as input

                 movf    PORTB,0       ;move port B  to accumulator
                 movwf   bcd1          ;move accumulator to bcd1

                 movlw   0xff          ;move literal value to accumulator
                 tris    PORTC         ;Set port C as input

                 movf    PORTC,0       ;move port C to accumulator
                 movwf   bcd2          ;move accumulator to bcd2


;perform  binary addition

;set msb as zero

		 clrw			;clear accumulator 		
                 movwf	carry		;move accumulator to carry
;add bcd and bcd2		
                 movf	bcd2,0		;move bcd2 to accumulator
 		 addwf	bcd1,0		;add accumulator and bcd1 and result in accumulator
;move result to sum,sum_lsb,sum_msb

	 	 movwf	sum		;move accumulator to sumlsb
	 	 movwf	sum_lsb		;move accumulator to sum_lsb
		 movwf	sum_msb		;move accumulator to sum_msb

; if carry bit is 1 set carry

		 btfsc	STATUS,0   	;check carry  = 0 ,skip if yes
		 bsf	carry,0		;set carry as 1
		
;check dc = 1  if true then call add6

		 btfsc	STATUS,1  	;check dc = 0 ,skip if yes
		 goto   add6	  	;goto add6

;masking upper nibble of sum_lsb

		 movlw	0x0f		;move literal value to accumulator
		 andwf	sum_lsb,1	;AND sum_lsb with accumulator and result in sumlsb_lsb	

;check sum_lsb greater than 9

		 movlw	0x9		;move literal value to accumulator
		 subwf	sum_lsb,0	;subtract 9 from sum_lsb result in accumulator

;if cy = 0 then it is sum_lsb is less than 9
;if sum_lsb grater than 9 add6 to lsb

		 btfsc	STATUS,0	; skip if carry is zero
		 goto   add6            ;goto add6

carrycheck


;if carry is one add 6 to msb

		 btfsc	carry,0       ;check carry  = 0 ,skip if yes
                 goto   add6_msb	;got add6_msb


;masking lower nibble of sum_msb and swap the result

		 movlw	0xf0		;move literal value to accumulator
		 andwf	sum_msb,1	;and sum_msb with accumulator and result in sumlsb_lsb
		 swapf	sum_msb,1	;swap sum_msb and result in sum_msb
		 
;check sum_msb is greater than 9
		 movlw	0x9		;move literal value to accumulator
		 subwf	sum_msb,0	;subtract 9 from sum_lsb result in accumulator

;if cy = 0 then  sumlsb_msb is less than 9
;if cy= 1  then  add 6 to msb

		 btfsc	STATUS,0	; skip if carry is zero
		 goto add6_msb          ; goto add6_msb

;end of the conversion

		 goto repeat 		;ends
		
;add 6 to sum
add6
		 movlw 	0x6		;move literal value to accumulator
		 addwf  sum,1	        ;add accumulator with sum result in sum_lsb

 ; if carry bit is 1 set carry
 
		 btfsc	STATUS,0       ;check carry  = 0 ,skip if yes
                 bsf	carry,0	       ;set carry as 1
 		
		 goto	carrycheck	;goto carry check

;add 60 to sum
add6_msb

		 movlw 	0x60		;move literal value to accumulator
                 addwf   sum,1		;add accumulator with sum result in sum
;if carry set carry

		 btfsc	STATUS,0       ;check carry  = 0 ,skip if yes
                 bsf	carry,0	       ;set carry as 1
                 
;end of conversion
		
repeat		
;display of output

;hpdl 1414 implementation

;74245 as output mode
              	 movlw   0xf          ;move literal value to accumulator
                 movwf   PORTA        ;move accumulator TO port A

                 movlw   0x00         ;move literal value to accumulator
                 tris    PORTB        ;Set PORT B as output

                 movlw   0x00         ;move literal value to accumulator
                 tris    PORTC        ;Set PORT C as output

;display  of DIG0

;Set 74245 in output mode

;separation of zeroth  digit

                  movlw  0x0f         ;move literal value to accumulator
                  andwf  sum,0        ;AND sum with accumulator and
                                      ;output in accumulator
                  movwf  dig0         ;move accumulator to dig0

;check digit is greater than 9

                  movlw  0x09         ;move literal value to accumulator
                  subwf  dig0,0       ;subtract 9 from dig0 and result in accumulator

                  btfss  STATUS, 0    ;skip if carry flag  is 1
                  goto   nochange     ;goto nochange

                  btfsc  STATUS,2     ;skip if zero flag is zero
                                      ;ie result is non zero
                  goto   nochange     ;goto nochange

                  movlw  0x09         ;move literal value to accumulator
                  subwf  dig0,1       ;subtract 9 from dig0 and result in dig0

                  movlw  0x13         ;move literal value to accumulator
                  movwf  control      ;move control to accumulator

                  goto   porting0     ;goto porting 0

nochange          movlw  0x0f         ;move literal value to accumulator
                  movwf  control      ;move control to accumulator

porting0          movf   dig0,0       ;move dig0 value to accumulator
                  movwf  PORTB        ;move accumulator to portB

                  movf   control,0    ;move control value to accumulator
                  movwf  PORTC        ;move accumulator to port C

                  movlw  0xff         ;move literal value to accumulator
                  movwf  control      ;move control to accumulator

                  movf   control,0    ;move control value to accumulator
                  movwf  PORTC        ;move accumulator to port C



;zeroth  digit complete

;first  digit

;separation of  first  digit

                  movlw  0xf0          ;move literal value to accumulator
                  andwf  sum,0         ;AND sum with accumulator and output in accumulator
                  movwf  dig1          ;move accumulator to dig1
                  swapf  dig1,1        ;swap and result in dig1

;check digit is greater than 9

                  movlw  0x09         ;move literal value to accumulator
                  subwf  dig1,0       ;subtract 9 from dig1 and result in accumulator

                  btfss  STATUS, 0    ;skip if carry flag  is 1
                  goto   nochange1    ;goto nochange1

                  btfsc  STATUS,2     ;skip if zero flag is zero
                                      ;ie result is non zero
                  goto   nochange1    ;goto nochange1

                  movlw  0x09         ;move literal value to accumulator
                  subwf  dig1,1       ;subtract 9 from dig1 and result in dig1

                  movlw  0x12         ;move literal value to accumulator
                  movwf  control      ;move control to accumulator

                  goto   porting1     ;goto porting 1

nochange1         movlw  0x0e         ;move literal value to accumulator
                  movwf  control      ;move control to accumulator

porting1          movf   dig1,0       ;move dig1 value to accumulator
                  movwf  PORTB        ;move accumulator to portB

                  movf   control,0    ;move control value to accumulator
                  movwf  PORTC        ;move accumulator to port C

                  movlw  0xff         ;move literal value to accumulator
                  movwf  control      ;move control to accumulator

                  movf   control,0    ;move control value to accumulator
                  movwf  PORTC        ;move accumulator to port C


;separation of second  digit

                  movlw   0x0f         ;move literal value to accumulator
                  andwf   carry,0      ;AND carry with accumulator and output in accumulator
                  movwf   dig2         ;move accumulator to dig2

;check digit is greater than 9

                  movlw   0x09         ;move literal value to accumulator
                  subwf   dig2,0       ;subtract 9 from dig2 and result in accumulator

                  btfss   STATUS, 0    ;skip if carry flag  is 1
                  goto    nochange2    ;goto nochange2

                  btfsc   STATUS,2     ;skip if zero flag is zero
                                       ;ie result is non zero
                  goto    nochange2    ;goto nochange2

                  movlw   0x09         ;move literal value to accumulator
                  subwf   dig2,1       ;subtract 9 from dig2 and result in dig2

                  movlw   0x11         ;move literal value to accumulator
                  movwf   control      ;move control to accumulator

                  goto    porting2     ;goto porting 2

nochange2         movlw   0x0d         ;move literal value to accumulator
                  movwf   control      ;move control to accumulator

porting2          movf    dig2,0       ;move dig2 value to accumulator
                  movwf   PORTB        ;move accumulator to portB

                  movf    control,0    ;move control value to accumulator
                  movwf   PORTC        ;move accumulator to port C

                  movlw   0xff         ;move literal value to accumulator
                  movwf   control      ;move control to accumulator

                  movf    control,0    ;move control value to accumulator
                  movwf   PORTC        ;move accumulator to port C


;second digit   complete

;third digit

;separation of  third  digit

                  movlw   0xf0          ;move literal value to accumulator
                  andwf   carry,0        ;AND carry with accumulator and output in accumulator
                  movwf   dig3          ;move accumulator to dig3
                  swapf   dig3,1        ;swap and result in dig3

;check digit is grater than 9

                  movlw    0x09         ;move literal value to accumulator
                  subwf    dig3,0       ;subtract 9 from dig3 and result in accumulator

                  btfss    STATUS, 0    ;skip if carry flag  is 1
                  goto     nochange3    ;goto nochange3

                  btfsc    STATUS,2     ;skip if zero flag is zero
                                        ;ie result is non zero
                  goto     nochange3    ;goto nochange3

                  movlw    0x09         ;move literal value to accumulator
                  subwf    dig3,1       ;subtract 9 from dig3 and result in dig3

                  movlw    0x10          ;move literal value to accumulator
                  movwf    control       ;move control to accumulator

                  goto     porting3      ;goto porting 3

nochange3         movlw    0x0c          ;move literal value to accumulator
                  movwf    control       ;move control to accumulator

porting3          movf     dig3,0        ;move dig3 value to accumulator
                  movwf    PORTB         ;move accumulator to portB

                  movf     control,0     ;move control value to accumulator
                  movwf    PORTC         ;move accumulator to port C

                  movlw    0xff          ;move literal value to accumulator
                  movwf    control       ;move control to accumulator

                  movf     control,0     ;move control value to accumulator
                  movwf    PORTC         ;move accumulator to port C 	


repeat2           goto repeat2		 ;infinte loop
		  end                   ; directive end of program

