gpt4 book ai didi

macos - 非法指令 : 4 (Mac 64-bit, NASM)

转载 作者:行者123 更新时间:2023-12-01 09:23:19 25 4
gpt4 key购买 nike

我正在尝试使用 NASM 在 Mac 上的汇编程序 64 中编写一个简单的 helloworld。
每次我尝试运行它时,我都会收到此错误:

Illegal instruction: 4

这是我的代码:
section .text
global _main

_main:
mov rax, 4
mov rbx, 1
mov rcx, tekst
mov rdx, dlugosc
int 80h

mov rax, 1
int 80h

section .data

tekst db "Hello, world", 0ah
dlugosc equ $ - tekst

我正在编译:
nasm -f macho64 HelloWorld.asm

我正在链接:
ld -o HelloWorld -arch x86_64 -macosx_version_min 10.10 -lSystem -no_pie HelloWorld.o

任何帮助都受到高度赞赏。

最佳答案

让我们从最重要的事情开始:

在 Mac OSX 上,系统调用以 0x2000### 开头,因此退出时它将是 0x2000001。

接下来,我们需要使用正确的寄存器来传递参数。

The number of the syscall has to be passed in register rax.

rdi - used to pass 1st argument to functions
rsi - used to pass 2nd argument to functions
rdx - used to pass 3rd argument to functions
rcx - used to pass 4th argument to functions
r8 - used to pass 5th argument to functions
r9 - used to pass 6th argument to functions

A system-call is done via the syscall instruction. The kernel destroys registers rcx and r11.

因此,将这些放在一起,您的代码的固定版本是:
section .text
global _main

_main:
mov rax, 0x2000004
mov rdi, 1
mov rsi, tekst
mov rdx, dlugosc
syscall

mov rax, 0x2000001
syscall

section .data

tekst db "Hello, world", 0ah
dlugosc equ $ - tekst

关于macos - 非法指令 : 4 (Mac 64-bit, NASM),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26893731/

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