
#include <p16c5x.inc>
	
;***** VARIABLE DEFINITIONS

TEMP_VAR        UDATA
BINARY 		RES		1	; Binary input
DBINARY		RES		1	; Dummy binary
BCD100 		RES		1	; BCD 100th position
BCD10 		RES		1	; BCD 10th position
BCD1		RES		1	; BCD 1st position
LBCD		RES 	        1 	; local BCD declaration
POWER		RES		1	; BCD 1st position
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	
       	       clrw			;clear accumulator
	       movwf      BCD1	        ;store 0 to BCD1
	       movwf      BCD10		;store 0 to BCD10
	       movwf      BCD100	;store 0 to BCD100
	       movwf      LBCD		;store 0 to LBCD

	       
;read from to port b to binary

;port reading


               clrw                     ;clear accumulator
               TRIS       PORTA         ;Set port A as output
               movlw      0x0           ;move literal value to accumulator
               movwf      PORTA         ;move acuumaltor to port A



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

               movf       PORTB,0       ;move port B b to accumulator
               movwf      BINARY        ;move accumulator to multiplicand
	
	       
	       movlw	  0x64		;move literal value to accumulator
	       movwf	  POWER		;move 64 to POWER
borrowp
	       movf 	  BINARY,0	;move binary to accumulator
	       movwf 	  DBINARY	;move binary to dummy binary	
	       movf	  POWER,0	;move power to accumulator
	       subwf	  BINARY,1	;subtract accumulator from Binary,result
                                        ;is stored in BINARY register
               btfss	  STATUS,0	;skip next statement if CY=0									
	       goto       borrown
               incf       LBCD,1	;increment BCD100
	       goto       borrowp  	;goto the next sequence

borrown
		movf 	  DBINARY,0	;move dummy binary to accumulator
		movwf 	  BINARY	;move  dummy binary to binary
		movf	  LBCD,0	;Local BCD to accumulator
        	movlw     0x64		;move literal value to accumulator
		subwf     POWER,0	;subtract 64 from power
		btfsc     STATUS,2 	;skip if clear,skip if z =0,output is not zero					
		call      power64		
                btfsc     STATUS,2 	;skip if clear,skip if z =0,output is not zero	
		goto      borrowp

;if power is 0xa
		movf	  LBCD,0	;Local BCD to accumulator
		movwf 	  BCD10		;move  Local BCD to BCD 10
		movf	  BINARY,0	;move binary to accumulator
		movwf	  BCD1		;move binary to BCD
		
;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


                  movf    BCD1,0         ;move BCD1 to 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
                  movf    BCD10,0       ;move BCD10 to accumulator
                  movwf   dig1          ;move accumulator to 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


;second  digit

                   movf   BCD100,0      ;move BCD100 to 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


                  clrw                  ;clear accumulator
                  movwf   dig3          ;move accumulator to 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

repeat            goto    repeat

power64
                  movf    LBCD,0	 ;Local BCD to accumulator
		  movwf   BCD100         ;move  Local BCD to BCD 100
		  clrw                   ;clear accumulator
		  movwf   LBCD		 ;move accumulator to LBCD
		  movlw   0xa            ;move literal value to accumulator		
		  movwf   POWER          ;move accumulator to POWER
		  return
		  end                    ;directive end of program'


