
       DSEG  AT  30h    	;this tells the compiler to start placing data from 30h
NrValues    EQU  8       	;const nrvalues=8;
OldValues:  DS   NrValues       ;reserve space for OldValues
RPin:       DS   1       	;reserve space for RPin (a counter)
AStore:     DS   1       	;reserve space where A will be stored temporarily

        CSEG             	;now we tell the compiler that we are going to write code
        ORG 0
	<b>MOV P3,#255		;Making P3 Input Port</b>

	MOV OldValues+0,#255	;Initilising memory locations for storing output 
	MOV OldValues+1,#255	;from the 8 latches
	MOV OldValues+2,#255	;and loading them with initial values
	MOV OldValues+3,#255	;which indicate all signals are low
	MOV OldValues+4,#255
	MOV OldValues+5,#255
	MOV OldValues+6,#255
	MOV OldValues+7,#255
				;three nested loops needed: one for the bits, 
				;one for the 8 bytes, one which repeats the all 
				;infinitely
LOOP:
	MOV R0,#OldValues+7	;location R0 is Rlatch
POLL:
        MOV A,R0
        ADD A,#-OldValues	;calculate latch Nr
        MOV P1,A		;set the latch Nr for the decoder
        MOV RPin,#8		;there are 8 bits in a byte
	MOV A,P3		;Reading data from Port3
	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 RPin,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
        CJNE R0,#OldValues-1,POLL
     	JMP LOOP		;repeat infinitely this loop

WRITE:	
        MOV AStore,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		;Sending data to Output Port2</b>
	MOV A,AStore		;Returning A its original Value
	JMP GETBAK
	END
