首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >gcc真的知道如何输出NASM程序集吗?

gcc真的知道如何输出NASM程序集吗?
EN

Stack Overflow用户
提问于 2011-12-06 20:12:17
回答 1查看 4.5K关注 0票数 3

因此,我有一个简单的C程序,它循环遍历传递给main的args,然后返回:

代码语言:javascript
代码运行次数:0
运行
复制
#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。下面是gcc的制作文件和输出。是我做错了什么,还是gcc没有输出真正的NASM语法?

代码语言:javascript
代码运行次数:0
运行
复制
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

代码语言:javascript
代码运行次数:0
运行
复制
    .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

命令行中的错误如下:

代码语言:javascript
代码运行次数:0
运行
复制
[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

由于

编码缺乏广泛使用的"ptr“关键字,因此它在使用中经常会出现词汇困难。

TASM使用如下:

mov al,字节ptr ds:si或mov ax,word ptr ds:si或mov eax,dword ptr ds:si

对于NASM来说,这只是转化为:

mov al,字节ds:si或mov ax,word ds:si或mov eax,dword ds:si

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

推送dword 123 jmp ds: word 1234;它们都指定偏移量jmp ds: dword 1234的大小;用于连接32位段和;16位段时的复杂代码。

它可能会变得毛茸茸的,但重要的是,当你想要它的时候,你可以拥有你所需要的所有控制。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-06 20:56:37

Intel语法意味着Intel语法,而不是NASM语法。MASM和TASM语法是建立在Intel语法基础上的,NASM语法从Intel语法中得到了启发,但它是不同的。

gcc输出的实际上是使用Intel语法对单个指令使用的气体语法(汇编程序指令、标签等)。使用气体专用语法)

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8406188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档