Assembly language in os x

I used the assembly language step by step to learn assembly language programming on Linux. I recently got a Mac on which int 0x80 doesn't seem to work (illegal instruction).

So I just wanted to know if there is a good link (book / web page) that gives differences in the standard unix build and the darwin build.

+6
source share
3 answers

For practical purposes, this answer shows how to compile the welcome application of the world using nasm on OSX .

This code can be compiled for linux as it is, but the cmd line command to compile it will probably be different:

 section .text global mystart ; make the main function externally visible mystart: ; 1 print "hello, world" ; 1a prepare the arguments for the system call to write push dword mylen ; message length push dword mymsg ; message to write push dword 1 ; file descriptor value ; 1b make the system call to write mov eax, 0x4 ; system call number for write sub esp, 4 ; OS X (and BSD) system calls needs "extra space" on stack int 0x80 ; make the actual system call ; 1c clean up the stack add esp, 16 ; 3 args * 4 bytes/arg + 4 bytes extra space = 16 bytes ; 2 exit the program ; 2a prepare the argument for the sys call to exit push dword 0 ; exit status returned to the operating system ; 2b make the call to sys call to exit mov eax, 0x1 ; system call number for exit sub esp, 4 ; OS X (and BSD) system calls needs "extra space" on stack int 0x80 ; make the system call ; 2c no need to clean up the stack because no code here would executed: already exited section .data mymsg db "hello, world", 0xa ; string with a carriage-return mylen equ $-mymsg ; string length in bytes 

Assemble the source (hello.nasm) into an object file:

 nasm -f macho hello.nasm 

Link to create an executable file:

 ld -o hello -e mystart hello.o 
+3
source

This question will probably help: List and documentation for system calls for the XNU kernel in OSX .

Unfortunately, there seems to be only one way to find out in the book mentioned. As for int 0x80, I doubt it will work, because it is a pretty Linux-specific API that is built right into the kernel.

The tradeoff I make when working with an unfamiliar OS is to simply use libc calls, but I can understand that even this can be too high if you just want to learn.

+1
source

can you post your code and how did you compile? (There are many ways to detect errors related to errors)

OSX chose the bsd style for passing arguments, so you should do something a little differently.

I bookmarked this earlier: http://www.freebsd.org/doc/en/books/developers-handbook/book.html#X86-SYSTEM-CALLS

0
source

Source: https://habr.com/ru/post/894602/


All Articles