“你好,世界”在FreeBSD 11.2使用NASM

问题描述 投票:3回答:1

在装配时,我不能要显示的文字。这汇编代码是直接出了一本书(由Igor日尔科夫底层编程)的。我不能要显示在我的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"

assembly x86-64 freebsd
1个回答
3
投票

试用一下这个例子(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

现在assembleit有:

$ 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
© www.soinside.com 2019 - 2024. All rights reserved.