Keywords in C51 data,idata,xdata,pdata,bdata

Source: Internet
Author: User

Written in the first words: The official website is the most credible answer. English is a good must see. Http://www.keil.com/support/man/docs/c51/c51_le_memtypes.htm

The following is reproduced in several Chinese, for reference only.

Data
Fixed refers to the front 0x00-0x7f 128 ram, can be directly read and write with ACC, the fastest, the resulting code is also the smallest.

Idata
Fixed refers to the 256 ram in front 0x00-0xff, where the first 128 and data 128 are identical only because of the different ways of accessing. Idata is accessed in a pointer-like manner in C. The statements in the assembly are: Mox ACC, @Rx. (Unimportant supplement: C-Idata do a good job of pointer-type access)

XData
External extended RAM, generally referred to as external 0x0000-0xffff space, accessed with dptr.

pdata

The lower 256 bytes of external extended RAM, the address appears on the A0-A7 when read and write, with Movx ACC, @Rx Read and write. This is more special, and C51 seems to have a bug, it is recommended to use less. But also has his merit, the concrete usage belongs to the intermediate question, here does not mention.

Startup.a51 the role
As with assemblies, the initialization of those variables and arrays defined in C is done in startup.a51, and if you have values when defining global variables, such as unsigned char data xxx= "100", then there will be relevant assignments in startup.a51. If there were no =100,startup.a51, he would be cleared 0. (Startup.a51 = = initialization of variables). After the initialization is complete, the SP pointer is also set. For non-variable areas, such as the stack area, there will be no assignment or clear 0 action.
Some people like to change the startup.a51, in order to satisfy their own some take for granted hobby, this is unnecessary, possibly wrong. For example, when the power-off protection to save some variables, but change startup.a51 to implement is a very stupid method, as long as the use of non-variable region characteristics, the definition of a pointer variable pointing to the lower part of the stack: 0xFF can be achieved. , why do you want to change it? So to speak: no need to change startup.a51 at any time, if you understand its characteristics

Bit

is in the internal data storage space in 20H.. The address of a bit in the 2FH area, which appears as a byte after the 20H of data and can be referenced to each other. Plus 8051 addressable SFR, but just tried, just 00h--7fh function, that is, when the data has changed color red, the future from 80H to--FFH is not a bit addressing area, is a special register of bit addressing, such as the address of the 11 of course will have a response.

code is in 0000H.. A code address between 0FFFFH.

Data is a memory address between 0 and 127,

The idata is a idata memory address in the range of 0 to 255.

idata is coincident with data 128 bytes Low, where only data represents 256 bytes of on-chip RAM,

The xdata is a xdata memory address in the range of 0 to 65535.

A detailed description of the relationship between pointer type and storage area

I. Storage type vs. store relationship

Data---> addressable on-chip RAM

Bdata---> Addressable on-chip RAM

Idata---> Addressable on-chip ram allowing access to all internal RAM

pdata---> Paging addressing off-chip RAM (MOVX @R0) (byte/page)

XData---> Addressable off-chip RAM (64k address range FFFFH)

Code---> Program Store (64k address range), corresponding to MOVC @DPTR

Ii. the relationship between the pointer type and the storage area

When declaring a variable, you can specify the storage type of the variable, such as:

UCHAR data x and data Uchar x equal prices are variables that allocate one byte in the internal Ram area.

Similarly, for pointer variable declarations, the corresponding store type keywords are associated with the storage location of the pointer variable itself and the location of the store to which the pointer points.

Use such as:

Uchar xdata * Data pstr

is to assign a pointer variable (the function of the data keyword after the "*") to the internal Ram area, and the pointer itself points to the XData area ("*" before the XData keyword),

May be beginner C51 when a bit difficult to understand also difficult to remember. It doesn't matter, we'll see what happens at compile time for the use of different keywords before and after the corresponding "*".

......

Uchar XData tmp[10]; 10 bytes of memory space in the external Ram area, the address is the 0x0000-0x0009 of external RAM

......

1th Case:

