Assembly &引用;“你好,世界”;在FreeBSD 11.2上使用nasm

Assembly &引用;“你好,世界”;在FreeBSD 11.2上使用nasm,assembly,x86-64,freebsd,Assembly,X86 64,Freebsd,在汇编中,我无法获取要显示的文本。此asm代码直接出自一本书(Igor Zhirkov的低级编程)。我无法在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代码直接出自一本书(Igor Zhirkov的低级编程)。我无法在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

尝试一下这个例子(在上测试)

将此保存到
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
您将使用以下链接的文件:

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

$ ./hello
hello, world!
如果您只是尝试:

$ ld -o hello -s hello.o
尝试运行后,可能会出现以下错误:

ELF binary type "0" not known.
./hello: Exec format error. Binary file not executable.
请检查此项,也可供进一步参考

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

mov rax, 1

否则似乎就要退出了

您可以在中找到这些数字,例如:

/*
 * 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

您确定使用了正确的系统调用号吗(即,您是否已在计算机上对照/usr/src/sys/kern/syscalls.master进行了交叉检查)?您至少应该能够在/usr/include/sys/syscall.h中找到系统调用。在Linux上,SYSCALL1(rax=1)是用来写的。在FreeBSD上,您会发现可能需要写入4个,退出系统调用为1
mov rax, 4
/*
 * 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