Author Topic: Mac x86-64 bit samples  (Read 8686 times)


  • Guest
Mac x86-64 bit samples
« on: July 22, 2009, 12:25:20 PM »
Here is something I finally for working.  I hope it might be of some help to someone else out there.

Calling libc functions, _puts and _printf, and the syscall function syswrite from OS X with x84-64 bit code.

I used nasm 2.07 and gcc 4.2.1

Next stop, calling Carbon for UI control.  Oh yes !


Code: [Select]
;nasm -f macho64 hello_3.asm
;gcc -m64 -mmacosx-version-min=10.4 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -o hello_3 hello_3.o
;gcc -m64 -mmacosx-version-min=10.5 -isysroot /Developer/SDKs/MacOSX10.5.sdk -o hello_3 hello_3.o

hello db 'syscall: hello, world', 10,0
hellolen equ $ - hello
hello2 db 'puts: hello, world', 10,0
hellolen2 equ $ - hello2
hello3 db 'printf: hello, world %d', 10,0
hellolen3 equ $ - hello3

GLOBAL _main
extern _printf, _puts

align 4, db 0x90

push  qword rbp ; build stack frame
mov rbp, rsp

; lea rdi, [hello3 wrt rip] ; lea works with yasm but not nasm
mov rdi, qword hello3 ; pointer to string to be displayed
xor eax, eax ; there are 0 float values being passed
mov rsi, 42 ; rsi is the first variable to display.  Just giving it a number
call _printf

;puts test
; lea rdi,  [hello2 wrt rip] ; lea works with yasm but not nasm
mov rdi, qword hello2 ; string to be displayed
call _puts

; syscall write to device test
   mov rax, 0x2000004    ; sys_write
   mov rdi, 1            ; stdout
   mov rsi, qword hello  ; string
   mov rdx, hellolen     ; length

xor eax,eax ; set return value to 0
  leave ; unwind our stack frame

Offline ras

  • Jr. Member
  • *
  • Posts: 8
Re: Mac x86-64 bit samples
« Reply #1 on: February 24, 2010, 10:10:06 PM »
i thank you kind sir <3
[this is stuff i've been wondering about myself]