2 A First Assembly Program
In this section, we will write a very simple assembly program, which can be
used as a template for others, compile and link it, and execute it. To do so,
it is assumed that you have installed a Qemu ARM virtual machine, using the
instructions provided on my website.
3
Assembler languages only define instructions and hardware resources. To
drive the assembly and linking processes, we also need to use directives, which
are interpreted by the assembler. We will use the GNU Assembler (as), with
its specific directives.
2.1 Assembler Program Template
Here is the simplest program we can write in the ARM assembler language, one
that immediately ends, returning control to the caller.
1 .arm /* Specify this is an ARM 32 - bit program */
2 .text /* Start of the program code section */
3 .global main /* declares the main ident i fier */
4 .type main , % functi o n
5 main : /* Address of the main function */
6 /* Program code would go here */
7 BX LR /* Return to the caller */
8 .end /* End of the program */
The .arm directive specifies that this program is an ARM 32-bit assembly
code. The ARM architecture includes other types of programs (which we will
not see here), so this directive is needed for the Assembler to decide how to
assemble the program.
The .text directive indicates the start of a section of the assembly program
that contains code. Other sections could contain, e.g., the definition of global
data (i.e., global variables).
The .global main line specifies that main is a globally visible identifier,
which can be referred to from outside this file
4
This is necessary, because main
is the program entry point, and the corresponding function will be invoked from
outside the program!
.type main, %function specifies that the main identifier refers to a func-
tion. More precisely, it is a label indicating the address of the first instruction
of the main function.
main: BX LR is the first (and only) instruction of this program. It is prefixed
with a label, which allows to refer to the instruction address from other points
in the same compilation unit, or other units if, as in this case, the symbol main
is defined as a global identifier.
The .end directive specifies the end of the program.
2.2 Using the GNU Assembler
The GNU Assembler can be invoked as a standalone program, with a line such
as:
3
http://home.dei.polimi.it/agosta
4
More precisely, outside this compilation unit.
3