2

如何在不包括 stdio.h 的情况下直接调用“printf”?

我在这里找到了一个有趣的教程:
http ://www.halcode.com/archives/2008/05/11/hello-world-c-and-gnu-as/

所以,这是我的尝试:

int main(){
 char ss[] = "hello";

 asm (
  "pushl %ebp ;"
  "movl %esp, %ebp ;"
  "subl $4, %esp ;"
  "movl $ss, (%esp) ;"
  "call _printf ;"
  "movl  $0, %eax ;"
  "leave ;"
  "ret ;"
 );

 return 0;
}

我正在使用 MinGW 4.4,这是我的编译方式:

gcc -c hello.c -o hello.o
ld hello.o -o hello.exe C:/mingw/lib/crt2.o C:/mingw/lib/gcc/mingw32/4.4.0/crtbegin.o C: /mingw/lib/gcc/mingw32/4.4.0/crtend.o -LC:/mingw/lib/gcc/mingw32/4.4.0 -LC:/mingw/lib -lmingw32 -lgcc -lmsvcrt -lkernel32

不幸的是,它失败了:

hello.o:hello.c:(.text+0x26): 未定义对 `ss' 的引用

如何解决这个问题?

4

4 回答 4

9

您可以将声明复制printf到您的程序中。在 C 中,包含其他文件只是将其文本复制粘贴到您的程序中。因此,您可以通过自己进行复制粘贴来完成这项工作。

extern int printf (const char* format, ...);

int main()
{
  printf("Hello, world!\n");
  return 0;
}

链接器肯定会在库中找到正确的定义,您的程序默认链接到该定义。

于 2009-09-27T09:57:25.060 回答
4
int main(void)
{
    char ss[] = "hello";

    __asm(
        "pushl %[ss]\n"    // push args to stack
        "call _puts\n"
        "addl $4, %%esp\n" // remove args from stack
        :: [ss] "r" (ss)   // no output args, no clobbered registers
    );

    /*  C equivalent:
        extern int puts(const char *);
        puts(ss);
    */
}
于 2009-09-27T13:09:13.907 回答
2
int main() {

    char ss[] = "hello";
    char *p = ss;

    asm (
        "movl %0, (%%esp);"
        "call _printf;" : "=r" (p)
    );

    return 0;
}
于 2009-09-27T10:34:03.270 回答
-4

它非常容易使用 printf 语句编写一个 c 程序,然后使用 .c 扩展名保存程序并运行程序它会工作....甚至它可以包含像 clrscr()、getch() 这样的函数,它们是 conio.h 的一部分

于 2010-10-28T10:43:17.280 回答