Mac assembly language by Gary B. Little, 1986, Prentice Hall Press edition, in English.
Shaping the future by understanding our past
![Text edit Text edit](/uploads/1/2/4/2/124226714/988444604.gif)
I'm constantly busy shaping the future, exploring innovations, working on new ideas. But to accomplish all of this I also need to understand our past, where we came from, how it all started. It is then so much easier to see the big picture and many of the underlying details of the otherwise unknown and uncertain future.
This is even more true for application development. In a period of Functional Programming, Machine Learning and AI, still under the hood it all goes down to pure machine code. A mere sequence of zeroes and ones organized in words and made human readable thanks to assembler pseudo codes.
CPU gets every year faster, bigger in capacity, smaller in size, but... the internal structure and modus operandi is basically still the same as 50 years ago
Evolution of Intel CPU vs. no-evolution of the instruction set:
Even the most advanced technology as Blockchain is deeply based on Assembly. On the Ethereum network GAS costs of Smart Contracts are visibly calculated based on which machine codes is being executed!
As you see unless a breakthrough in the way CPU internally works... a real revolution in Machine Learning or even AI will remain just a chimera. But this is a different story.
Assembler on a Mac
Guess my surprise when I actually discovered that I could still program in Assembler using my newest MacBook Pro !
Yeaaah... you can build, debug and run machine code programs right from your Mac!
Complete source code and some tutorials at my GitHub page: github.com/gaetanocausio
Well, first you need to install the proper tools. Open the terminal application and enter the ld command. If you get a popup warning like the print-screen below, then the tools are not yet installed, simply confirm by clicking on 'Install' (no need to get Xcode).
Now we are ready to try our first assembly program. Open a text editor, copy and paste code below and save it as... guess what? HelloWorld.s
![Edit Edit](/uploads/1/2/4/2/124226714/974041852.png)
To be able to run an assembly program we need first to create an executable. This is a two step process. First you compile your source code by using the as command line tool, this will produce an intermediate object code that would need to be linked via the ld tool in the second step. The linker will make use of system libraries and resolve all references in actual addresses so to create the actual executable. See example below:
For macOS Mojave 10.14 use these parameters while linking:
But as developers we know very well that debugging is an important aspect of application development and for assembly it is even more crucial. To debug an assembly program simply use the lldb command in terminal as shown below:
TIP: After executing the run command in lldb, try entering the gui command (lowercase)... it will start a GUI debug editor like in the cover image on this post!
Diving even deeper in assembly
Let's have a look on how parameters are passed from command line while discovering the similarities with the C language. In assembly the stack has an important role when passing control by means of a call statement. It points to 3 addresses:
Don't be confused with the third address, it's a bit tricky: it actually does not point to the parameters themselves, but to two more addresses, the first one is the location of the program name, while the second finally points to an array with the parameters:
It should all become clear with an example, when starting a program you have:
Tips and nice to know
- Assembler on a Mac is slightly different than on a Unix or a PC. Therefore source code from other systems may not compile correctly on a Mac.
- The Stack Pointer register needs to be aligned to 16-byte boundary prior of performing a call. Failing to do so will lead to a segment address exception.
- Registers are name based on the architecture used, so the Accumulator register is named AX on a 16bit architecture, EAX for 32bit and RAX for 64bit.
- The full Apple reference to Mac OS X Assembler can be found here.