A summary of mixed programming between C and assembler

Source: Internet
Author: User
Tags mixed

In C and assembly mixed programming, there is the C language and assembly language variables and function interface problems.

Variables defined in the C program, compiled to an. asm file, are placed in the. BSS area, and the variable name is preceded by an underscore. The function defined in the C program is compiled with an underscore in front of the function name. For example:

extern int num就会变成 .bss _num, 1
extern float nums[5]就会变成.bss _nums, 5
extern void func ( )就会变成 _func,

The mutual invocation of a compilation and C can be divided into the following situations:

(1) Access to the variables and functions in the C program in an assembler.

In the assembler, you can access the variable xx in C with _xx. When you access an array, you can access it with a _xx+ offset, such as _xx+3 accessing Xx[3 in the array.

When the assembler calls the C function, if there is no parameter passing, use _funcname directly. If a parameter is passed, the leftmost parameter in the function is given by register a, and the other parameters are given sequentially by the stack. The return value is the address returned to a register or given by a register. Also note that in order to enable assembly language to access the variables and functions defined in the C language, they must be declared as external variables, plus an extern prefix.

(2) Accessing variables in assembler in C program

If you need to access a variable in the assembler in a C program, the variable name in the assembler must be underlined as the leading character and the global variable with global.

If you need to call a procedure in an assembler in a C program, the procedure name must be an underscore-headed character, and the procedure should be correctly written based on whether the pattern used by the C program is compiled Stack-basedmodel or Registerargumentmodel. So that it can get the calling parameters correctly.

(3) Online assembly

In the C program directly into the ASM ("* *"), embedded assembly statements, you need to be careful with this use, online assembly provides the ability to directly read and write hardware, such as read and write interrupt control allowed registers, but the compiler does not check and analyze online assembly language, Inserting an online assembly language changing the assembly environment or possibly changing the value of a C variable can cause serious errors.

Change of addressing mode in the second assembly and C interface:

It should be noted that in the C language, the establishment and access of local variables is done through the stack, and its addressing is achieved through the stack register SP. In assembly language, in order to make the program code become more concise, ti in the direct addressing mode, the address of the low 7-bit directly included in the instruction, the lower 7-bit can address the specific location can be determined by the DP register or SP register. The implementation can be achieved by setting the CPL bit of the ST1 register, CPL=0,DP addressing, cpl=1,sp addressing. In DP addressing, the DP provides a high 9-bit address and a 16-bit address with a low 7-bit, and 16-bit addresses are added directly from the SP (16-bit) to the lower 7 digits when the SP is addressed.

Because in the C language environment, the address of local variables must be implemented through the SP registers, in mixed programming, in order to make assembly language does not affect the stack register SP, the usual way is in the assembly environment using DP method, so that they can not interfere with each other. In programming, just pay attention to the CPL bit correctly set.

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.