Keilc data, bdata, idata, pdata, xdata, code storage type and Storage Area

Source: Internet
Author: User

Bit

It is an IP address of a single position in the 20 h. 2fh Area of the internal data storage space, or a 8051-bit address that can address SFr.

Code
Is a Code address between 0000 h .. 0 ffffh.

Data

It is a data storage address between 0 and 127, or a special function register (SFR) address within the range of 128 .. 255.

Idata
Is an idata storage address in the range of 0 to 255.

Xdata is an xdata storage address in the range of 0 to 65535.

Relationship between pointer types and storage areas

I. Relationship between Storage types and storage areas

Data ---> addressable in-chip RAM
Bdata ---> bit-addressable in-chip RAM
Idata ---> addressable in-chip RAM, allowing access to all internal RAM
Pdata ---> paging addressing off-chip RAM (movx @ R0) (256 byte/Page)
Xdata ---> addressable off-chip RAM (64 K address range)
Code ---> program storage area (64 K address range), corresponding to movc @ dptr

2. Relationship between pointer types and storage areas

When declaring a variable, you can specify the storage type of the variable, for example:
The same price between uchar data X and data uchar X is a Byte variable allocated to the internal RAM zone.

Similarly, for pointer variable declaration, the storage location of the pointer variable itself is different from the location of the storage area pointed to by the pointer variable.
Example:

Uchar xdata * Data pstr

It refers to allocating a pointer variable (The role of the Data keyword after "*") to the internal RAM zone, and the pointer itself points to the xdata zone (the role of the xdata keyword before ),
It may be hard to understand or remember when you are a beginner at the C51 level. It doesn't matter. We can immediately see the usage of different keywords before and after the corresponding "*" during compilation.

......
Uchar xdata TMP [10]; // create 10 bytes of memory in the external Ram zone. The address is 0x0000-0x0009 of the external Ram.
......

1st cases:

Uchar data * Data pstr;
Pstr = TMP;

First of all, we should remind you that such code has a bug and he cannot correctly access the TMP space in this way. Why? After compilation, we can see the following Assembly
Code:

MoV 0x08, # TMP (0x00); 0x08 is the storage address of the pointer pstr

Have you seen it! Originally, external Ram needs 2 bytes to address 64 K space, but because the data keyword (the one before "*") is used, compiling the environment by keilc
Compile it into a pointer variable pointing to the internal RAM. This is also a bug caused by a beginner who does not understand the definition of keywords for various storage types. Especially in the project
When the default storage class is large and the TMP [10] is declared as uchar TMP [10], such bugs are hidden and not easily discovered.

2nd cases:

Uchar xdata * Data pstr;
Pstr = TMP;

This is okay. This usage means that the internal RAM allocates a pointer variable (The role of the Data keyword after "*"), and the pointer itself points
Xdata area (the role of the xdata keyword before ). The compiled assembly code is as follows.

MoV 0x08, # TMP (0x00); 0x08 and 0x09 are the pstr pointer variable address space allocated in the internal RAM zone.
MoV 0x09, # TMP (0x00)

This should be the most efficient way to access external RAM in various cases. Please remember this.

3rd cases:

Uchar xdata * xdata pstr;
Pstr = TMP;

This is also true, but it is less efficient than 2nd cases. The compiled assembly code is as follows.

MoV dptr, # 0x000a; 0x000a, 0x000b is the pstr pointer variable address space allocated in the external Ram Zone
MoV A, # TMP (0x00)
MoV @ dptr,
INC dptr
MoV A, # TMP (0x00)
Movx @ dptr,

This method is generally used in projects with relatively tight internal RAM resources and low efficiency requirements.

4th cases:

Uchar data * xdata pstr;
Pstr = TMP;

If the readers read 1st cases in detail, they find that this writing method is very similar to 1st cases. Yes, there are bugs in the same way as 1st cases. But this time, pstr points
Assigned to the external Ram zone. The compiled assembly code is as follows.

MoV dptr, # 0x000a; 0x000a is the address space of the pstr pointer variable allocated in the external Ram Zone
MoV A, # TMP (0x00)
Movx @ dptr,

5th cases:

Uchar * Data pstr;
Pstr = TMP;

We have noticed that the keyword declaration before "*" is missing. So what will happen? Let's write it like this! By the way, the name of an old song of Qi Yu is "please contact me
Come on. Please come with me to check the compiled assembly code. Is someone asking if this is not about C51? Why do we need to show us the assembly code. To make good use of the C51, we should try to improve the C51.
After compilation, you can see that the compiled compilation will help you become a master in producing efficient C51 code as soon as possible. Check the code!

MoV 0x08, #0x01; 0x08-0x0a is the address space of the pstr pointer variable allocated in the internal RAM Zone
MoV 0x09, # TMP (0x00)
MoV 0x0a, # TMP (0x00)

Note: This is a new introduction. You may wonder why pstr pointer variables in the previous situations use 2 byte space and 3 byte space.
What about it? This is a system internal processing of keilc. In keilc, a pointer variable occupies a maximum of 3 bytes of space. For pointers that do not declare pointers to the bucket type,
When compiling code, the system forces a byte pointer type resolution value to be loaded. For more information about the corresponding relationship, see the C51 User's Guide in the help section of keilc.

6th cases:

Uchar * pstr;
Pstr = TMP;

This is the most direct and simple pointer variable Declaration, but it has the lowest efficiency. Let's say that together! The compiled assembly code is as follows.

MoV dptr, # 0x000a; 0x000a-0x000c is the pstr pointer variable address space allocated in the external Ram zone.
MoV A, #0x01
MoV @ dptr,
INC dptr
MoV dptr, # 0x000a
MoV A, # TMP (0x00)
MoV @ dptr,
INC dptr
MoV A, # TMP (0x00)
Movx @ dptr,

This situation is similar to the combination of 5th and 3rd cases. pstr is allocated out of the ram space and the resolution value of the pointer type is increased.

Summary: we have seen the above six cases, of which 2nd are the most efficient. This allows you to access the ram zone correctly and saves code, and the worst is 6th.
But it doesn't mean that we can only use 2nd methods. It depends on the situation. Generally, the internal RAM resources used to apply the 51 series system architecture are very tight.
Use the internal RAM for local variables within the defined function or program segment, instead of declaring the global variables as the internal RAM zone. Therefore, we recommend that you use
There are three cases, and 2nd methods are used for local pointer variables.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.