Think In Geek

In geek we trust

ARM assembler in Raspberry Pi – Chapter 1

In my opinion, it is much more beneficial learning a high level language than a specific architecture assembler. But I fancied learning some ARM assembler just for fun since I know some 386 assembler. The idea is not to become a master but understand some of the details of what happens underneath.

Introducing ARM

You will see that my explanations do not aim at being very thorough when describing the architecture. I will try to be pragmatic.

ARM is a 32-bit architecture that has a simple goal in mind: flexibility. While this is great for integrators (as they have a lot of freedom when designing their hardware) it is not so good for system developers which have to cope with the differences in the ARM hardware. So in this text I will assume that everything is done on a Raspberry Pi Model B running Raspbian (the one with 2 USB ports and 512 MB of RAM).

Some parts will be ARM-generic but others will be Raspberry Pi specific. I will not make a distinction. The ARM website has a lot of documentation. Use it!

Writing assembler

Assembler language is just a thin syntax layer on top of the binary code.

Binary code is what a computer can run. It is composed of instructions, that are encoded in a binary representation (such encodings are documented in the ARM manuals). You could write binary code encoding instructions but that would be painstaking (besides some other technicalities related to Linux itself that we can happily ignore now).

So we will write assembler, ARM assembler. Since the computer cannot run assembler we have to get binary code from it. We use a tool called, well, assembler to assemble the assembler code into a binary code that we can run.

The tool to do this is called as. In particular GNU Assembler, which is the assembler tool from the GNU project, sometimes it is also known as gas for this reason. This is the tool we will use to assemble our programs.

Just open an editor like vim, nano or emacs. Our assembler language files (called source files) will have a suffix .s. I have no idea why it is .s but this is the usual convention.

Our first program

We have to start with something, so we will start with a ridiculously simple program which does nothing but return an error code.

/* -- first.s */
/* This is a comment */
.global main /* 'main' is our entry point and must be global */
main:          /* This is main */
    mov r0, #2 /* Put a 2 inside the register r0 */
    bx lr      /* Return from main */

Create a file called first.s and write the contents shown above. Save it.

To assemble the file type the following command (write what comes after $ ).

$ as -o first.o first.s

This will create a first.o. Now link this file to get an executable.

$ gcc -o first first.o

If everything goes as expected you will get a first file. This is your program. Run it.

$ ./first

It should do nothing. Yes, it is a bit disappointing, but it actually does something. Get its error code this time.

$ ./first ; echo $?

Great! That error code of 2 is not by chance, it is due to that #2 in the assembler code.

Since running the assembler and the linker soon becomes boring, I’d recommend you using the following Makefile file instead or a similar one.

# Makefile
all: first
first: first.o
	gcc -o [email protected] $+
first.o : first.s
	as -o [email protected] $<
	rm -vf first *.o

Well, what happened?

We cheated a bit just to make things a bit easier. We wrote a C main function in assembler which only does return 2;. This way our program is easier since the C runtime handled initialization and termination of the program for us. I will use this approach all the time.

Let’s review every line of our minimal assembler file.

/* -- first.s */
/* This is a comment */

These are comments. Comments are enclosed in /* and */. Use them to document your assembler as they are ignored. As usually, do not nest /* and */ inside /* because it does not work.

.global main /* 'main' is our entry point and must be global */

This is a directive for GNU Assembler. A directive tells GNU Assembler to do something special. They start with a dot (.) followed by the name of the directive and some arguments. In this case we are saying that main is a global name. This is needed because the C runtime will call main. If it is not global, it will not be callable by the C runtime and the linking phase will fail.

main:          /* This is main */

Every line in GNU Assembler that is not a directive will always be like label: instruction. We can omit label: and instruction (empty and blank lines are ignored). A line with only label:, applies that label to the next line (you can have more than one label referring to the same thing this way). The instruction part is the ARM assembler language itself. In this case we are just defining main as there is no instruction.

    mov r0, #2 /* Put a 2 inside the register r0 */

Whitespace is ignored at the beginning of the line, but the indentation suggests visually that this instruction belongs to the main function.

This is the mov instruction which means move. We move a value 2 to the register r0. In the next chapter we will see more about registers, do not worry now. Yes, the syntax is awkward because the destination is actually at left. In ARM syntax it is always at left so we are saying something like move to register r0 the immediate value 2. We will see what immediate value means in ARM in the next chapter, do not worry again.

In summary, this instruction puts a 2 inside the register r0 (this effectively overwrites whatever register r0 may have at that point).

    bx lr      /* Return from main */

