Using "mov DPTR, #data16" in 8051 ASM

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Referring to instruction:

mov DPTR, #data16

I have 2 programs:  test.c and test2.asm.  In test.c I have:

const unsigned int code TEST[3] = ;
unsigned int *index;
extern void programcode(void);

void main(void) {

index = &TEST[0];


and in test2.asm I have:

EXTRN DATA (index)

    mov A, #0
    mov DPTR, #index
    movc A, @A+DPTR

Does "mov DPTR, #index" mean "move the value stored in variable named
"index" into DPTR" or does it mean "move the address of variable named
"index" into DPTR"?  I observe the latter, but I want to implement the
former.  Any help would be appreciated.

Re: Using "mov DPTR, #data16" in 8051 ASM
Quoted text here. Click to load it

Assuming index is just one byte, try:
mov  A,index
mov DPL,A
mov A,#0
mov DPH, A

Re: Using "mov DPTR, #data16" in 8051 ASM
If two bytes,

mov  A,index
mov DPL,A
mov A,index+1   ; assuming 8051 is little-endian
mov DPH, A

Re: Using "mov DPTR, #data16" in 8051 ASM

Quoted text here. Click to load it

"mov DPTR, #index" means move the address of variable "index" into dptr.  To
read a byte stored in this variable, you then use "movc a, @a+dptr" if index
is in code space, and "movx a, @dptr" if index is in xdata space.

Karl Olsen

Re: Using "mov DPTR, #data16" in 8051 ASM
Quoted text here. Click to load it

Firstly #index is not a variable, its a number, so the operation simply loads
that number into the DPTR register. Secondly the dptr is a 16 bit reg whereas
"normal" MOV instructions only move 8 bit data, so your operation would only
load half the dptr which I guess is not what you want. I assume you have a
table of values somewhere in ram which means you need to use the dtpr register
to fetch it with the MOVX instruction, you could store them in 2 registers,
then use MOV DPH,Rn and MOV DPL,R(n+1) to load the dptr. Maybe you should
rethink what it is you are trying to achieve.

Re: Using "mov DPTR, #data16" in 8051 ASM
Thank you for all the responses.  I managed to resolve the issue as follows:

mov A, byteindex
mov DPL, ArrayStart+1
mov DPH, ArrayStart
movc A, @A+DPTR;get MSB of timer constant
mov B, A                                        ;save MSB
anl A, #07FH                                ;clear msb
                        cpl A                                                ;complement A
                        mov TMR2RLH, A                            ;load MSB into Timer2 MSB
                        clr A                                                ;zero byte index
                        inc DPTR                                        ;increment table pointer
                        movc A, @A+DPTR                            ;get LSB of constant
                        cpl A                                                ;complement A
                        mov TMR2RLL, A                            ;load LSB into Timer2 LSB
                        mov A, B

Re: Using "mov DPTR, #data16" in 8051 ASM
Thank you for all the responses (ignore previous incomplete post)!  I
managed to resolve issue as follows:

In C:

const unsigned int code Table[3] = ;
unsigned int code *ArrayStart;
byteindex = 0;
ArrayStart = Table;


    mov A, byteindex
    mov DPL, ArrayStart+1   ;load ArrayStart LSB into DPL
    mov DPH, ArrayStart     ;load ArrayStart MSB into DPH
    movc A, @A+DPTR         ;get MSB of Table[0] contents into ACC
    mov B, A            ;save MSB into B
    clr A
    inc DPTR
    movc A, @A+DPTR         ;get LSB of Table[0] contents into ACC

This seems to work OK.  I guess when one loads DPTR with an integer
variable, it loads the address of the variable into DPTR.  But if one
loads DPL and DPH individually with an integer variable, the
variable's value itself (not its address) is loaded, MSB then LSB,
into DPTR.

Thanks & Regards

Re: Using "mov DPTR, #data16" in 8051 ASM

Quoted text here. Click to load it

First of all, you should never, repeat: *NEVER*, resort to guessing
what a piece of assembly code actually does.  If you have to guess, it
means you don't truly understand what your program is doing, and it
may mean that you're simply not up to writing that particular routine
in assembly.

Second, no, the difference is not between loading bytes and 16-bit
quantities.  It's between assembly operands that have a leading '#',
and those that don't.

    mov dptr, #number

is effectively the same as

    mov DPL, #BYTE0(number)
    mov DPH, #BYTE1(number)    

(assuming Keil A51 syntax), which is quite different from

    mov DPL, BYTE0(number)
    mov DPH, BYTE1(number)

Hans-Bernhard Broeker (
Even if all the snow were burnt, ashes would remain.

Site Timeline