gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:33:29 25 4
gpt4 key购买 nike

我有一些只想编译的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com