Texas Instruments MSC1210 User Manual
Page 226
Reading Code Memory/Tables (MOVC)
16-32
16.30 Reading Code Memory/Tables (MOVC)
It is often useful to be able to read code memory itself from within a program.
This allows for the placement of data or tables in code memory to be read at
run time by the program itself. This is accomplished by the MOVC instruction.
The MOVC instruction comes in two forms: MOVC A,@A+DPTR and
MOV A,@A+PC. Both instructions move a byte of code memory into the
accumulator. The code memory address from which the byte is read depends
on which of the two forms is used.
MOV C A,@A+DPTR reads the byte from the code memory address calcu-
lated by adding the current value of the accumulator to that of DPTR. For ex-
ample, if DPTR holds the value 1234
H
and the accumulator holds the value
10
H
, the instruction copies the value of code memory address 1244
H
into the
accumulator. This can be thought of as an absolute read because the byte is
always read from the address contained in the two registers, accumulator and
DPTR. DPTR is initialized to point to the first byte of the table, and the accumu-
lator is used as an offset into the table.
For example, perhaps there is a table of values that resides at 2000
H
in code
memory. A subroutine needs to be written that obtains one of those six values
based on the value of the accumulator. This could be coded as:
MOV A,#04h
;Set accumulator to offset into the
;table we want to read
LCALL SUB
;Call subroutine to read 4th byte of
;the table
…
SUB:
MOV DPTR,#2000h ;Set DPTR to the beginning of the
;value table
MOVC A,@A+DPTR
;Read the 5th byte from the table
RET
;Return from the subroutine
MOVC A,@A+PC will read the byte from the code memory address calculated
by adding the current value of the accumulator to that of the Program Counter;
that is, the address of the currently executing instruction. This can be thought
of as a relative read because the address of code memory from which the byte
will be read depends on where the MOVC instruction is found in memory. This
form of MOVC is used when the data read immediately follows the code that
read it.