How to Install FASM in Mac OSX

Lots of discussions have been tackled  as to the issues encountered in running fasm in Mac OS X. The following threads in FASM forum detail these discussions:

Out of these discussions, some have posted their successes on installing fasm their own way on github:

Basically, these are the sources of our knowledge on how to install fasm on Mac OS X (Sierra)

How-To:

  1. Go to https://github.com/JohnDDuncanIII/fasm-osx.git and click on the “Clone or download” button to download the package.
  2. After downloading, open a terminal and execute the following commands:
    $ sudo –s                           # we need to be root to chmod the file
    # cd /Users/<your_username>/Downloads/fasm-osx-master   # go to the download folder
    # chmod ugo+x fasm                  # ensure that fasm is executable
    # chmod ugo+x objconv               # ensure that objconv is executable
    # mv –f fasm /usr/local/bin         # put it in the directory included in the path
    # mv –f objconv /usr/local/bin      # put it in the directory included in the path
  3. Execute fasm, the following should be the output:
    mac_fasm
  4. Execute objconv, the following should be the output:
    mac_objconv
  5. The real test that it’s working is to use it to compile a program. We shall compile the two test programs from the package we downloaded: hello32.asm and hello64.asm. To generate an executable in OS X, we would need to use the following tools other than fasm:
    objconv Used for converting object files between COFF/PE, OMF, ELF and Mach-O formats for all 32-bit and 64-bit x86 platforms. We’ll use this to convert the object file generated by fasm into Mach-O format that can be linked using ld.
    ld GNU Linker. We need this to link the object file created by objconv to make an executable. Fasm in itself cannot generate executable in OS X.
    file We shall let Mac perform a test and classification the file accordingly.

     

  6. While you are still in the dowload directory, perform the following highlighted commands:
    pre-compilation
  7. Compile hello32.asm
    $ fasm hello32.asm hello32.o
    fasm_compile
  8. The generated hello32.o must be converted to Mach-O format before we can successfully link it.
    $ objconv –fmacho32 -nu hello32.o hello32_m.o
    The –nu option specifies to change the symbol Name Underscores to the default for the target file format. After objconv successfully converted hello32.o, we would have a new file called hello32_m.o — this is the converted version.
    objconv
  9. Then we use ld to link hello32_m.o to make an executable file that we’ll call hello32.
    $ ld -arch i386 -macosx_version_min 10.6 -o hello32 hello32_m.o /usr/lib/crt1.o /usr/lib/libc.dylib
    The –arch option specifies the architecture (e.g., ppc, ppc64, i386, x86_64) of the output file. Since we are compiling a program written for 32-bit, we use i386. The –macosx_version_min option specifies the olsdest OS X version that the output is to be used on, we indicate at least 10.6; the oldest supported version so far is 10.4. We use –o to specify the output file: hello32, otherwise the output file would be something like “a.out”. We also specify in this command that we want to link hello32 with hello32_m.o, crt1.o, and libc.dylib.If during linking you encounter an error like “ld: file not found: /usr/lib/crt1.o,” it means you first need to install the “Command Line Developer Tools for Mac OS X.” You cannot proceed with the linking unless this is resolved. So back to terminal, type in the following command (make sure you are connected to the Internet):
    $ xcode-select –install
    Execute ld and it should not give any output message if it’s successful. The result would be an executable file called hello32.
    ld32
  10. Run the file command to let Mac OS X perform the validation and proper classfication of hello32 for us. After it successfully classified the file as executable, we can now execute it from the terminal.
    hello32
  11. Compile the hello64.asm (64-bit version). For 64-bit, we take a slightly different approach. For this to work, we would need the following files:
    hello64.asm – here we use the example file from Method #2, because the equivalent file from Method #1 doesn’t work—it fails during the linking process. We slightly modify the file to include “osx.inc” as seen below:
    hello64-asmosx.inc

    – contains the syscalls definition numbers
    osx-inc

  12. Compile hello64.asm.
    $ fasm hello64.asm hello64.o
  13. Convert the object file “hello64.o” into the appropriate format
    $ objconv –fmacho64 –ar:start:_start –nu hello64.o hello64_m.o
    the –ar:start:_start option specifies that “_start” is an alias for the public symbol “start” defined in the x86_84 architecture. If we don’t make the alias, ld would spit an “undefined symbol” error like the following:
    undefined_symbol
  14. Link the the resulting hello64_m.o to generate the hello64 executable, validate it using file command and execute the binary.
    $ ld –arch x86_64 -macosx_version_min 10.6 -o hello64 hello64_m.o
    $ file hello64
    $ ./hello64
  15. The following screenshot demonstrates the execution of the commands as described and the resulting outputs:
    hello64

That’s it!

For easy access to the files used in this How-To, I attached them in .rar format—download it here.

Comments on this post

  1. John

    Nice tutorial. Works great.

  2. Wow, this post is fastidious, my sister is analyzing such things, therefore I am going to inform her.

  3. If you are going for most excellent contents like I do, just pay a visit this site every day because it provides feature contents, thanks

  4. I was able to find good advice from your blog posts.

  5. This information is priceless. When can I find out more?|

  6. This helps. Thanks!

  7. I am extremely impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you modify it yourself? Either way keep up the excellent quality writing, it’s rare to see a great blog like this one these days.

  8. This blog was… how do I say it? Relevant!! Finally I have
    found something which helped me. Kudos!

  9. Hi there! Would you mind if I share your blog with my myspace group? There’s a lot of people that I think would really enjoy your content. Please let me know. Cheers
    [url=https://rexuiz.itch.io/rexuiz-fps]Rexuiz FPS game[/url]

Leave a Reply

Your email address will not be published. Required fields are marked *

Trackbacks and Pinbacks on this post

No trackbacks.

TrackBack URL