This instruction bx means branch and exchange. We do not really care at this point about the exchange part. Branching means that we will change the flow of the instruction execution. An ARM processor runs instructions sequentially, one after the other, thus after the mov above, this bx will be run (this sequential execution is not specific to ARM, but what happens in almost all architectures). A branch instruction is used to change this implicit sequential execution. In this case we branch to whatever lr register says. We do not care now what lr contains. It is enough to understand that this instruction just leaves the main function, thus effectively ending our program.

And the error code? Well, the result of main is the error code of the program and when leaving the function such result must be stored in the register r0, so the mov instruction performed by our main is actually setting the error code to 2.

That’s all for today.

, , ,

80 thoughts on “ARM assembler in Raspberry Pi – Chapter 1

  • Jonathan Hinchliffe says:

    Really excellent tutorial.

  • Fernando says:

    Nice! I’ve been doing a bit of reading before about registers and general CPU functioning (basically, reading some chapters about ‘Computer design’), and I think that everything makes much more sense with this bit of background.

    I can’t stop thinking about how I’d interpreted this same Introduction with any idea – I think it’d sound like magic!

    Again, still nice. I’d carry on with the next part!

  • […] Artikel zur Assembler-Programmierung im MagPi! Für den weiteren Einstieg in das Thema gibt es bei ThinkGeek noch einen 12-Teiligen kostenfreien Kurs. Yeah, das Wochenende ist damit wohl […]

  • Barret says:

    I’m getting this error when I try to assemble the file with
    $ as -o first.o first.s

    first.s:7: Error: expecting operand after ','; got nothing
    first.s:8: Error: no such instruction: `bx lr'

    • rferrer says:

      Are you running that command on the Raspberry Pi? This is ARM assembler, so you must use an ARM assembler like the one in the Raspberry Pi.

      I get the very same error when trying to assemble first.s on my x86_64 machine. It will not work there, obviously.

      • Barret says:

        Oh yeah, duh. Is there a way to cross compile code on a x86_64 machine for ARM? I’ve tried Google, but with little concrete instructions.

        • rferrer says:

          You can use crosstol-ng. Last time I checked it was pretty easy to make a cross toolchain for Raspberry Pi. Anyways, you won’t be able to run the code unless you copy it to the Raspberry Pi. I think it is easier to assemble directly there.

  • Fernando says:


    I’ve found elsewhere that there are some directives such as “.req” and “.unreq” that let us work with registers as if they were variables.

    I haven’t seen that in this course, so I just add this quick comment hoping it is useful for someone else!

    P.S: Complete list of directives for ARM can be found here,

  • carnivion says:

    Hi, rferrer. Thanks for this tutorial. I’m very beginning at arm assembly. I’m wondering how to build the developing environment? Do you use any IDE? Since I read above paragraphs, it’s seems using an IDE with debugging feature, and output the error code into “output” panel or some other kind of that.

    • rferrer says:


      I’m not using any IDE.

      As stated above, a text editor like vim, nano or emacs will do.

      To build the programs you may want to use the makefile shown in the post.

      Kind regards.

      • carnivion says:

        Great! I run it ok! Thanks rferrer, and another question, I’m also getting along with baking Pi OS by Cambridge. Can I use in-built as command instead of ...none-eabi... thing?

        • rferrer says:


          yes as long as the tool as --version says somehting like This assembler was configured for a target of `arm-linux-gnueabihf'..

  • Eddie Walsh says:

    Hi, Thanks for doing this Tutorial, as I am a complete beginner, so my first question is how do I Launch GNU Assembler, but then you say to open a Text editor like vim, nano, or emacs, so where do I get one of them, again Thanks, Regards ED

    • rferrer says:

      Hi Eddie,

      if you are using Raspbian and using the graphical environment, you will see an icon on your desktop that reads “LXTerminal”. Double-click on it and it will open a terminal window where you can type commands (in that window you may use the right-button to open a popup-menu with the usual options for copy/paste, etc).

      Now open an editor, given that you are a beginner you’ll probably want to use ‘nano’ which is the easiest of the three. In the terminal type

      nano first.s

      and copy-paste the text of Our first program shown above in the post. Then press Ctrl-X to exit (in nano this is depicted as ^X). Before exiting nano will ask you about keeping the file, say Yes and press Return to use the proposed filename (which is the one we specified in the command line, i.e. first.s).

      Now copy paste the following commands shown above (note that the in the post $ at the beginning of the line is just an representation of the prompt and you do not have to type it). Press Return after each line. (Note that I am repeating here the commands in the post for clarity)

      as -o first.o first.s
      gcc -o first first.o

      The first line invokes the GNU Assembler (to assemble the code into machine code) and the second one invokes the C compiler just to link the program and generate an executable file called ‘first’. Then you can run it.

      ./first; echo $?

      You should see a 2, which is the exit code of our very first program.

      If you feel intimidatd by the command line, you may want to invest some time reading a tutorial like this one. It is not specific to Raspberry but nevertheless will give you enough information so you can feel comfortable in the terminal. Regarding the Makefile, you may want to read this short introduction to make which will allow you to understand how to use the Makefile shown at the end of the post.

      Kind regards,

      • Eddie Walsh says:

        Hi rferrer, Thanks again, I tried again to follow your instructions from your reply, but when you say “Copy” and “Paste” you did not say from where ??, so I went back to the orig Tutorial and at last got to the end, so I will check out the 2 more links you suggested and hopefully I will get it ??, again can not Thank you enough, Regards ED

        • rferrer says:

          Hi Eddie,

          maybe I wasn’t clear enough. I meant to copy and paste from the code of the first program (you’ll find in the body of this post, above the comments block of this page) into the ‘nano’ editor.

          Kind regards,

          • Eddie Walsh says:

            Hi rferrer, I thaught that was what you meant, but if I try your Tutorial at same time as Nano on Pi it virtually slows to a crawl, so I run the Tutorial on my Desk Top pc, or again am I doing something wrong, any help appreciated, Regards Ed

          • rferrer says:

            Hi Eddie,

            you can connect to the Raspberry Pi from your laptop using the SSH protocol. Make sure that both the laptop and the Raspberry are connected to the same router.

            This way you can have your Raspberry turned on but you do not need to connect a keyboard nor a display.

            This tutorial explains the process very thoroughly.

  • Chria says:

    Thank you for this great tutorial. This mini hello world assembler program puts me on the right track. 🙂

  • […] the ARM assembly tutorial series from is an interesting stuff to learn about ARM assembly and has about 21 chapters about various […]

  • SaberWriter says:

    Great article. Thanks for writing it up. This was the kind of start I was looking for with my RPi.

  • Jimmy Yang says:

    Hi, I have a question.
    In real word, do the engineer write assembly language or just write c code to develop arm?

    • rferrer says:

      Hi Jimmy Yang,

      in real world most of the time a high level language is used (like C or others). Sometimes, though, there are performance-critical scenarios where one has to get down to the assembler level because even a C compiler may not deliver all the possible performance. An example of such scenario may be video decoding: in order to be able to deliver the appropiate number of frames per second, the decoding process must be as fast as possible.

      Also, while I would not recommend anyone to write in assembler in a day-to-day basis, it is very interesting to know what underpins higher level languages.

      Kind regards,

    • Denis Borisevich says:

      Another application for knowledge assembly language is to be able to read assembly generated by your C compiler to be sure that compiler understood your high-level code correctly and you used the right language feature for the task your are solving. It concerns mostly the optimization tasks and not your everyday coding routine, though.

  • […] Reading about ARM assembler in Raspberry Pi… […]

  • Denis says:

    Dear Roger,
    big Thanks for your articles, will be waiting new posts.

    Can I translate your articles to Russian language for the with references to you site?

    Best Regards

  • Christopher says:

    Very friendly introduction! Thanks 🙂

  • Christopher says:

    Your wp-comments-post.php is publicly displaying your PHPMailer communications.

  • Helder says:

    Hi. I just found this post and I have to say: Thank you! And congratulations for the great job!

    I didn’t see the whole series yet but, will You cover how to use some native Raspbian’s API (probably’s api) to create a program with GUI in Assembly?

    Would be interesting see something like that!

    Again thank you and keep going the great job!

    • Roger Ferrer Ibáñez says:

      Hi Helder,

      regarding your question about GUI in assembly, I do not cover GUI in any chapter and I’m not planning to do it. But who knows, maybe I change my mind.

      It is doable, of course, but it would end being doing lots of calls to the specific GUI toolkit API. At first it looks rather boring and tedious. This does not mean it is impossible but I think it is not a task particularly revealing when done using assembler 🙂

      Kind regards,

      • Helder says:

        I see…
        I dont know assembly and I pretend to use your tutorials to learn it for fun… Do you thing is possible to use any GUI library in assembly code?

        Thank you.

  • One of the most lucid introductions to ARM assembly language I’ve seen.
    The clear, and easy, expository style is a breath of fresh air.
    One can only hope that you’ve got quite a bit more to say on this subject…

    for example–
    the actual assembly process from beginning to end, i.e., text editor > assembler > loading object code into machine and running the program (use of the Raspberry Pi as the target machine might be desirable due to the ubiquitous nature of this device).

    Please keep up the good work.

    • Roger Ferrer Ibáñez says:

      Hi Robert,

      thank you for the kind words.

      Regarding explaining the rest of the assembly process: yes I have planned to make some posts in this line in the future.

      Warm regards,

  • Please bear with me…
    My only use for assembly language has been to get hardware designs to run (minicomputers, Z80, 68XXX, 80XXX, PIC, Arduino, etc.), when there’s not a “proper” operating system to lean on. My software IS the operating system.
    I am not at all conversant (except for Fortran and BASIC) with any higher-level language, or environment, except to be able to READ C and C++ (which explains my trepidation about embracing the Raspberry Pi, I suppose).
    Hopefully, you’re in a good position to understand my question, and can answer it admirably:
    What is the reason for writing Assembly Language encased in a “C/C++” “wrapper”? Is this necessitated by the RPi environment? Can I write Assembly Language for the RPi as I do for a PIC machine, or an Atmel processor?
    For me, at least, clarification of this point will be deeply appreciated.
    Personal note: I have been extremely disappointed in the number of “authors” who purport to teach RPi Assembly Language by requiring one to use Raspbian. I may not be an expert, but I know this is not true Assembly programming. It at least appears as if you’re leading us to “the real thing”, and in the proper direction.

    Thanks in advance very much, and please keep up the outstanding work.

    • Roger Ferrer Ibáñez says:

      Hi Robert,

      as you excellently pointed, there is actually no need to write assembly in the way we do in these chapters: this is not a requirement of the Raspberry Pi itself.

      There are several resources where people have done bare metal development on the Raspberry Pi. For instance, there is the course Baking Pi – Operating Systems Development from the University of Cambridge where a basic operating system is written on top of the Raspberry Pi. Another resource of bare metal information for the Raspberry Pi can be found here.

      In this blog, though, I focus on the point of view of applications rather than the systems. I think that most Raspberry Pi users are using an operating system, likely Raspbian (or another Linux or BSD derivative), so it makes sense to me to start writing small programs on top of the operating system using assembler.

      I struggle with the concept of «true assembly programming». Personally, assembler is just another tool in the box (a special one that, unfortunately, may not be portable among systems). There are a few situations where assembler knowledge is a requirement: systems programming (i.e. firmwares and some critical routines of operating systems), writing compilers, debugging and writing performance-critical code.

      I hope I answered your questions well enough.

      Kind regards.

  • Thank you for your rapid response; I’m beginning to ‘get it’.
    No need to reply; I have very confidence that, given your outstanding efforts, the only additional element needed is patience on my part.
    Warmest regards…

  • Marius says:

    Hi Roger,

    First of all , thanks for the awesome tutorial you set up here. I have a few questions:

    1) Is there any chance this will work in the raspberry pi 2 ?
    2) Will you ever make a tutorial for more OS-development oriented folks, nothing fancy, just something that boots up and takes you to a simple command line. I guess then everyone could get their simple OS started and modify it as they wish.

    Again ,thanks for teaching me quite a bit about ARM assembly.

    • Roger Ferrer Ibáñez says:

      Hi Marius,

      thank you for your kind comments.

      1) in principle yes given that the ARMv6 architecture of the Raspberry 1 is contained in the ARMv7 architecture of the Raspberry 2.
      That said, most of the VFPv2 instructions used in the chapter 14 will run very slow as they are emulated in software (NEON instructions should be used instead)

      2) Not at the moment. You may want to check Baking Pi – Operating Systems Development. They have a big warning that the tutorial has not been updated for the Raspberry Pi 2 but this should not mean it cannot work there.

      Kind regards,

  • […] Sophie Wilson fala tudo sobre o ARM, e se você quer aprender o Assembler dele, divirta-se […]

  • Gabriel Tobar says:

    Hi! Nice tutorial and very helpful! I was wondering if I could use some of your help… currently I’m making an assembly project and right now I need to generate a random number. I have tried with an algorithm do generate pseudo random numbers but I dont know if it is working because whenever I print the result it just gives me error code 1.

    this is the code:


    .balign 4
    mensaje1: .asciz “Random \n”

    .balign 4
    return: .word 0


    .global main

    ldr r1, addr_of_return
    str lr, [r1]

    ldr r0, addr_of_msg1
    bl printf

    mov r0,#3
    mov r1,#4

    tst r1,r1, lsr #1
    movs r2,r0, rrx
    adc r1,r1, r1
    eor r2,r2, r0, lsl #12
    eor r0,r2, r2, lsr #20

    ldr r0, [r0]
    bl printf

    ldr lr, addr_of_return
    ldr lr, [lr]
    bx lr

    addr_of_msg1: .word mensaje1
    addr_of_return: .word return

    .global printf

    Could you help me? I sincerely dont know what did I do wrong. Is there another way to generate the random numbers? I was trying as well with the clock, but had the same results…

    Thank you!


    • Roger Ferrer Ibáñez says:

      Hi Gabriel,

      I have not checked your code but an easy way to generate a random number is calling C library function random. It receives no arguments (but recall that r0 to r3 may be modified by the callee!) and it returns the result as a 32-bit number in r0. Note that these random numbers should not be used for serious purposes (like cryptography) 🙂

      Kind regards,

      • Max Q. says:

        Hi Roger, i was wondering if you could give an example using that random function, or give another page where i can find such example, thank you in advance.

        • Roger Ferrer Ibáñez says:

          Hi Max,

          in chapter 9 and 10 it is explained how to call functions. Function random receives 0 arguments, so it should be easy. The random number will be left in r0.

          More information on the random function can be found in its man page.


  • Michael Koch says:

    Where can I find a manual for the “as” assembler? I need some more info about pseudo-commands like .equ, .rept, .endr, .macro, .endm, push, pop…

  • Michael Koch says:


  • Fred says:

    Looks like an awesome and well explained tutorial. Since I already know some x86 assembler in Linux I always wondered how this looks like for ARM. And the Raspberry Pi is the perfect platform for that. I am really looking forward to learn something about ARM assembler.

  • Luis Guilherme says:

    .s for Source

  • Martin says:


    Just discovered this site, a bit late to the game it appears…

    thanks so much for writing these, I’ve always wanted to get ‘my hands dirty’ playing with assembler and understanding its processes…this looks like the ideal support and you are clearly committed to passing on your knowledge. I am already looking forward to the next chapters

    Thanks again


  • Ron Yorke says:

    Having finished my blether, does anybody have full hardware details of the various versions of the RPi? The last time I tried to get a copy, I was informed that that level of information was only available to bona fide companies designing products for third parties. I hope that this attitude has changed, and the information is now more freely available.

  • ehrt74 says:

    btw. If anybody wants to use the gnu linker rather than gcc you have to change your code like this;

    .global _start
    mov r0, #2
    mov r7, #1 // request to exit program
    swi 0

    then you can assemble and link it as follows:

    as myProgram.s -o myProgram.o
    ld myProgram.o -o myProgram

    and run it with


  • aldenq says:

    Hello, thanks for creating this article.
    I have been having some problems with it though, for example upon executing the code from the first example it returns 132 rather then 2.

    • Roger Ferrer Ibáñez says:

      That’s weird.

      Without more info I cannot help much. Are you following the correct steps?

      Kind regards

  • […] stumbled across this Raspberry Pi assembler tutorial which seemed like a fun thing to try out. Who would have thought you could spend your evening […]

  • […] The “Hello World” of assembly language programming. (Code credit: Roger Ferrer Ibáñez) […]

  • Ciro Santilli says:

    Good article. Here are a bunch of further examples with a nice QEMU user mode setup and and assertions:

  • Kip Ingram says:

    I would say that the more one knows the better off one is – familiarity with the underlying architecture of your platform (in all respects – instruction set, cache operation, etc. etc.) can only make you a stronger engineer. So yes, know high level languages. But know more – know as much as possible.

  • John says:

    Hi all,
    is it possible use directly gcc as frontend to as?
    I tried:
    gcc -o first first.s
    ./first ; echo $?
    and it works

  • Marcin says:

    .s probably stands for ‘source’

  • Muhammad says:

    OMG! i have been searching for a way to do this all over the internet but couldn’t get a useful info like this. Thank you very much.

  • George Adams says:

    I would appreciate it if you would globally replace the word “assembler” with “assembly” in this document to match the name that the computing community and the ARM organization give to the language so well presented here. Thank you for considering this request.

    • Roger Ferrer Ibáñez says:

      Hi George,

      I’m afraid my usage of “assembler” will stay: it occurs so many times in these posts that I don’t feel very motivated to update all the occurrences.

      That said, consider this an acknowledgement that I might have wanted to use the term “assembly” instead.

      Thank you,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.