High-efficiency embedded program development skills

Source: Internet
Author: User
Tags switch case
High-efficiency embedded program development skills

Abstract: embedded systems have high quality requirements on application software. In embedded development, you must optimize the code to improve code efficiency as much as possible. Although the C compiler provides code optimization to a certain extent, most of the optimization technologies executed by the compiler only involve the balance between execution speed and code size, and the program cannot be fast or small, therefore, necessary measures must be taken during programming. This article provides some programming skills for high-efficiency embedded program development and plays an important role in actual system development.

Ad insertion Information
Latest Hot Chip: ds26ls 32CM CY7C164-15PC qmv233at5 mc33078dr2 g CY7C145-17JC ev1527 YM3433B-F tps76733qd AM29LV160DB-90EI isl6537crz

Introduction

In multimedia, communication and other applications with high computing complexity, in order to meet the requirements of manufacturing costs, power consumption, performance, real-time performance, and many other constraints, embedded system programs often require special design. This requires a set of practical programming principles when designing embedded software for specific applications. In actual programming, engineers need to consider the use of variables and the processing of cyclic programs.

Variable usage

During actual program development, the use of variables is crucial. The use of global variables is more effective than the transfer of parameters to functions. This removes the need for parameter entry and exit during function calling. Of course, using global variables has some side effects on the program. The order of variable definitions may lead to different data la s in the final image, as shown in 1.

Figure 1 chaotic variable image order

It can be seen that when declaring variables, you need to consider how to best control the memory layout. The best way is to put all variables of the same type together for definition during programming.

Generally, engineers try to use short or Char to define variables to save storage space. When the number of local variables of a function is limited, the compiler will allocate local variables to internal registers, each of which occupies one register. In this case, using short and char variables will not only save space, but will lead to other side effects. 2. Assume that A is an arbitrary register that stores the local variables of the function. The addition of 1 is also completed. The 32-bit int type variable is the fastest, and only one addition command is used. The 8-bit and 16-bit variables require symbol extension in 32-bit registers after addition. The signed variables must use two commands, logical left shift and arithmetic right shift, to complete symbol extension. The unsigned variables must use one logic and instruction to clear the symbol bit. Therefore, a 32-bit Int or unsigned int local variable is the most effective. In some cases, a function reads local variables from external memory for calculation. In this case, a variable not 32-bit must be converted to 32-bit. After the 8-bit or 16-bit variables are extended to 32-bit variables, the problem of overflow exceptions may be hidden. further careful consideration is required.

Figure 2 addition programs for different types of local variables

In a program, the switch case statement is often used. Every test and jump implemented by the machine language is only used to decide what to do next, which wastes the processing time. To increase the speed, you can sort the specific situations according to their relative frequency. That is, the most likely case is placed at the end, and the probability of occurrence is small, which reduces the average code execution time.

In general, engineers always try to avoid using redundant variables to streamline the program. In general, this is correct, but there are exceptions, as shown below:

Int F (void );

Int g (void );

File: // F () and g () do not access the global variable errs

Int errs; file: // global variable

Void test1 (void)

{Errs + = f ();

Errs + = g ();

}

Void Test2 (void)

{Int localerrs = errs;

// Define Redundant local variables

Localerrs + = f ();

Localerrs + = g ();

Errs = localerrs;

}

In the first case, in test1 (), each time you access the global variable errs, you must first download the data from the corresponding memory to the register, through F () or G () after the function is called, it is stored in the original memory. In this example, two such download/storage operations are required. In the second case, in Test2 (), the local variable localerrs is assigned as a register, so that the entire function only needs to download/store the global variable memory once. Save storage access times as much as possible, which is very useful for improving system performance.

Processing of cyclic programs

A counting loop is a common process control structure in a program. In C, for loops similar to the following are everywhere:

For (loop = 1; Loop

  • Previous Article: How to Determine CS8900 base address
  • Next article: the realization of GUI interface Culture -- porting Chinese Input Method MurphyPinyin-0.03-src.tgz
  • 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.