私は Mac での x64 アセンブリにかなり慣れていないので、32 ビット コードを 64 ビットに移植するのに混乱しています。プログラムは、C 標準ライブラリ
の関数を介して単純にメッセージを出力する必要があります。
私はこのコードから始めました:printf
section .data
msg db 'This is a test', 10, 0 ; something stupid here
section .text
global _main
extern _printf
_main:
push rbp
mov rbp, rsp
push msg
call _printf
mov rsp, rbp
pop rbp
ret
このようにnasmでコンパイルします:
$ nasm -f macho64 main.s
次のエラーが返されました:
main.s:12: error: Mach-O 64-bit format does not support 32-bit absolute addresses
コードを次のように変更して、その問題のバイトを修正しようとしました。
section .data
msg db 'This is a test', 10, 0 ; something stupid here
section .text
global _main
extern _printf
_main:
push rbp
mov rbp, rsp
mov rax, msg ; shouldn't rax now contain the address of msg?
push rax ; push the address
call _printf
mov rsp, rbp
pop rbp
ret
上記のコマンドで正常にコンパイルされましたが、オブジェクト ファイルを実際のプログラムにnasm
コンパイルする際に警告が表示されます。gcc
$ gcc main.o
ld: warning: PIE disabled. Absolute addressing (perhaps -mdynamic-no-pic) not
allowed in code signed PIE, but used in _main from main.o. To fix this warning,
don't compile with -mdynamic-no-pic or link with -Wl,-no_pie
エラーではなく警告なので、a.out
ファイルを実行しました:
$ ./a.out
Segmentation fault: 11
私が間違っていることを誰かが知っていることを願っています。