Instantly share code, notes, and snippets.

Embed
What would you like to do?
NASM Hello World for x86 and x86_64 Intel Mac OS X (get yourself an updated nasm with brew)
; /usr/local/bin/nasm -f macho 32.asm && ld -macosx_version_min 10.7.0 -o 32 32.o && ./32
global start
section .text
start:
push dword msg.len
push dword msg
push dword 1
mov eax, 4
sub esp, 4
int 0x80
add esp, 16
push dword 0
mov eax, 1
sub esp, 12
int 0x80
section .data
msg: db "Hello, world!", 10
.len: equ $ - msg
; /usr/local/bin/nasm -f macho64 64.asm && ld -macosx_version_min 10.7.0 -lSystem -o 64 64.o && ./64
global start
section .text
start:
mov rax, 0x2000004 ; write
mov rdi, 1 ; stdout
mov rsi, msg
mov rdx, msg.len
syscall
mov rax, 0x2000001 ; exit
mov rdi, 0
syscall
section .data
msg: db "Hello, world!", 10
.len: equ $ - msg
@bannychen

This comment has been minimized.

bannychen commented May 12, 2014

hi,i run "nasm -f macho64 64.asm" ok,
but run "ld -macosx_version_min 10.7.0 -lSystem -o 64 64.o" result is "-bash: ld: command not found",please help me
what should i do?
many Thanks!

@metacritical

This comment has been minimized.

metacritical commented Sep 14, 2014

You dont have gcc installed just install gcc, actually on mac you need to just install developer tools that come with xcode.

@benwei

This comment has been minimized.

benwei commented Feb 22, 2015

if you already installed xcode, you can use "xcode-select --install" to install command line developer tools. have been tested on osx 10.9.5

@LionelZoc

This comment has been minimized.

LionelZoc commented Mar 19, 2015

hi !, instead of :" mov rsi, msg", use: "lea rsi, [rel msg]"

it should work

@banxi1988

This comment has been minimized.

banxi1988 commented Aug 17, 2015

@LionelZoc Thanks It Works

@dpwell

This comment has been minimized.

dpwell commented Aug 23, 2015

Hi

when I tried nasm -f macho64 64.asm I got
nasm: fatal: unrecognised output format `macho64'
I tried to find "nasm with brew" as suggested above but couldn't find it. (this is on 10.10.5)

thanks.

@deadalnix

This comment has been minimized.

deadalnix commented Aug 28, 2015

@dpwell , if you have a solution, I'm taking.

@iCurlmyster

This comment has been minimized.

iCurlmyster commented Sep 3, 2015

@dpwell nasm should be downloadable through brew. because when i do 'brew search nasm' it comes back with nasm. but if you can't find it through brew just download the newest version from their website because I had the same problem you had and the way I fixed it was just updating nasm to the newest version. But I did mine through brew.

@eulerworks

This comment has been minimized.

eulerworks commented Oct 4, 2015

(change your program name to suit, but this worked on 10.10.4 with nasm from brew)

nasm -fmacho simple.s && ld -macosx_version_min 10.7.0 -o simple simple.o && ./simple

uname -a
14.4.0 Darwin Kernel Version 14.4.0: Thu May 28 11:35:04 PDT 2015; root:xnu-2782.30.5~1/RELEASE_X86_64 x86_64

gcc --version
gcc (GCC) 4.9.2 20141029 (prerelease)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

@adriansr

This comment has been minimized.

adriansr commented Oct 9, 2015

If someone is having the "fatal: No section for index 2 offset 0 found" error even after replacing the mov with a lea instruction, this is a problem of recent (2.11.x) nasm available from brew. Works fine with nasm 2.10 from nasm's website.

@MareoRaft

This comment has been minimized.

MareoRaft commented Dec 19, 2015

I'm on Mavericks 10.9.5. I'm using nasm 2.11.08_1 from brew. I have gcc:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

I got

% nasm -f macho world.asm
world.asm:9: error: symbol `rax' undefined
world.asm:10: error: symbol `rdi' undefined
world.asm:11: error: symbol `rsi' undefined
world.asm:12: error: symbol `rdx' undefined
world.asm:15: error: symbol `rax' undefined
world.asm:16: error: symbol `rdi' undefined

but fixed it by changing the "r"s to "e"s in the file. Then it compiled with

% ld -macosx_version_min 10.7.0 -lSystem -o world world.o

successfully. But when I execute it, I get

% ./world
zsh: illegal hardware instruction  ./world
@D3m0t3p

This comment has been minimized.

D3m0t3p commented Mar 9, 2016

i have the same problem as you @MareoRaft

@Leandros

This comment has been minimized.

Leandros commented Apr 23, 2016

@MareoRaft @D3m0t3p You're using the 64-Bit assembly, but compile it as 32-Bit. It's obvious that this doesn't work.

@ourui

This comment has been minimized.

ourui commented May 26, 2016

@dpwell I install nasm from brew, and the same problem with you. When I type 'nasm -v' , the result is 'NASM version 0.98.40 (Apple Computer, Inc. build 11) compiled on Feb 10 2016' . I doubt that the terminal has use system nasm. So, I close the terminal and restart, and it works. Now nasm can recognised 'macho64'. Type 'nasm -v' again, the result is 'NASM version 2.12.01 compiled on Mar 23 2016'.

@tilarids

This comment has been minimized.

tilarids commented Sep 23, 2016

I've done a 32-bit version that works on 10.11.5: https://gist.github.com/tilarids/3fe6d8499ad25b722c307cfcfb817dc2
It seems padding and symbol table are now necessary.

@lwickstr

This comment has been minimized.

lwickstr commented Oct 23, 2016

for those having trouble with the "install nasm from brew" directions above, here is a guide to install brew:
http://www.howtogeek.com/211541/homebrew-for-os-x-easily-installs-desktop-apps-and-terminal-utilities/
(i only needed the one line command:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
)
then running:
nasm install brew
will give you nasm version 2.12+

@shoaibmohi

This comment has been minimized.

shoaibmohi commented May 24, 2017

How to run NASM code in Xcode on MacOS Sierra.
Please Help.

@sguillia

This comment has been minimized.

sguillia commented Sep 10, 2017

When I run the program, the return value is 1, but if I change sub esp, 12 to sub esp, 4 as it was in old revisions, then it works, return value 0. Why does it happen like that ? On OS X Sierra 10.12.6

@kwccoin

This comment has been minimized.

kwccoin commented Oct 15, 2017

I also need to do :

push   dword 0
mov    eax,  1  ; 0 ; should not be 1
sub    esp,  4  ; 12 ; not sure about this and note there is a push dword 0 here
int    0x80

to work under OS X Sierra 10.12.6.


Also, the first few line is mysterious

section .text
start:
push    dword msg.len ;4 bytes
push    dword msg     ;4 bytes 
push    dword 1       ;4 bytes. and there is push dword 1 here
mov     eax, 4
sub     esp, 4        ;reserves 4 bytes more 
int     0x80
add     esp, 16       ;restore esp to original value

; usually should be 
; push ebp
; mov  ebp, esp
; push ??? the 4 bytes not sure what that for
; ...
; pop  ??? the 4 bytes
; pop  ebp

May be c calling standard, Win32 calling standard or ...

@HGarron

This comment has been minimized.

HGarron commented Dec 8, 2017

FiloSottile:

Thank you very much. I am trying to learn assembly language on a Mac.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment