ARM Assembly Code (Raspberry Pi)

Pi Debugger

On Saturday 8th September my son and I visited my local Pi Jam in Manchester, where Jack Kelly (Twitter: @SecretImbecile) was giving a workshop on ARM assembly code.

Before I continue I would recommend readers to give the Crash Course Computer Science video below a quick view as it explains registers, and registers play a big part in assembly code:

 

Unlike most computers which use an x86 processor , the Raspberry Pi uses an ARM processor. The processor in the Raspberry Pi (specifically the Raspberry Pi 3) has 13 registers that it can write / read from.

Storing a value in Register 0
Storing a value in Register 0

Jack explained how to store values within the registers, how to create loops, branches and use arithmetic in assembly. In the above example the value 65 was stored in register 0 and on exit the value 1 was stored in register 7.

After successfully writing assembly code and compiling it the group then got to run it through the Pi’s debugger to check that the code worked. As no output (i.e. print statements) were used we relied on the debugger to show if the code worked correctly:

Pi Debugger
Pi Debugger

The debugger showed the register values in both hexadecimal (base 16) and binary (base 10).

The benefit of writing code in assembly language versus writing code in a higher language (e.g. Python) was demonstrated during the workshop using a count loop. The loop would start at 1 and incrementally add 1 until it reached 1,000,000,000 (a billion). In assembly code this completed within a second. A similar loop in Python 3 was still running after a few moments and would have taken over 15 minutes to complete.

I had a good time learning about assembly code, the commands for compiling code on the Pi and exploring the Pi debugger (gdb).

Another cool fact I learned today, the time command can be used in front of a program to display how long it takes to complete. This works on Linux and Mac (not had chance to test on Windows yet, I presume it will in PowerShell).

geektechstuff_time_option
Using the time option to run a program

NOTE: Assembly code runs differently on different processors, which is why assembly code written on the Raspberry Pi 3 errors on my Apple MacBook.