ARM assembly hello world tutorial

This tutorial will show the basics of how to create and build a simple hello world program written in ARM assembly language, for this tutorial I am using the VIM editor but any plain text editor will work since assembly has very simple syntax, aditional software required for this tutorial is an ARM assembler and linker, and also a Linux operating system.

Creating an new file

Create a new file called "hello.s", ARM assembler files use the extension of .s, then open it in your editor. we start by telling the assembler that the section of code we are writing contains the code using the directive ".text",
next we create the entry point for the program, the entry point is where execution will start. the default label name used to indentify the entry point is "_start" above this we add the directive ".global _start", the file should now look like this:

.text        
.global _start
_start:
        

Exiting

The file now has a entry point and could be assembled, but the program is missing a way to terminate properly. so we will use a system call to tell the operating system that we are done. system call codes are passed in register r7 and the number for SYS_EXIT is 1, to move small numbers directly into registers we use the "mov" instuction, here how "mov r7, #1", the pound sign in front of the number tells the assembler that it is a literal, numbers up to 255 can be used this way. next well call the software interrupt with the instuction "swi 0". the file should now look like this:

.text        
.global _start
_start:
    mov r7, #1
    swi 0        
        

Defining the data

Now we can define the data that the program will use. the data is stored in the ".data" section of the program. this section can be read and written to by the program. we start by creating a new label to indentify the string to display, let's call it message but it cound be called anything. this label referances the address of where the data is stored in memory. then we use the directive ".asciz" to tell the assembler that the data is a null terminated string.
After that create another label to store the length of the string, this is how it's done "len = .-message", the assembler will calculate the size of the string and store it at that label. The file should now look like this:

.text            
.global _start
_start:
    mov r7, #1
    swi 0

.data
message:
    .asciz "hello world\n"
len = .-message    
            

Displaying the text

Now we display the message that we defined. to display text to the console the system call SYS_WRITE is used, it is call number 4, SYS_WRITE needs some arguments such as where to display the message, the memory location and the string length. In register r0 we tell it to display the message to stdout using the code 1, then we load into r1 the memory address of the label used to indentify the string using the load into register instruction "ldr", this is how the ldr instruction is used "ldr r1, =message" the first argument is the destination register and the 2nd is the label name.
The next step is to load the label containing the length of the string into r2, this is how "ldr r2, =len", then we move into r7 SYS_WRITE which is number 4, the last step is to call the software interrupt using "swi 0", after that the text will be displayed. The file should now look like this:

.text            
.global _start
_start:
    mov r0, #1
    ldr r1, =message
    ldr r2, =len
    mov r7, #4
    swi 0

    mov r7, #1
    swi 0

.data
message:
    .asciz "hello world\n"
len = .-message                  
            

Building the program

We are now done writing the program and can build and run it, first assemble it using an assembler, this will produce an object file from the assembly code. we will use the assembler as but you might have to use the ARM assembler arm-linux-gnueabihf-as, here is how the assembler is used "as hello.s -o hello.o", then we need to link the object file which it produces using the linker ld using similar syntax "ld hello.o -o hello", this will create an ELF executable which we can run. It will be outputed to a file named hello but if you do not give it a output name it would be outputed to a.out.
To run the executable call the command ./hello, the program is now complete.

 

If you have any questions or comments please add a comment bellow.

Comments