0% found this document useful (0 votes)
232 views

ARM Assembler RaspberryPi

This document introduces ARM assembly language programming on the Raspberry Pi. It begins with an overview of ARM architecture and its goal of flexibility. The document then discusses using the GNU Assembler (gas) to assemble ARM assembly code files with a .s extension. As a first example, a very simple "hello world" style program is presented that stores the value 2 in a register and returns without doing anything else. The program is assembled using gas and linked with gcc to create a runnable executable. When run, the program exits with an error code of 2 as expected. Finally, a Makefile is suggested for simplifying building and running assembly programs going forward.

Uploaded by

König Löwe Pie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
232 views

ARM Assembler RaspberryPi

This document introduces ARM assembly language programming on the Raspberry Pi. It begins with an overview of ARM architecture and its goal of flexibility. The document then discusses using the GNU Assembler (gas) to assemble ARM assembly code files with a .s extension. As a first example, a very simple "hello world" style program is presented that stores the value 2 in a register and returns without doing anything else. The program is assembled using gas and linked with gcc to create a runnable executable. When run, the program exits with an error code of 2 as expected. Finally, a Makefile is suggested for simplifying building and running assembly programs going forward.

Uploaded by

König Löwe Pie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

THINK IN GEEK In geek we trust

ARM assembler in Raspberry Pi – Chapter 1 Calendar


January 2013
January 9, 2013 rferrer, 3
M T W T F S S

In my opinion, it is much more beneficial learning a high level language than a specific 1 2 3 4 5 6
architecture assembler. But I fancied learning some ARM assembler just for fun since I know 7 8 9 10 11 12 13
some 386 assembler. The idea is not to become a master but understand some of the details of
14 15 16 17 18 19 20
what happens underneath.
21 22 23 24 25 26 27

Introducing ARM
28 29 30 31

« Dec Feb »

You will see that my explanations do not aim at being very thorough when describing the Recent Posts
architecture. I will try to be pragmatic.
Capybara, pop up windows and the new PayPal
ARM is a 32-bit architecture that has a simple goal in mind: flexibility. While this is great for sandbox
integrators (as they have a lot of freedom when designing their hardware) it is not so good for
ARM assembler in Raspberry Pi – Chapter 12
system developers which have to cope with the differences in the ARM hardware. So in this text I
ARM assembler in Raspberry Pi – Chapter 11
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). ARM assembler in Raspberry Pi – Chapter 10

ARM assembler in Raspberry Pi – Chapter 9


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!
Recent Comments

Writing assembler rferrer on ARM assembler in Raspberry Pi –


Chapter 11

Einstieg in Pi-Assembler | ultramachine on ARM


Assembler language is just a thin syntax layer on top of the binary code. assembler in Raspberry Pi – Chapter 1

Binary code is what a computer can run. It is composed of instructions, that are encoded in a Fernando on ARM assembler in Raspberry Pi –
Chapter 7
binary representation (such encodings are documented in the ARM manuals). You could write
Loren Blaney on ARM assembler in Raspberry Pi –
binary code encoding instructions but that would be painstaking (besides some other
Chapter 11
technicalities related to Linux itself that we can happily ignore now).
ห.ร.ม. ด้วยภาษา Assembly บน Raspberry Pi |
So we will write assembler, ARM assembler. Since the computer cannot run assembler we have to Raspberry Pi Thailand on ARM assembler in
Raspberry Pi – Chapter 9
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.
Tags
The tool to do this is called as. In particular GNU Assembler, which is the assembler tool from the .net activerecord ajax apple archlinux
GNU project, sometimes it is also known as gas for this reason. This is the tool we will use to
assemble our programs. arm assembler bind

Just open an editor like vim, nano or emacs. Our assembler language files (called source files) branches c# firefox function
dhcp firebug

will have a suffix .s. I have no idea why it is .s but this is the usual convention. function call functions gadgets html
indexing modes ipod Java

Our first program javascript jquery linux mac os


mac os x MVC networking parallels pi
We have to start with something, so we will start with a ridiculously simple program which does
nothing but return an error code. programming tips rails
1 /* -- first.s */ raspberry ruby ruby
2 /* This is a comment */
3
4
.global main /* 'main' is our entry point and must be global */
.func main /* 'main' is a function */
on rails security sql software sports

5
server subversion tips and tricks tools
6 main: /* This is main */
7 mov r0, #2 /* Put a 2 inside the register r0 */ ubuntu visual studio xmonad
8 bx lr /* Return from main */
Archives

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

March 2013
To assemble the file type the following command (write what comes after $ ).
February 2013
1 $ as -o first.o first.s
January 2013

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

November 2012
1 $ gcc -o first first.o
August 2012

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

1 $ ./first June 2012

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

1 $ ./first ; echo $? November 2011


2 2
October 2011

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

June 2011
Since running the assembler and the linker soon becomes boring, I’d recommend you using the
May 2011
following Makefile file instead or a similar one.
April 2011
1 # Makefile
2 all: first March 2011
3
4 first: first.o February 2011
5 gcc -o $@ $+
December 2010
6
7 first.o : first.s
November 2010
8 as -o $@ $<
9 October 2009
10 clean:
11 rm -vf first *.o July 2009

June 2009

Well, what happened? March 2009

November 2008

July 2008
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 September 2007

and termination of the program for us. I will use this approach all the time. July 2007

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

1 /* -- first.s */
2 /* 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.

3 .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.

4 .func main /* 'main' is a function */

Another GNU assembler directive. Here we state that main is a function. This is important
because an assembler program usually contains instructions (i.e. code) but may also contain
data. We need to explicitly state that main actually refers to a function, because it is code.

6 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.

7 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).

8 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.

arm, assembler, pi, raspberry


Fast and easy way to block bots from your website using Apache
ARM assembler in Raspberry Pi – Chapter 2

3 thoughts on “ARM assembler in Raspberry Pi – Chapter 1”

Jonathan Hinchliffe says:


March 6, 2013 at 10:22 pm

Really excellent tutorial.

Reply

Fernando says:
March 21, 2013 at 12:15 am

You might also like