gcc真的知道如何输出NASM Assembly

Mat*_*gan 3 gcc nasm tasm

所以我有一个简单的C程序循环传递给main的args然后返回:

#include <stdio.h>

int main(int argc, char *argv[])
{
    int i;
    for(i = 0; i < argc; ++i) {
        fprintf(stdout, "%s\n", argv[i]);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我想看看gcc如何用NASM格式写出程序集.我正在查看.asm文件中的输出,并注意到语法是TASM.下面是make文件和gcc的输出.我做错了还是gcc没有输出真正的NASM语法?

all: main

main: main.o
        ld -o main main.o

main.o : main.c
        gcc -S -masm=intel -o main.asm main.c
        nasm -f elf -g -F stabs main.asm -l main.lst
Run Code Online (Sandbox Code Playgroud)

    .file   "main.c"
    .intel_syntax noprefix
    .section    .rodata
.LC0:
    .string "%s\n"
    .text
.globl main
    .type   main, @function
main:
    push    ebp
    mov ebp, esp
    and esp, -16
    sub esp, 32
    mov DWORD PTR [esp+28], 0
    jmp .L2
.L3:
    mov eax, DWORD PTR [esp+28]
    sal eax, 2
    add eax, DWORD PTR [ebp+12]
    mov ecx, DWORD PTR [eax]
    mov edx, OFFSET FLAT:.LC0
    mov eax, DWORD PTR stdout
    mov DWORD PTR [esp+8], ecx
    mov DWORD PTR [esp+4], edx
    mov DWORD PTR [esp], eax
    call    fprintf
    add DWORD PTR [esp+28], 1
.L2:
    mov eax, DWORD PTR [esp+28]
    cmp eax, DWORD PTR [ebp+8]
    jl  .L3
    mov eax, 0
    leave
    ret
    .size   main, .-main
    .ident  "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)"
    .section    .note.GNU-stack,"",@progbits
Run Code Online (Sandbox Code Playgroud)

命令行上的错误是:

[mehoggan@fedora sandbox-print_args]$ make
gcc -S -masm=intel -o main.asm main.c
nasm -f elf -g -F stabs main.asm -l main.lst
main.asm:1: error: attempt to define a local label before any non-local labels
main.asm:1: error: parser: instruction expected
main.asm:2: error: attempt to define a local label before any non-local labels
main.asm:2: error: parser: instruction expected
main.asm:3: error: attempt to define a local label before any non-local labels
main.asm:3: error: parser: instruction expected
main.asm:4: error: attempt to define a local label before any non-local labels
main.asm:5: error: attempt to define a local label before any non-local labels
main.asm:5: error: parser: instruction expected
main.asm:6: error: attempt to define a local label before any non-local labels
main.asm:7: error: attempt to define a local label before any non-local labels
main.asm:7: error: parser: instruction expected
main.asm:8: error: attempt to define a local label before any non-local labels
main.asm:8: error: parser: instruction expected
main.asm:14: error: comma, colon or end of line expected
main.asm:17: error: comma, colon or end of line expected
main.asm:19: error: comma, colon or end of line expected
main.asm:20: error: comma, colon or end of line expected
main.asm:21: error: comma, colon or end of line expected
main.asm:22: error: comma, colon or end of line expected
main.asm:23: error: comma, colon or end of line expected
main.asm:24: error: comma, colon or end of line expected
main.asm:25: error: comma, colon or end of line expected
main.asm:27: error: comma, colon or end of line expected
main.asm:29: error: comma, colon or end of line expected
main.asm:30: error: comma, colon or end of line expected
main.asm:35: error: parser: instruction expected
main.asm:36: error: parser: instruction expected
main.asm:37: error: parser: instruction expected
make: *** [main.o] Error 1
Run Code Online (Sandbox Code Playgroud)

是什么让我相信这是TASM语法是在这个链接上发布的信息:http: //rs1.szif.hu/~tomcat/win32/intro.txt

TASM编码器通常在NASM中存在词汇困难,因为它缺少TASM中广泛使用的"ptr"关键字.

TASM使用这个:

mov al,byte ptr [ds:si]或mov ax,word ptr [ds:si]或mov eax,dword ptr [ds:si]

对于NASM,这只是转换为:

mov al,byte [ds:si]或mov ax,word [ds:si]或mov eax,dword [ds:si]

NASM允许在很多地方使用这些大小的关键字,因此可以以统一的方式对生成的操作码进行大量控制,例如,这些都是有效的:

push dword 123 jmp [ds:word 1234]; 这两个都指定了偏移量jmp [ds:dword 1234]的大小; 对于接口32位和时的棘手代码; 16位段

它可以变得非常毛茸茸,但重要的是要记住,你可以在需要时拥有所需的所有控制.

nin*_*alj 7

Intel语法是指Intel语法,而不是NASM语法.MASM和TASM语法基于英特尔语法,NASM语法从英特尔语法中获得灵感,但却有所不同.

gcc输出实际上是使用英特尔语法进行单独指令的气体语法,(汇编程序指令,标签等使用特定于气体的语法)