gpt4 book ai didi

assembly - "Hello, World"在 FreeBSD 11.2 上使用 nasm

转载 作者:行者123 更新时间:2023-12-01 13:16:22 25 4
gpt4 key购买 nike

在汇编中,我无法显示文本。这个 asm 代码直接来自一本书(Igor Zhirkov 的 Low Level Programming)。我无法让文本显示在我的 shell 提示符上,但程序组装正常,然后成功与 ld 链接。

global _start

section .data
message: db 'hello, world!', 10

section .text
_start:
mov rax, 1
mov rdi, 1

mov rsi, message
mov rdx, 14
syscall

asm source code, "hello world"

最佳答案

试试这个例子(在 FreeBSD 12 上测试过)

将其保存到 hello.s 中:

section .data

message:
db 'hello, world!', 10

section .text

global _start
_start:
mov rax, 4
mov rdi, 1
mov rsi, message
mov rdx, 14
syscall

mov rax, 1
xor rdi, rdi
syscall

安装nasm:

# pkg install nasm

现在组装它:

$ nasm -f elf64 hello.s

这将生成一个文件 hello.o,您将使用 ld 链接该文件。 :

$ ld -m elf_amd64_fbsd -o hello -s hello.o

这应该创建一个名为 hello 的文件:

$ ./hello
hello, world!

如果你只是尝试:

$ ld -o hello -s hello.o

尝试运行后你可能会得到这个错误:

ELF binary type "0" not known.
./hello: Exec format error. Binary file not executable.

检查这个post (elf_i386_fbsd)还有this answer以供进一步引用。

要修复您粘贴的代码,请替换:

mov rax, 1

mov rax, 4

否则似乎只是退出。

您可以找到这些 syscall /usr/include/sys/syscall.h 中的数字,例如:

/*
* System call numbers.
*
* DO NOT EDIT-- this file is automatically generated.
* $FreeBSD: stable/12/sys/sys/syscall.h 339002 2018-09-28 17:25:28Z jhb $
*/

#define SYS_syscall 0
#define SYS_exit 1
#define SYS_fork 2
#define SYS_read 3
#define SYS_write 4
#define SYS_open 5
#define SYS_close 6
#define SYS_wait4 7
/* 8 is old creat */
#define SYS_link 9
#define SYS_unlink 10
/* 11 is obsolete execv */
#define SYS_chdir 12
#define SYS_fchdir 13
#define SYS_freebsd11_mknod 14
#define SYS_chmod 15
#define SYS_chown 16
#define SYS_break 17

关于assembly - "Hello, World"在 FreeBSD 11.2 上使用 nasm,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54540373/

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