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:
- Method #1: https://github.com/JohnDDuncanIII/fasm-osx
- Method #2: https://github.com/davidad/fasm_example_64
- Method #3: https://github.com/FlyingJester/fasm
Basically, these are the sources of our knowledge on how to install fasm on Mac OS X (Sierra)
- Go to https://github.com/JohnDDuncanIII/fasm-osx.git and click on the “Clone or download” button to download the package.
- 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
- Execute fasm, the following should be the output:
- Execute objconv, the following should be the output:
- 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.
- While you are still in the dowload directory, perform the following highlighted commands:
- Compile hello32.asm
$ fasm hello32.asm hello32.o
- 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.
- 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.
- 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.
- 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:
- Compile hello64.asm.
$ fasm hello64.asm hello64.o
- 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:
- 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
- The following screenshot demonstrates the execution of the commands as described and the resulting outputs:
For easy access to the files used in this How-To, I attached them in .rar format—download it here.