Thursday 15 September 2011

osx - Illegal instruction: 4 (Mac 64-bit, NASM) -



osx - Illegal instruction: 4 (Mac 64-bit, NASM) -

i'm trying write simple helloworld in assembler 64 on mac nasm. every time seek run i'm getting error:

illegal instruction: 4

here code:

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

i'm compiling with:

nasm -f macho64 helloworld.asm

and i'm linking with:

ld -o helloworld -arch x86_64 -macosx_version_min 10.10 -lsystem -no_pie helloworld.o

any help highly appreciated.

let's start of import thing:

on mac osx, scheme calls preceded 0x2000###, exit 0x2000001.

next, need utilize right registers pass arguments.

the number of syscall has passed in register rax. rdi - used pass 1st argument functions rsi - used pass 2nd argument functions rdx - used pass 3rd argument functions rcx - used pass 4th argument functions r8 - used pass 5th argument functions r9 - used pass 6th argument functions system-call done via syscall instruction. kernel destroys registers rcx , r11.

so bringing together, fixed version of code is:

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

osx assembly nasm x86-64 ld

No comments:

Post a Comment