ARM Assembly w/GCC for Raspberry PI

I’ve actually found it oddly challenging to find resources on the Internet that talk specifically about how to write assembly code on the Raspberry PI (or any other ARM device) using GCC and C/C++. I specifically wanted to avoid inline assembly as I wish to work with assembly on its own without cluttering up my work with C code. So, here goes:

Getting Started:

First things first, I am using a completely fresh “wheezy” on the Raspberry Pi to do this work. This should work on any distribution though you may need to ensure you have the appropriate gcc toolchain installed if you are using something else.

Hello World:

asmtest.c

hello.s

Coming from x86 Assembly, a few things were new to me. The first is the completely natural name for registers! R0 to R14 is fantastic. The first 4 parameters of a C function are passed in R0-R3 similar to x86’s use of eax-edx. The second thing was the lack of a proper “ret” statement. Instead, I found I needed to use “mov pc, lr” which is basically a manual version of a return. I don’t know yet if there’s a true “ret” but this works just fine (though it’s a bit harder to read than x86).

Compiling and running the code is as easy as any other compilation with gcc:

Leave a Reply

%d bloggers like this: