assembly - 如何使用 nasm/gcc 将 att 语法 .asm 文件编译为 linux 上的可执行文件

标签 assembly nasm att

我有一些只想编译的 asm 代码。我查看了堆栈上的一些示例,它们总是会产生错误。

我的代码:

.data
hello:
        .string "Hello World!\n"

.text
.globl main
main:
        movl $4, %eax
        movl $1, %ebx
        movl $hello, %ecx
        movl $13, %edx
        int $0x80

        movl $1, %eax
        movl $0, %ebx
        int $0x80

我尝试过的:

nasm -f elf hello.asm

产生的错误:

hello.asm:1: warning: label alone on a line without a colon might be in error [-w+label-orphan]
hello.asm:3: error: parser: instruction expected
hello.asm:5: warning: label alone on a line without a colon might be in error [-w+label-orphan]
hello.asm:6: error: parser: instruction expected
hello.asm:8: error: parser: instruction expected
hello.asm:9: error: parser: instruction expected
hello.asm:10: error: parser: instruction expected
hello.asm:11: error: parser: instruction expected
hello.asm:12: error: expression syntax error
hello.asm:14: error: parser: instruction expected
hello.asm:15: error: parser: instruction expected
hello.asm:16: error: expression syntax error

最佳答案

您的示例是针对 GNU 汇编程序的,而不是针对 NASM 的。要组装它,请在将文件重命名为 hello.s 后键入 cc -m32 -o hello hello.s

NASM 语法中的相同示例是

        section .data
hello:  db      "Hello World!", 10, 0

        section .text
        global  main
main:   mov     eax, 4
        mov     ebx, 1
        mov     ecx, hello
        mov     edx, 13
        int     0x80

        mov     eax, 1
        mov     ebx, 0
        int     0x80

要组装和链接这个 NASM 示例,请键入

nasm -felf32 hello.asm
cc -m32 -o hello hello.o

关于assembly - 如何使用 nasm/gcc 将 att 语法 .asm 文件编译为 linux 上的可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71400093/

相关文章:

assembly - 在汇编中推送并打印浮点值

c - 使用cmpxchg时,c内联程序集出现“操作数大小不匹配”

c++ - 使用 Visual Studio 从 C++ 代码调用程序集过程

c - 如何将此 C 代码转换为 MIPS 汇编代码

assembly - 10 个用户在汇编中输入数字的平均值

assembly - 是否可以在 NASM 中定义浮点常量 (`equ` )?

linux nasm程序集隔离字符串中的字符

c - 如何使用clflush?

linux nasm 程序集显示输入加 1

assembly - 如何在 x86 上使用 AT&T 语法将立即字节添加到长寄存器?