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.
Previous article:Some experience accumulated when debugging a system using Keil C
Next article:C51 Soft Reset Analysis
- Popular Resources
- Popular amplifiers
- IC packaging basics and engineering design examples
- Chip Manufacturing: A Practical Tutorial on Semiconductor Process Technology (Sixth Edition)
- RP2040 Assembly Language Programming ARM Cortex-M0+ on the Raspberry Pi Pico
- The C answer book solutions to the exercises in The C programming language, second edition = C Programming Language
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- The generation, measurement and suppression of switching power supply ripples are all covered in one article!
- Circuit reference ground potential issues
- Developers must-have: Sharing common commands of Docker client
- [NXP Rapid IoT Review] Unboxing the Rapid IoT Kit and Understanding the Development Environment
- Recommended China chip + SI522 ultra-low power consumption 13.56M card reader chip, CI522 ultra-low cost 13.56M card reader chip
- In-depth explanation of how to choose the inductor value for DC-DC boost converter
- [Synopsys IP Resources] Using Computational Storage with Processor IP to Optimize Data Center Power and Performance
- I want to make a circuit to transmit audio signals on DC18V. Does anyone have any good circuits to refer to?
- Layering of MCU Software
- LGSR-WIFI AT-free command-free format remote serial port transparent transmission module information