The ARM is one of the most popular processors today. Think the iPhone, Raspberry Pi. I decided I should learn more about it since I knew almost nothing about the ARM architecture.
From Wikipedia, “In 2005, about 98% of all mobile phones sold used at least one ARM processor. In 2010, producers of chips based on ARM architectures reported shipments of 6.1 billion ARM-based processors, representing 95% of smartphones, 35% of digital televisions and set-top boxes and 10% of mobile computers. In 2011, the 32-bit ARM architecture was the most widely used architecture in mobile devices and the most popular 32-bit one in embedded systems. In 2013, 10 billion were produced and ‘ARM-based chips are found in nearly 60 percent of the world’s mobile devices.’”
The short version is the following. The ARM 32-bit processor has 16 general purpose 32-bit registers, though R14 and R15 are the stack pointer and program counter, with an orthogonal set of instructions. There’s the so-called “Thumb” set of instructions. If you use this subset, the instructions can be “compressed” into 16-bit instructions.
Orthogonal means you don’t need dedicated instructions for particular registers, e.g., “LDA” to load a value into the accumulator, or “STX” to store a value from an X index register into a memory location. These instructions look like “MOV R2, R3” to move a value from R3 into R2.
At some point it occurred to me that it should be pretty straightforward to write and run assembler on a Raspberry Pi running Raspbian Linux. Voilà!
There’s an excellent series called “ARM Assembler in Raspberry Pi.” The posts are broken out into small chapters. This set of examples uses the gcc compiler to assemble .s files. It pulls in the C overhead to make an executable file.
First Program
Here’s my first program
.global main
main:
mov r0, #2 /* Put 2 in register r0. */
bx lr /* Return from main. */
To assemble and build it:
as -o first.o first.s
gcc -o first first.o
pi> ls -l
total 20
-rwxr-xr-x 1 pi pi 5564 Jan 14 21:38 first
-rw-r--r-- 1 pi pi 640 Jan 14 21:37 first.o
-rw-r--r-- 1 pi pi 193 Jan 14 21:37 first.s
Run. The answer is the return code from the program.
pi> ./first
pi> ./first ; echo $?
2
I found another excellent tutorial by Derek Banas on YouTube which showed how to use the Linux “ld(1)” command to load (link) the assembled .o file which has much lower overhead than using the gcc compiler. Of course, how could I have forgotten ld.
ARM Processor Architecture
Wikipedia: ARM architecture
ARM assembler in Raspberry Pi – Chapter 1
Introduction to ARM
Assembly Language Tutorial (Video)