C Language Embedded system programming practice of memory operation

Source: Internet
Author: User

Data pointers

In the programming of embedded system, it is often required to read and write the contents in a specific memory unit, and the corresponding MOV instruction is compiled, while other programming languages other than C + + have no direct access to the absolute address. In the actual debugging of embedded system, the ability to read and write the content of absolute address unit with the help of C language pointer is more. The direct manipulation of memory with pointers occurs in a number of situations as follows:

(1) An I/O chip is positioned in the CPU storage space rather than I/O space, and the register corresponds to a specific address;

(2) Two CPU communication between two-port RAM, the CPU needs in the dual-port RAM specific units (called Mail box) to write content in the other CPU to generate interrupts;

(3) Read Chinese characters and English text models that are burned in a specific unit of ROM or flash.

Such as:

unsigned char *p = (unsigned char *)0xF000FF00;
*p=11;

The meaning of the above program is to write 11 in absolute address 0xf0000+0xff00 (80186 using 16-bit address and 16-bit offset address).

When using an absolute address pointer, be aware that the result of the pointer self-decrement operation depends on the type of data that the pointer points to. The result of the p++ in the previous example is p= 0xf000ff01, if p points to int, that is:

int *p = (int *)0xF000FF00;

The result of p++ (or ++p) is equal to: p = p+sizeof (int), and P = p-sizeof (int).

Similarly, if you perform:

long int *p = (long int *)0xF000FF00;

The result of the p++ (or ++p) is equal to: P = p+sizeof (long int), and P = p-sizeof (long int).

Remember: The CPU is addressed in bytes, while the C language pointer increases and is reduced to the length of the data type to point to. Understanding this is very important for manipulating memory directly with pointers.

function pointers

The first is to understand the following three questions:

(1) The function name in C language corresponds directly to the address of the instruction code generated by the function in memory, so functions name can be directly assigned to the pointer to the function;

(2) The call function is actually equivalent to "switch instruction + parameter transfer processing + return position into stack", essentially the most core operation is to assign the first address of the function generated target code to the CPU's PC register;

(3) because the essence of the function call is to jump to a certain address cell code to execute, so you can "call" a non-existent function entity, Halo? Please look down:

Please come up with any one of the university "Principles of Microcomputer" textbook, the book said, 186 CPU started to jump to the absolute address 0xffff0 (corresponding to the C language pointer is 0xf000fff0,0xf000 for segment address, 0xfff0 offset within the paragraph), please see the following code:

typedef void (*lpFunction) ( ); /* 定义一个无参数、无返回类型的 */
/* 函数指针类型 */
lpFunction lpReset = (lpFunction)0xF000FFF0; /* 定义一个函数指针,指向*/
/* CPU启动后所执行第一条指令的位置 */
lpReset(); /* 调用函数 */

In the above program, we do not see any of the function entities, but we have executed such a function call: Lpreset (), it actually played a "soft reset" effect, jump to the CPU after the start of the first instructions to execute the position.

Remember: The function does not have it, only the instruction sets the ear; you can call a function without the function body, essentially simply changing an address to start the command!

Array vs. dynamic request

In embedded system, dynamic memory request exists more stringent requirements than the general system programming, because the memory space of embedded system is very limited, and the inadvertent memory leak will quickly cause the system to crash.

So make sure your malloc and free pairs appear, if you write a program like this:

char * function(void)
{
  char *p;
  p = (char *)malloc(…);
  if(p==NULL)
   …;
   … /* 一系列针对p的操作 */
  return p;
}
Related Article

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.