Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile using nasm on MacOSX

Tags:

assembly

nasm

I am trying to compile and link my first program on Assembler. I try to compile the following code:

; %include "stud_io.inc"    
global _main     

section .text
_main: 
    xor eax, eax
again:
    ; PRINT "Hello"
    ; PUTCHAR 10
    inc eax     
    cmp eax, 5
    jl again

Below the console command for compiling and linking a program:

-bash-3.2$ nasm -f macho main.asm -o main.o  && ld -e _main -macosx_version_min 10.8 -arch x86_64 main.o

But the result is:

ld: warning: ignoring file main.o, file was built for i386 which is not the architecture being linked (x86_64): main.o
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     -u command line option
ld: symbol(s) not found for architecture x86_64

I think its necessary to compile the main.asm file for x86_64.. How to compile programs for my system properly?

like image 233
Ilya Lavrenov Avatar asked Dec 31 '12 16:12

Ilya Lavrenov


People also ask

Can you compile C on Mac?

Various compilers are available in Mac to compile C code. Mac C compilers compile C code into an executable. This executable can be run directly to run the C code. Clang and GCC(GNU Compiler Collection) are the common compilers that are used to compile C code.


1 Answers

I would recommend first updating your NASM.

After that, try running this:

nasm -f macho64 main.asm -o main.o  && ld -e _main -macosx_version_min 10.8 -arch x86_64 main.o -lSystem

Notice that the new command adds JasonD's suggestion above (macho64), but also adds the -lSystem to the ld command to stop ld from throwing following error:

ld: dynamic main executables must link with libSystem.dylib for architecture x86_64
like image 143
RageD Avatar answered Oct 14 '22 09:10

RageD