
#include <p16c5x.inc>

;***** VARIABLE DEFINITIONS
TEMP_VAR                UDATA


bcd        		RES     1  	;bcd variable definition
binary			RES	1	;binary variable declaration
bcdlsb			RES	1	;bcdlsb variable declaration
bcdmsb			RES 	1	;bcdmsb variable declaration
multiplicand	        RES	1	;multiplicand varible declaration
multiplier		RES	1	;multiplier variable declaration
result			RES	1	;result variable declaration
count			RES	1	;count 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


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

MAIN             CODE    0x000
start	


; remaining code goes here
		
;read bcd value to bcd througgh port b

;port reading


                 clrw                 ;clear accumulator
                 TRIS    PORTA        ;Set PORT A as output
                 movlw   0x0          ;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 portb b to accumulator
                 movwf   bcd          ;move accumulator to bcd

;display bcd value
;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


;second  digit


                  movlw   0x0f         ;move literal value to accumulator
                  andwf   bcd,0        ;AND resultmsb 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

                  movlw   0xf0          ;move literal value to accumulator
                  andwf   bcd,0         ;AND resultmsb 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


;separate bcd to bcd msb and bcd lsb

                  movlw   0x0f          ;move literal value to accumulator
                  andwf   bcd,0         ;and bcd with accumator and output in accumulator
                  movwf   bcdlsb        ;move accumulator to bcdlsb
                  
                  movlw   0xf0          ;move literal value to accumulator
                  andwf   bcd,0         ;and bcd  with accumulator and output in accumulator
                  movwf   bcdmsb        ;move accumulator to bcdmsb
                  swapf   bcdmsb,1      ;swap and result in bcdmsb

;conversion algorithm		
		

                  movf 	  bcdmsb,0	;move bcdmsb to accumulator
		  movwf	  multiplicand	;move accumulator to mulitplicand
		
		  movlw   0xa	        ;move literal value to accumulator
	   	  movwf   multiplier    ;move accumulator to mulipiler

		  call 	  multiplication;for multiplying bcdmsb by 0xa
		
	  	  movf    bcdlsb,0	;move bcdlsb to accumulator
		  addwf   result,1	;add accumulator with result and store in result	
		

;display result to hpdl 1414
;separation of zeroth  digit

                  movlw   0x0f          ;move literal value to accumulator
                  andwf   result,0      ;AND result 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   result,0      ;AND result 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


repeat            goto    repeat       ;infinite loop


multiplication

		  clrw 			;clear accumulator
		  movwf   result	;move accumulator to result
	          incf 	  multiplier,1	;increment multiplier by 1 and store in multiplier
		  movf 	  multiplier,0	;move multiplier to accumulator
		  movwf	  count		;move multiplier value to count
loop
		  decfsz  count,1	;decrement count and test is zero if yes then skip
		  goto    addition      ;goto addition
		  retlw   0		;return subroutine

addition
		  movf 	  multiplicand,0;move multiplicand to accumulator
		  addwf   result,1      ;add result with accumulator
		  goto    loop          ;goto loop
                  end                   ;directive 'end of program'


		


		


		
	
	
		

					


		
