ARM Assembly language tutorial
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.
If everything goes as expected you will get a first file. This is your program. Run it.
It should do nothing. Yes, it is a bit disappointing, but it actually does something. Get its error code this time.
|
1 2 |
$ ./first ; echo $? 2 |
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.
|
1 2 3 4 5 6 7 8 9 10 11 |
# Makefile all: first first: first.o gcc -o $@ $+ first.o : first.s as -o $@ $< clean: 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.
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.
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.
You might also like


|
LWW ipazzport KP-810-19 Germany + English Language Mini Wireless 2.4GHz 84-Keys Keyboard - Black PC Accessory (LWW Computers)
|
|
C Programming with The Programmable Box (Orange-Kit) Software (Your Inner Geek, LLC)
|





The AVR is a modified Harvard architecture 8-bit RISC single chip microcontroller which was developed by Atmel in 1996. The AVR was one of the first microcontroller families to use on-chip flash memory for program storage, as opposed to one-time programmable ROM, EPROM, or EEPROM used by other microcontrollers at the time.