Some use experiences of the Keil C51 compiler (1)

zhaozj2021-02-16  67

I have just participated in the work, I have been using the Keil compiler for more than half a year. In these half years, I summed up some of the experience of Keil's use, I hope to share with you, and ask the high-end people to dial, because I am not Computer professional, so there is no contact with a high-profile course such as compilation and operating system, always believe that some of the laws mentioned below have theoretical basis, hoping to advice. The current memory is not as expensive as seven or eight years ago, but the price of RAM relative to ROM and EEPROM is still not as viewed, so there is still a crucial in the program. The following points: 1. The access speed of the algae is much faster relative to the EEPROM's access speed, not in a quantitude, mainly because EEPROM storage must be erased, and EEPROM's wipe The need to be erased (this is because the principle of EEPROM is the gate electrical erase of the field effect tube. In order to save cost manufacturers, it is generally 8bytes / page 64bytes / page), so use RAM to process the middle Data is capable of conforming to speed requirements. 2. Whether it is XRAM or EEPROM, it is an external memory. When the negative value is used, the 16bit address space (8-bit machine) is used, so that the size of the program's Code is increased and the speed is also affected, so It is very meaningful to use the RAM of the Indata area.

I summarize some rules that save memory, and provide you with a discussion to see if it is feasible.

1. Basic principle of memory allocation: Keil and other C language compiler I think it is basically the same from the principle of memory allocation. Summary, it is actually very simple, it is the longest path to compile (political bruises), such as the following two programs Program 1: unsigned char A (); void b (); void main () {unsigned char Byte1; unsigned char BYTE2;

BYTE1 = byte2 = 3;

IF (a () == 3) {b ();}

A ();

Return;}

// a functionunsigned char A () {unsigned char BYTE_A1; UNSIGNED CHAR BYTE_A2;

BYTE_A1 = BYTE_A2 = 3;

BYTE_A1 = 4; BYTE_A2 = 5;

Return Byte_a1;

// b Functionvoid b () {unsigned char BYTE_B1; Unsigned char BYTE_B2; UNSIGNED Char Byte_b3;

BYTE_B1 = BYTE_B2 = byte_b3 = 3;

Return;}

Program 2: void a (); void b (); void main () {unsigned char BYTE1; unsigned char Byte2;

BYTE1 = byte2 = 3;

a ();

Return;}

// a functionvoid a () {unsigned char BYTE_A1; UNSIGNED Char Byte_a2;

BYTE_A1 = BYTE_A2 = 3;

IF (Byte_a1 == 3) {b ();

BYTE_A1 = 4; BYTE_A2 = 5;

Return;}

// b Functionvoid b () {unsigned char BYTE_B1; Unsigned char BYTE_B2; UNSIGNED Char Byte_b3;

BYTE_B1 = BYTE_B2 = byte_b3 = 3;

RETURN;} The role of the two sections is the same, and the function A is first executed, and then determines the B program according to the value of byte_a1, but the result of compiling KEIL is not the same Program 1 compiled the result is DATA: 14 CODE: 48, and the result of Program 2 is Data: 16 Code: 56, which can be seen in PROGRAM 2 saves CODE and saves memory again than PROGRAM 2.

Look at the disassembly code, you can understand the reason. Call the B function in the A function, the BYTE_A1 and BYTE_A2 variable defined by the A function are not released, so the memory allocation of Program 2 is 8 (SFR) 1 (stack) 2 (MAIN FUNC) 3 (B Func) = 16 BYTES, and the memory allocation of Program 1 is 8 (SFR) 1 (STACK) 3 (B Func) = 14bytes, due to B function and A The function is parallel, so 2 bytes required for A function are saved.

This summary seems that the program should not serial, and should be as parallel to make full use of limited RAM resources, which can make the Code zone becomes smaller or the speed can be made fast.

2.uncalled segment affects memory allocation: I don't know if you have found that the memory space is likely to overflow when there is a function that there is no call. This reason is actually very simple, for example: Program 3: Void A (); void b () ; void c (unsigned char BYTE_INPUT); void main () {unsigned char Byte1; unsigned char BYTE2;

BYTE1 = byte2 = 3;

a ();

C (3);

Return;}

// a functionvoid a () {unsigned char BYTE_A1; UNSIGNED Char Byte_a2;

BYTE_A1 = BYTE_A2 = 3;

IF (Byte_a1 == 3) {b ();

BYTE_A1 = 4; BYTE_A2 = 5;

Return;}

// b Functionvoid b () {unsigned char BYTE_B1; Unsigned char BYTE_B2; UNSIGNED Char Byte_b3;

BYTE_B1 = BYTE_B2 = byte_b3 = 3;

Return;}

Void C (unsigned char BYTE_INPUT) {UNSIGNED Char byte_c;

BYTE_C = byte_input;

Return;}

PROGRAM 3 is shown if c (3) compiled DATA: 16 after main.c, and if C (3) is not adjusted, DATA: 17 after compiling DATA: 17, because C (3) DATA = 8 (SFR) 1 (main func) 2 (a FUNC) 3 (B Func) = 16bytes, and if c (3) DATA = 8 (SFR) 1 (main func) is not adjusted 2 (a FUNC) 3 (B Func) 1 (c func) = 17 bytes. So, I suggest that if you temporarily don't call the function to be shielded, avoid affecting the overall memory allocation.

This time, I will write here, I hope everyone will discuss with me. Next time I want to discuss with you about Data_Group on Keil.

转载请注明原文地址:https://www.9cbs.com/read-22324.html

New Post(0)