	
<b>
        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</b>
        ORG 0
	MOV P3,#255	;Making P3 Output Port

	MOV <b>OldValues+0</b>,#255	;Initilising memory locations for storing output 
	MOV <b>OldValues+1</b>,#255	;from the 8 latches
	MOV <b>OldValues+2</b>,#255	;and loading them with initial values
	MOV <b>OldValues+3</b>,#255	;which indicate all signals are low
	MOV <b>OldValues+4</b>,#255
	MOV <b>OldValues+5</b>,#255
	MOV <b>OldValues+6</b>,#255
	MOV <b>OldValues+7</b>,#255
<b>;three nested loops needed: one for the bits, one for the 8 bytes, one which repeats the all infinitely</b>
<b>LOOP:</b>
	MOV R0,<b><u>#</u>OldValues+7</b>  ;location R0 is Rlatch
<b>POLL:</b>
<b>        MOV A,R0
        ADD A,#-OldValues   ;calculate latch Nr
        MOV P1,A            ;set the latch Nr for the decoder</b>
        MOV <b>RPin</b>,<b><u>#8</u></b>	;there are 8 bits in a byte
	MOV A,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 <b>RPin</b>,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
;<b> following line not needed if it is initialised at the beginning of the loop</b>
;	MOV 10,#8	;ReInitialising Rpin value as 8 for next loop
;following line replaced by 2 lines as R0 won't point into 1..8 any more
;	DJNZ R0,POLL
        <b>DEC   R0
        CJNE R0,#OldValues-1,POLL</b>
;again no need
;	MOV R0,#8	;ReInitilising value of Rlatch as 8 for next loop
<b>     JMP LOOP       ;repeat infinitely this loop</b>

WRITE:	
        MOV <b>AStore</b>,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
	MOV P3,A
	MOV A,<b>AStore</b>	;Returning A its original Value
	JMP GETBAK
	END
