
Site Search
SECTION VI - ON-CHIP ROM ACCESS AND INDEXED ADDRESSING MODE IN 8051
SECTION VI - ON-CHIP ROM ACCESS AND INDEXED ADDRESSING MODE IN 8051
To access data elements of look-up table entries located in the program ROM space of the 8051, indexed addressing mode is used. The instruction format for this mode will be "MOVC A, @A+DPTR". The 16 bit register DPTR and register A are used to form the address of the data element stores in on-chip ROM. The data elements are stored in the program or code space ROM of the 8051, the instruction MOVC will be used instead of MOV. The "C" in "MOVC" instruction mean code. In the instruction "MOVC A, @A+DPTR", the contents of A are added to the 16 bit register DPTR to form the 16 bit address of the data, which is to be needed.
Example # 1:
The word "CANADA" is used to burned into ROM locations stating at 300H, and that program is burned into ROM locations starting at 0. Lets analyze the program, where "CANADA" is stored after this program is run.
ORG 0000H | ;Burn into ROM starting at 0 |
---|---|
MOV DPTR, #300H | ;Data pointer DPTR=300H look-up table address |
CLR A | ;Clear accumulator A |
MOVC A, @A+DPTR | ;Get the character from code space |
MOV R0,A | ;Save it in R0 register |
INC DPTR | ;DPTR=301, pointing to the next character |
CLR A | ;Clear accumulator A |
MOVC A, @A+DPTR | ;Get the next character from code space |
MOV R1,A | ;Save it in R1 register |
INC DPTR | ;DPTR=302, pointing to the next character |
CLR A | ;Clear accumulator A |
MOVC A, @A+DPTR | ;Get the next character from code space |
MOV R2, A | ;Save it in R2 register |
INC DPTR | ;DPTR=303, pointing to the next character |
CLR A | ;Clear accumulator A |
MOVC A, @A+DPTR | ;Get the next character from code space |
MOV R3, A | ;Save it in R3 register |
INC DPTR | ;DPTR=304, pointing to the next character |
CLR A | ;Clear accumulator A |
MOVC A, @A+DPTR | ;Get the next character from code space |
MOV R4, A | ;Save it in R4 register |
INC DPTR | ;DPTR=305, pointing to the next character |
CLR A | ;Clear accumulator A |
MOVC A, @A+DPTR | ;Get the next character from code space |
MOV R5, A | ;Save it in R5 register |
HERE: SJMP HERE | ;Stay Here |
;Data is burned into code space starting at 300H below | |
ORG 300H | |
OURDATA: DB "CANADA" | |
END | ;End of program |
ROM locations 300H-305H have contents:
300H='C', 301H='A', 302H='N', 303H='A', 304H='D', 305H='A'.
ROM Location 300H Alphabet "C":
In the above assembly language program, the data pointer DPTR=300H. The instruction "MOVC A,A+DPTR" moves the contents of ROM location 300H i.e 300H+0=300H to register accumulator A. The register accumulator contains value 43H. 43H value is the ASCII value for "C". This value is then moved and saved to R0. The data pointer DPTR is then incremented to make DPTR=301H i.e 300+1=301H by instruction "DPTR INC". The accumulator is set to zero again i.e A=0 by the instruction "CLR A", to get the contents of the next ROM location which is 301H.
ROM Location 301H Alphabet "A":
Now the accumulator has value 41H which is the ASCII code of the next alphabet "A" for word "CANADA". The instruction "MOVC A,A+DPTR" moves the contents of ROM location 301H i.e 300H+1=301H to register accumulator A. The register accumulator contains value 41H. 41H value is the ASCII value for "A". This value is then moved and saved to R1. The data pointer DPTR is then incremented to make DPTR=302H i.e 301+1=302H by instruction "DPTR INC". The accumulator is set to zero again i.e A=0 by the instruction "CLR A", to get the contents of the next ROM location which is 302H.
ROM Location 302H Alphabet "N":
Now the accumulator has value 4EH which is the ASCII code of the next alphabet "N" for word "CANADA". The instruction "MOVC A,A+DPTR" moves the contents of ROM location 302H i.e 301H+1=302H to register accumulator A. The register accumulator contains value 4EH. 4EH value is the ASCII value for "N". This value is then moved and saved to R2. The data pointer DPTR is then incremented to make DPTR=303H i.e 302+1=303H by instruction "DPTR INC". The accumulator is set to zero again i.e A=0 by the instruction "CLR A", to get the contents of the next ROM location which is 303H.
ROM Location 303H Alphabet "A":
Now the accumulator has value 41H which is the ASCII code of the next alphabet "A" for word "CANADA". The instruction "MOVC A,A+DPTR" moves the contents of ROM location 303H i.e 302H+1=303H to register accumulator A. The register accumulator again contains value 41H. 41H value is the ASCII value for "A". This value is then moved and saved to R3. The data pointer DPTR is then incremented to make DPTR=304H i.e 303+1=304H by instruction "DPTR INC". The accumulator is set to zero again i.e A=0 by the instruction "CLR A", to get the contents of the next ROM location which is 304H.
ROM Location 304H Alphabet "D":
Now the accumulator has value 44H which is the ASCII code of the next alphabet "N" for word "CANADA". The instruction "MOVC A,A+DPTR" moves the contents of ROM location 302H i.e 301H+1=302H to register accumulator A. The register accumulator contains value 4EH. 4EH value is the ASCII value for "N". This value is then moved and saved to R4. The data pointer DPTR is then incremented to make DPTR=303H i.e 302+1=303H by instruction "DPTR INC". The accumulator is set to zero again i.e A=0 by the instruction "CLR A", to get the contents of the next ROM location which is 303H.
ROM Location 305H Alphabet "A":
Now the accumulator has value 41H which is the ASCII code of the next alphabet "A" for word "CANADA". The instruction "MOVC A,A+DPTR" moves the contents of ROM location 305H i.e 304H+1=305H to register accumulator A. The register accumulator contains once again value 41H. 41H value is the ASCII value for "A". This value is then moved and saved to R5. When the whole program run, we have R0=43H, R1=41H, R2=4EH, R3=41H, R4=44H, R5=41H. The ASCII values for characters "C", "A", "N", "A", "D", "A".
Example # 2:
The ROM space starting at 200H contains "CANADA". This program transfers the bytes into ROM locations starting at 30H.
By Counter Method:
ORG 0000 | |
---|---|
MOV DPTR, #OURDATA | ;Load ROM Pointer |
MOV DPTR, #30H | ;Load RAM pointer |
MOV R3, #6 | ;Load counter for word "CANADA" |
BACK: CLR A | ;Accumulator register set to zero A=0 |
MOVC A, @A+DPTR | ;Move data from code space |
MOV @R0, A | ;Save fetched value from code space into RAM location pointed to by R0 |
INC DPTR | ;Increment ROM data pointer DPTR |
INC R0 | ;Increment RAM pointer |
DJNZ R3, BACK | ;Loop until counter is zero for word "CANADA" |
HERE: SJMP HERE | ;Stay in this loop |
;-----On-Chip Code Space Used for Storing Data | |
ORG 200H | |
OURDATA: DB "CANADA" | |
END |
Null Char for end of String Method:
ORG 0000 | |
---|---|
MOV DPTR, #OURDATA | ;Load ROM pointer |
MOV R0, #30H | ;Load RAM pointer |
BACK: CLR A | ;Accumulator register set to zero i.e A=0 |
MOVC A, @A+DPTR | ;Move data from code space |
JZ HERE | ;Exit from loop if NULL Character found |
MOV @R0, A | ;Save fetched value from code space into RAM location pointed to by R0 |
INC DPTR | ;Increment ROM pointer |
INC R0 | ;Increment RAM pointer |
SJMP BACK | ;Repeat the loop |
HERE: SJMP HERE | ;Stay in this loop forever |
;-----On-Chip Code Space Used for Storing Data | |
ORG 200H | |
OURDATA: DB "CANADA", 0 | ;Notice the NULL CHARACTER at the end of string |
"JZ" instruction is used to detect the Null Character, 0, at the end of string.
The Use of Look-Up Table:
The look-up table is used to allow access to table with minimum operations. We can use the look-up table for specific type of application where we need to avoid calculations, by just putting the calculated values inside the look-up table and fetch them as required without performing complex mathematical calculations
Example # 3:
To get the value of x from Port 2 i.e P1, and send the square of the value x2 to Port 3 i.e P3.
ORG 0000 | |
---|---|
MOV DPTR, #200H | ;Load the Look-up table address |
MOV A, #0FFH | ;Load accumulator A with FFH i,e A=FFH |
MOV P2, A | ;Configue the Port 1 as input port |
BACK: MOV A, P2 | ;Get the value of 'x' |
MOVC A, @A+DPTR | ;Get the x square from look-up table |
MOV P3, A | ;Send the squared value to Port 3 |
SJMP BACK | ;Keep doing it repeatedly |
ORG 200H | |
LOOKUP_TABLE: DB 0,1,4,9,16,25,36,49,64,81 |
The first instruction "MOV DPTR,#200H" can also be replaced with:
"MOV DPTR, #LOOKUP_TABLE"