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

标签 assembly x86-64 freebsd

在汇编中,我无法显示文本。这个 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/

相关文章:

c# - C# 如何在没有中间 C++ 层的情况下直接调用 ml64 dll?

assembly - 未知使用没有大小后缀的指令助记符 (x86_64)

assembly - asm rip 相对寻址

c - 如何设置 dr7 寄存器的值以便在 x86-64 上创建硬件断点?

gcc - 哪个内联汇编代码对于 rdtscp 是正确的?

c - 如何解决此编译器错误

performance - 文件系统通过大量小文件寻求性能

c - K&R 练习 1-8。我可以让我的代码正常工作

winapi - 如何在MASM32中循环播放声音文件

cl.exe 产生奇怪的汇编代码