我想在我的 macOS Monterey 12.1 上學習 NASM x86 匯編語言。
我的代碼如下:
SECTION .data
msg db 'Hello World!', 0Ah ; assign msg variable with your message string
SECTION .text
global _start
_start:
mov edx, 13 ; number of bytes to write - one for each letter plus 0Ah (line feed character)
mov ecx, msg ; move the memory address of our message string into ecx
mov ebx, 1 ; write to the STDOUT file
mov eax, 4 ; invoke SYS_WRITE (kernel opcode 4)
int 80h
我輸入命令后:
nasm -f macho64 -o helloworld.o helloworld.asm
我會得到:
helloworld.asm:15: error: Mach-O 64-bit format does not support 32-bit absolute addresses
有什么解決方案可以解決嗎?
uj5u.com熱心網友回復:
用于的尋址空間macho64是x86-64,因此暫存器的大小不同,可以處理 64 位尋址。例如,ECX 是 32 位,您嘗試使用 64 位地址加載它,導致錯誤。
這是 x86-64 的 Hello World 代碼的樣子:
global _main
SECTION .data
msg db 'Hello World!', 0x0A ; assign msg variable
SECTION .text
_main:
mov rdx, 13 ; number of bytes to write - one for each letter plus LF char.
lea rsi, [rel msg] ; move the memory address of our message string into rsi
mov rdi, 1 ; write to the STDOUT file
mov rax, 0x2000004 ; invoke SYS_WRITE (kernel opcode 4)
syscall
mov rax, 0x2000001 ; exit
mov rdi, 0
syscall
編譯、鏈接、執行:
% nasm -f macho64 -o helloworld.o helloworld.asm
% ld helloworld.o -o hello -lSystem -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib
% ./hello
Hello World!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/405173.html
標籤:
