[英]How to call a C function from Assembly?
Here's the entire assembly program: 这是整个汇编程序:
.model small
.stack 256
.code
start:
call printer
mov ax, 3 ; store 3 into ax
mov ah, 76 ; back to DOS
mov al, 0 ; no errors
int 21h ; interupt -> DOS
end start
And this is where I define the C
function printer
这是我定义
C
函数printer
#include <stdio.h>
void printer()
{
printf("Hello!\n");
}
When compiling the assembly code, I get an error: undefined symbol: printer
. 编译汇编代码时,出现错误:
undefined symbol: printer
。 In C
I would do an #include "file.h"
, how do I achieve the same result here? 在
C
我将执行#include "file.h"
,如何在此获得相同的结果?
The problem is not compilation, it's linking. 问题不是编译,而是链接。 You must link (using
ld
if on Unix/Linux) your executable including the object file with the assembly code and the object file with the C code. 您必须链接(如果使用Unix / Linux,则使用
ld
)可执行文件,包括带有汇编代码的目标文件和带有C代码的目标文件。
Or put your assembly code into your C file using an "asm"
block. 或者使用
"asm"
块将汇编代码放入C文件中。
You should add something like 您应该添加类似
extern _printer
on the top of your assembly and use call with this name 在程序集的顶部,并使用具有此名称的调用
call _printer
Correct name of function depends on naming convention of your C compiler. 函数的正确名称取决于C编译器的命名约定。 Compiler may add some characters to the C name of the function.
编译器可能会在函数的C名称中添加一些字符。
Correct "extern" keyword depends on your assembler and it could be ".extern" or so. 正确的“ extern”关键字取决于您的汇编程序,并且可能是“ .extern”左右。
Edit 1: In Turbo Assembler and for that case with function without parameter, it should be just 编辑1:在Turbo汇编程序中,对于这种情况,如果没有参数,函数应该只是
extrn printer
or 要么
extrn printer:NEAR
I am not familiar with TASM. 我对TASM不熟悉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.