Last week, we talked about using a new Apple M1 based Macintosh as a development workstation and how installing Apples development system XCode also installed a large number of open source development tools including LLVM and make. This week, well cover how to compile and run a simple command line ARM Assembly Language Hello World program.
Thanks to Alex vonBelow
My book Programming with 64-Bit ARM Assembly Language contains lots of sample self contained Assembly Language programs and a number of iOS and Android samples. The command line utilities are compiled for Linux using the GNU tool set. Alex vonBelow took all of these and modified them to work with the LLVM tool chain and to work within Apples development environment. He dealt with all the differences between Linux and MacOS/iOS as well. His version of the source code for my book, but modified for Apple M1 is available here:
Differences Between MacOS and Linux
Both MacOS and Linux are based on Unix and are more similar than different. However there are a few differences of note:
- MacOS uses LLVM by default whereas Linux uses GNU GCC. This really just affects the command line arguments in the makefile for the purposes of this article. You can use LLVM on Linux and GCC should be available for Apple M1 shortly.
- The MacOS linker/loader doesnt like doing relocations, so you need to use the ADR rather than LDR instruction to load addresses. You could use ADR in Linux and if you do this it will work in both.
- The Unix API calls are nearly the same, the difference is that Linux redid the function numbers when they went to 64-bit, but MacOS kept the function numbers the same. In the 32-bit world they were the same, but now they are all different.
- When calling a Linux service the function number goes in X16 rather than X8.
- Linux installs the various libraries and includes files under /usr/lib and /usr/include, so they are easy to find and use. When you install XCode, it installs SDKs for MacOS, iOS, iPadOS, iWatchOS, etc. with the option of installing lots for versions. The paths to the libs and includes are rather complicated and you need a tool to find them.
- In MacOS the program must start on a 64-bit boundary, hence the listing has an .align 2 directive near top.
- In MacOS you need to link in the System library even if you dont make a system call from it or you get a linker error. This sample Hello World program uses software interrupts to make the system calls rather than the API in the System library and so shouldnt need to link to it.
- In MacOS the default entry point is _main whereas in Linux it is _start. This is changed via a command line argument to the linker.
Hello World Assembly File
Below is the simple Assembly Language program to print out Hello World in a terminal window. For all the gory details on these instructions and the architecture of the ARM processor, check out my book.
//// Assembler program to print “Hello World!”// to stdout.//// X0-X2 – parameters to linux function services// X16 – linux function number//.global _start // Provide program starting address to linker.align 2// Setup the parameters to print hello world// and then call Linux to do it.
_start: mov X0, #1 // 1 = StdOutadr X1, helloworld // string to printmov X2, #13 // length of our stringmov X16, #4 // linux write system callsvc 0 // Call linux to output the string
// Setup the parameters to exit the program// and then call Linux to do it.
mov X0, #0 // Use 0 return codemov X16, #1 // Service command code 93 terminates this programsvc 0 // Call linux to terminate the program
helloworld: .ascii “Hello World!\n”
Here is the makefile, the command to assemble the source code is simple, then the command to link is a bit more complicated.
HelloWorld: HelloWorld.old -macosx_version_min 11.0.0 -o HelloWorld HelloWorld.o -lSystem -syslibroot`xcrun -sdk macosx –show-sdk-path` -e _start -arch arm64 HelloWorld.o: HelloWorld.sas -o HelloWorld.o HelloWorld.s
The xcrun command is Apples command to run or find the various SDKs. Here is a sample of running it:
stephensmith@Stephens-MacBook-Air-2 ~ % xcrun -sdk macosx –show-sdk-pathobjc: Class AMSupportURLConnectionDelegate is implemented in both ?? (0x1edb5b8f0) and ?? (0x122dd02b8). One of the two will be used. Which one is undefined.objc: Class AMSupportURLSession is implemented in both ?? (0x1edb5b940) and ?? (0x122dd0308). One of the two will be used. Which one is undefined.objc: Class AMSupportURLConnectionDelegate is implemented in both ?? (0x1edb5b8f0) and ?? (0x1141942b8). One of the two will be used. Which one is undefined.objc: Class AMSupportURLSession is implemented in both ?? (0x1edb5b940) and ?? (0x114194308). One of the two will be used. Which one is undefined./Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdkstephensmith@Stephens-MacBook-Air-2 ~ %
After the ugly warnings from Objective-C, the path to the MacOS SDK is displayed.
Now we can compile and run our program.
stephensmith@Stephens-MacBook-Air-2 Chapter 1 % make -Bas -o HelloWorld.o HelloWorld.sobjc: Class AMSupportURLConnectionDelegate is implemented in both ?? (0x1edb5b8f0) and ?? (0x1145342b8). One of the two will be used. Which one is undefined.objc: Class AMSupportURLSession is implemented in both ?? (0x1edb5b940) and ?? (0x114534308). One of the two will be used. Which one is undefined.ld -macosx_version_min 11.0.0 -o HelloWorld HelloWorld.o -lSystem -syslibroot `xcrun -sdk macosx –show-sdk-path` -e _start -arch arm64 stephensmith@Stephens-MacBook-Air-2 Chapter 1 % ./HelloWorld Hello World!stephensmith@Stephens-MacBook-Air-2 Chapter 1 %
The new Apple M1 Macintoshes are running ARM processors as part of all that Apple Silicon and you can run standard ARM 64-bit Assembly Language. LLVM is a standard open source development tool which contains an Assembler that is similar to the GNU Assembler. Programming MacOS is similar to Linux since both are based on Unix and if you are familiar with Linux, most of your knowledge is directly applicable.