UCHAR data * Data pstr;

Pstr= "TMP";

First of all to remind you that this code is a bug, he can not be properly accessed in this way to the TMP space. Why? We'll compile and see the following compilation

Code:

MOV 0x08, #tmp (0x00); 0x08 is the storage address of the pointer pstr

See it! It would have required 2 byte to address 64k space, but because the data keyword was used (the one before the "*"), it was KEILC compiled environment

He compiled exponentially pointer variables into internal RAM, which is also a bug caused by novice C51 friends who don't understand the definition of a keyword for each type of storage. Especially when it's in engineering.

When the default storage class is large, and tmp[10] is declared as Uchar tmp[10], such bugs are very secretive and not easily discoverable.

2nd case:

Uchar XData * Data pstr;

PSTR = tmp;

This is not a problem, the use of this method is to refer to the internal Ram Assigning a pointer variable (The role of the data keyword after the "*" number ) , and the pointer itself points to

XData Area ("*" before xdata The function of the keyword ). The compiled assembly code is as follows.

MOV 0x08, #tmp (0x00); 0x08 and 0x09 are pstr pointer variable address spaces allocated within the RAM area

MOV 0x09, #tmp (0x00)

This should be the way to introduce the most efficient external ram in all situations, so please remember him.

3rd Case:

Uchar XData * XData pstr;

Pstr= "TMP";

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

MOV DPTR, #0x000A; 0x000a,0x000b is the PSTR pointer variable address space allocated in the outside RAM area

MOV A, #tmp (0x00)

MOV @DPTR, A

INC DPTR

MOV A, #tmp (0x00)

MOVX @DPTR, A

This approach is typically used in projects where RAM resources are relatively strained and inefficient.

4th Case:

Uchar data * XData pstr;

Pstr= "TMP";

If the reader of the 1th case is found to be similar to the 1th, yes, the same is true with the 1th case, but this time the PSTR is assigned to the outside Ram area. The compiled assembly code is as follows.

MOV DPTR, #0x000A; 0x000A is the address space of the PSTR pointer variable assigned in the outside Ram area

MOV A, #tmp (0x00)

MOVX @DPTR, A

5th case:

UCHAR * Data pstr;

Pstr= "TMP";

You notice that the keyword statement before "*" is gone, yes, so what happens? Here's what it says! By the Chyi of an old song, "Please Follow me", please follow me to see the compiled assembly code, someone asked this is not talking about C51? Why do you want to show us the assembly code? C51 want to use good need to upgrade C51 compile efficiency, see compiled compilation will help you as soon as possible to produce efficient C51 code master. Or look at the code!

MOV 0x08, #0X01; 0x08-0x0a is the address space of the PSTR pointer variable allocated within the RAM area

MOV 0x09, #tmp (0x00)

MOV 0x0A, #tmp (0x00)

Note: This is a new introduction to everyone, you will wonder why in the previous several cases of the PSTR pointer variables are used in 2 byte space and here to use 3 byte space? This is a system-internal processing of KEILC, in which a pointer variable occupies up to 3 bytes of space, and the system compiles code that forces a byte pointer-type resolution to be loaded when a pointer to a storage-space type is not declared. The specific correspondence can be referenced in KEILC's help C51 User's Guide.

6th case:

Uchar * PSTR;

Pstr= "TMP";

This is the most straightforward and simplest pointer variable declaration, but he has the lowest efficiency. Or that sentence, everyone together said! The compiled assembly code is as follows.

MOV DPTR, #0x000A; 0x000a-0x000c is the PSTR pointer variable address space allocated in the outside RAM area

MOV A, #0x01

MOV @DPTR, A

INC DPTR

MOV DPTR, #0x000A

MOV A, #tmp (0x00)

MOV @DPTR, A

INC DPTR

MOV A, #tmp (0x00)

MOVX @DPTR, A

This is similar to the combination of 5th and 3rd cases where the PSTR is allocated outside the RAM space and the resolution of the pointer type is increased.

Keywords in C51 data,idata,xdata,pdata,bdata

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.