A summary of mixed programming of C and assembly

Publisher:数据梦想Latest update time:2012-07-04 Source: 21ic Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When programming in C and assembly, there are interface issues between variables and functions in C and assembly.
Variables defined in a C program are put into the .bss area after being compiled into an .asm file, and the variable names are preceded by an underscore. Functions defined in a C program are also preceded by an underscore after being compiled. For example:

extern int num becomes .bss _num, 1
extern float nums[5] becomes .bss _nums, 5
extern void func ( ) becomes _func,

The mutual calls between assembly and C can be divided into the following situations:

(1) Accessing variables and functions in a C program in an assembly program.
In an assembly program, you can use _XX to access variable XX in C. When accessing an array, you can use _XX+offset to access it, such as _XX+3 to access XX[3] in the array. When

calling a C function in an assembly program, if no parameters are passed, you can directly use _funcname. If there are parameters to be passed, the leftmost parameter in the function is given by register A, and the other parameters are given in order from the stack. The return value is returned to register A or the address given by register A. Also note that in order for assembly language to access variables and functions defined in C language, they must be declared as external variables, that is, with the extern prefix.

(2) Accessing variables in assembly program

in C program If you need to access variables in assembly program in C program, the variable name in assembly program must start with an underscore and use global to make it a global variable.

If you need to call a procedure in assembly program in C program, the procedure name must start with an underscore, and the procedure must be correctly written according to the mode used when the C program is compiled, whether it is the stack-based model or the register argument model, so that it can correctly obtain the calling parameters.

(3) Online assembly

Directly insert asm("***") into the C program, embed assembly statements. It should be noted that this usage should be used with caution. Online assembly provides the ability to directly read and write hardware, such as reading and writing interrupt control enable registers, but the compiler does not check and analyze online assembly language. Inserting online assembly language to change the assembly environment or possibly change the value of C variables may cause serious errors.

2. Changes in addressing methods in assembly and C interfaces:

It should be noted that in C language, the creation and access of local variables are implemented through the stack, and its addressing is implemented through the stack register SP. In assembly language, in order to make the program code more concise, in the direct addressing mode, the lower 7 bits of the address are directly included in the instruction. The specific location that these lower 7 bits can address can be determined by the DP register or the SP register. The specific implementation can be achieved by setting the CPL bit of the ST1 register. CPL=0, DP addressing, CPL=1, SP addressing. When DP addressing is used, the 9 high bits of the address are provided by DP, and the 16-bit address is formed by adding the 7 low bits to the 16-bit address; when SP addressing is used, the 16-bit address is obtained by directly adding the 16-bit SP (16 bits) to the 7 low bits.

In the C language environment, the addressing of local variables must be implemented through the SP register. In mixed programming, in order to prevent the assembly language from affecting the stack register SP, the usual method is to use DP addressing in the assembly environment, so that the two do not interfere with each other. In programming, just pay attention to the correct setting of the CPL bit.

Reference address:A summary of mixed programming of C and assembly

Previous article:Some experience accumulated when debugging a system using Keil C
Next article:C51 Soft Reset Analysis

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号