8051 Can you explain to me this program

Thread Starter

antiantianti

Joined Aug 4, 2016
45
Hi
Can somebody please explain to me this program line by line I tried googling for the instruction didnt understand the program as a whole


MOVC A, @A+DPTR ; what is this instruction doing
MOV DPTR, #MYDATA what do we need to do this
MOV R2, #5 ; same question
Code:
  cseg at 0
    jmp 100
  cseg at 100
/* 5. Assuming that ROM space starting at 248H contains “Shiue”, write a program to
transfer the bytes into RAM locations starting at 37H using indexed address mode (6
points)*/
ORG 0 ; ROM location starts from 0000H
MOV DPTR, #MYDATA ; Copy the content in ROM to RAM
MOV R0, #37H ; R0 is a pointer starting from 37H
MOV R2, #5 ; “Shiue” has 5 characters
Back: CLR A ; A is reused, so it needs to be cleared
MOVC A, @A+DPTR ; DPTR is a pointer
MOV @R0, A
INC DPTR
INC R0
    DJNZ R2, Back
Here: SJMP Here ; keep in the loop
ORG 248H
MYDATA: DB “Shiue” ; the data in the ROM starting 248H
END  
main:
       sjmp main
       end
 

absf

Joined Dec 29, 2010
1,968
Hi
Can somebody please explain to me this program line by line I tried googling for the instruction didnt understand the program as a whole


MOVC A, @A+DPTR ; what is this instruction doing
MOV DPTR, #MYDATA what do we need to do this
MOV R2, #5 ; same question
To understand the first 2 instructions, you have to know a little bit more about the 8051 hardware.

The original 8031/8032 have no user programmable program area inside.
But the 8051/8052 have built-in ROM (OTP-EPROM or UV-EPROM).
Only the AT89X51 has flash memory inside for user programs.

In 8051/2, if you extend the external memory (e.g. 32K SRAM plus 32K ROM)
You can access the external RAM using MOVX instruction. As external RAM > 256 bytes, you'd need a 16 bit pointer. That's why DPTR was used.

If my memory is correct, both the external memories (RAM and ROM) can overlap each other. Can someone here confirm this?

If you want to access the internal or external ROM, you'd need the MOVC instruction. See picture below:

MOVC and MOVX instr.PNG
Allen
 
Last edited:

absf

Joined Dec 29, 2010
1,968
I put your program into my 8051 simulator...

AAC HW2 8051 SIM.PNG
I single step your program from line 7 to 11.
At line 7. #MYDATA (0248H) is stored in DPTR on the right box.
8. #37 is written into R0
9. #05 is written into R2
10. ACC is cleared
11. The content pointed by DPTR (0248="S") is read into Acc. Look at content of A= 53H, b'01010011, ascii="S".

So can you figure the rest out by yourself? The comments are all there in the program.

Allen
 
Last edited:

absf

Joined Dec 29, 2010
1,968
You need to repeat the loop 5 times between L10 to L15.
The instruction at

15. DJNZ R2,Back ;will loop back 5 times each time decrementing R2 by one until R2 becomes 0, to Back.

So, DPTR will point to "S" , then after "INC DPTR" DPTR=DPTR+1
next, DPTR points to "H" , then DPTR=DPTR+2..............................
next, DPTR points to "I" , then DPTR=DPTR+3................................
next, DPTR points to "U" , then DPTR=DPTR+4................................
next, DPTR points to "E" , then DPTR=DPTR+5..................................

Allen
 
Top