
	ORG 0
	MOV P<b>3</b>,#255	;<b>Making P3 and NOT P2 as INPUT Port </b>
	MOV 1,#255	;Initilising memory locations for storing output 
	MOV 2,#255	;from the 8 latches
	MOV 3,#255	;and loading them with initial values
	MOV 4,#255	;which indicate all signals are low
	MOV 5,#255
	MOV 6,#255
	MOV 7,#255
	MOV 8,#255
	MOV R0,8	;location R0 is Rlatch
	MOV 10,8	;location 10 is Rpin
POLL:	MOV P1,10	;write Rlatch to location P1
	MOV A,P3        ;Reading data from P3
	ANL A,@R0	;ANDing A with the locations 8 to 1 which will store CPL of P3
CHKPIN:	RLC A		;Get the Pins thatwent form LOW to HIGH
	JC WRITE
GETBAK:	DJNZ 10,CHKPIN
	MOV A,P3	;Since the output is latched, it has not changed since last time
	CPL A
	MOV @R0,A	;Moving the data of the respective latches to the respective registers
	MOV 10,#8	;ReInitialising Rpin value as 8 for next loop
	DJNZ R0,POLL
	MOV R0,#8	;ReInitilising value of Rlatch as 8 for next loop
        <b>JMP POLL        ;Getting back to work again after completing one loop</b>
WRITE:	MOV 20,A	;storing the value of A in 20 for further use
	MOV A,R0
	ADD A,R0	;Adding contents of R0 to A seven more times
	ADD A,R0	;I read that using MUL always clears the flag
	ADD A,R0	;And I dont want to do that
	ADD A,R0
	ADD A,R0
	ADD A,R0
	ADD A,R0
	ADD A,10
	<b>MOV P2,A</b>
	MOV A,20	;Returning A its original Value
	JMP GETBAK
	END
