Using "mov DPTR, #data16" in 8051 ASM

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] = {1,2,3}; unsigned int *index; extern void programcode(void);

void main(void) {

index = &TEST[0]; programcode();

}

------------------------------------------

and in test2.asm I have:

------------------------------------------ EXTRN DATA (index)

programcode: mov A, #0 mov DPTR, #index movc A, @A+DPTR ret END

------------------------------------------

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.

Reply to
Jack
Loading thread data ...

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

Reply to
Gary Kato

"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

Reply to
Karl Olsen

If two bytes,

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

Reply to
Gary Kato

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.

Reply to
CBarn24050

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

Reply to
Jack

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] = {0x2B67,0x56CE,0x8235}; unsigned int code *ArrayStart; . . byteindex = 0; ArrayStart = Table; int_isr(); . .

In ASM:

-------

int_isr: 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 (0x2B) mov B, A ;save MSB into B clr A inc DPTR movc A, @A+DPTR ;get LSB of Table[0] contents into ACC (0x67)

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 Jack

Reply to
Jack

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 (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
Reply to
Hans-Bernhard Broeker

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.