如果使用Windows x86上的GCC编译器进行编译,我有以下程序(AT&T汇编语法)可以完美地运行:
LC0:
.ascii "Hello, world!\0"
.globl _main
_main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
call ___main
movl $LC0, (%esp)
call _puts
movl $0, %eax
leave
ret
这个简单程序可以在C程序中执行吗?我尝试了以下方法:
#include <stdlib.h>
int main()
{
__asm__ ("LC0:\n\t"
".ascii 'Welcome Message\0'\n\t"
"LC1:\n\t"
".ascii 'Hello\0'\n\t"
"_main:\n\t"
"LFB11:\n\t"
"leal 4(%esp), %ecx\n\t"
"andl $-16, %esp\n\t"
"pushl -4(%ecx)\n\t"
"pushl %ebp\n\t"
"movl %esp, %ebp\n\t"
"pushl %ecx\n\t"
"subl $20, %esp\n\t"
"call ___main\n\t"
"movl $1, 12(%esp)\n\t"
"movl $LC0, 8(%esp)\n\t"
"movl $LC1, 4(%esp)\n\t"
"movl $0, (%esp)\n\t"
"call _MessageBoxA@16\n\t"
"subl $16, %esp\n\t"
"movl $0, %eax\n\t"
"movl -4(%ebp), %ecx\n\t"
"leave\n\t"
"leal -4(%ecx), %esp\n\t"
"ret\n\t");
return 0;
}
我遇到一个错误:
错误:行尾有垃圾,第一个无法识别的字符是'8'
答案 0 :(得分:4)
这很好:
__asm__(
"LC0:\n"
" .ascii \"Hello, world!\\0\"\n"
".globl _main\n"
"_main:\n"
" pushl %ebp\n"
" movl %esp, %ebp\n"
" andl $-16, %esp\n"
" subl $16, %esp\n"
" call ___main\n"
" movl $LC0, (%esp)\n"
" call _puts\n"
" movl $0, %eax\n"
" leave\n"
" ret\n"
);
仅用C字符串直义转义了双引号字符串和其他地方的's/^/"/;s/$/\\n"/'
。
我的gcc的汇编器不接受单引号中的字符串文字,
LC0:
.ascii 'Hello, world!\0'
.globl _main
_main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
call ___main
movl $LC0, (%esp)
call _puts
movl $0, %eax
leave
ret
因此,如果您通过__asm__
提供它们,我不明白为什么它应该开始接受它们。