简体   繁体   中英

AT&T Assembly Syntax in C program (GCC compiler)?

I have the following program (AT&T Assembly Syntax) that works perfectly on its own if I compile it with GCC compiler on Windows x86:

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

Can this simple program be executed in a C program? I tried with the following:

#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;

}

I get one error:

Error: junk at end of line, first unrecognized character is `8'

This works fine:

__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"
);

Simply C-string-literal-escaped the double quoted string and 's/^/"/;s/$/\\\\n"/' elsewhere.

My gcc's assembler doesn't accept single quoted string literals as in

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

so I don't see why it should start accepting them if you supply them through __asm__ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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