简体   繁体   English

如何从程序集中调用C函数

[英]How to call a C function from assembly

I am programming x86 assembly in Visual Studio 2012. 我在Visual Studio 2012中编写x86程序集。

I'm trying to call printf from an assembly program, but it throws an error as soon as I include the EXTERN line. 我试图从汇编程序中调用printf,但是只要包含EXTERN行就会抛出错误。 It's probably something really stupid like including the library of C functions but I can't find any online resource that tells me how to do it right. 它可能是非常愚蠢的东西,比如包含C函数库,但我找不到任何可以告诉我如何正确执行它的在线资源。

Here's my code: 这是我的代码:

.model flat

            EXTERN  _printf

.data

digits      byte    "0" dup (10), 0         

.code

main proc

            lea     eax,    digits
            push    eax
            call    _printf
            add     esp,    4

main endp

END

And the error thrown is: 抛出的错误是:

error A2008: syntax error : in directive

on line 3 (the line with EXTERN) 在第3行(与EXTERN的行)

EDIT: Added information about environment (the first line). 编辑:添加了有关环境的信息(第一行)。

As commented, use 如评论,使用

        extern  _printf:proc

or if 32 bit mode, you may need to use: 或者如果是32位模式,您可能需要使用:

        extern  _printf:near

externs may need to be placed in the .code section. externs可能需要放在.code部分。

Also add these two lines near the top of your source code to cause the linker to look in these libraries. 还要在源代码顶部附近添加这两行,以使链接器查看这些库。

        includelib   msvcrtd
        includelib   oldnames

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM