3

したがって、mainに渡された引数をループしてから返す単純なCプログラムがあります。

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

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

    .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

コマンドラインのエラーは次のとおりです。

[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

これが TASM 構文であると信じるようになったのは、次のリンクに投稿された情報でした: http://rs1.szif.hu/~tomcat/win32/intro.txt

TASM コーダーは、TASM で広く使用されている「ptr」キーワードがないため、通常、NASM で字句の問題を抱えています。

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ビットセグメント

かなり複雑になる可能性がありますが、覚えておくべき重要なことは、必要なときに必要なすべての制御を行うことができるということです。

4

1 に答える 1

7

Intel構文とは、NASM構文ではなく、Intel構文を意味します。MASMおよびTASM構文はIntel構文に基づいており、NASM構文はIntel構文からインスピレーションを得ていますが、それは異なります。

gccが出力するのは、実際には個々の命令にIntel構文を使用したガス構文です(アセンブラーディレクティブ、ラベルなどはガス固有の構文を使用します)

于 2011-12-06T20:56:37.770 に答える