During mixed programming of C and assembly, there are problems with the variables and function interfaces of C and assembly languages. After the variables defined in the C program are compiled into the. ASM file, they are all put in the. BSS area, and the variable names are prefixed with an underscore. The function defined in the C program is also underlined before the function name after compilation. For example:
Extern int num is changed to. BSS _ num, 1
Extern float Nums [5] is changed to. BSS _ Nums, 5
Extern void func () is changed to _ FUNC,
Mutual calls between a compilation and C can be divided into the following situations:
(1) access the variables and functions in the C program in the assembler.
In the assembler, use _ xx to access the variable XX in C. When accessing an array, you can use _ xx + offset to access it. For example, _ xx + 3 accesses XX [3] in the array.
When the assembler calls the C function, if no parameter is passed, use _ funcname directly. If parameters are passed, the leftmost parameter in the function is provided by register a, and other parameters are provided by the stack in sequence. The return value is returned to register a or the address given by register. At the same time, note that in order to allow the Assembly Language to access the variables and functions defined in the C language, they must be declared as external variables, that is, prefix with extern.
(2) Access to variables in assembler in C program
If you need to access the variables in the assembler in the C program, the variable name in the assembler must be an underscore (_) as the first character and use global to make it a global variable.
If you need to call the process in the assembler in the C program, the process name must be the first character, and, compile the process correctly based on the mode used for C program compilation, stack-based model or register argument model, so that the call parameters can be obtained correctly.
(3) Online Compilation
Insert ASM ("***") directly in the C program and embed Assembly statements. Note that this usage should be used with caution. Online Assembly provides the ability to directly read and write hardware, for example, the read/write interrupt control allows registers, but the compiler does not check and analyze the online assembly language. inserting the online assembly language may cause serious errors when changing the Assembly environment or changing the value of the C variable.
Change in addressing methods in assembly 2 and C interfaces:
Note that in the C language, local variable creation and access 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 streamlined, Ti directly contains the minimum seven characters of the address in the direct addressing mode, the specific location of this low 7-bit addressing can be determined by the DP register or 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. In DP addressing, the DP provides a high 9-bit address and a low 7-bit address to form a 16-bit address. In SP addressing, the 16-bit address is composed of SP (16-bit) and the lower 7 bits are directly added.
In the C language environment, the addressing of local variables must be implemented through the SP register. In hybrid programming, in order to ensure that the Assembly language does not affect the stack register sp, the general method is to use the DP addressing in the assembly environment, so that the two do not interfere with each other. In programming, you only need to pay attention to the correct setting of the CPL bit.
There won't be too many things to learn.