MarkdownBlog Main Website All posts

2024-05-16

Your first ARM Assembly Program


This is a simple guide to writing your first ARM assembly program for linux. This guide will show you how to write a simple program that will print Hello World to the console.

Installing required packages

You will need to install the following packages:

sudo apt-get update
sudo apt-get install build-essential
			

These packages are required to compile the program.

Writing the program

Open a text editor like vim or nano and create a new file called hello.s:

nano hello.s
			

Or if you prefer vim:

vim hello.s
			

This should put you in a text editor. Copy the following into it:

.section .data
    msg:
        .ascii "Hello world\n"
        len = . - msg

.section .text
    .globl _start

_start:
    mov x0, 1
    ldr x1, =msg
    ldr x2, =len
    mov x8, 64
    svc 0
    mov x8, 93
    mov x0, 0
    svc 0
			

Then save the file.

Compiling the program

To compile our program we are going to use as ans ld. Run the following commands:

as -o hello.o hello.s
ld -o hello hello.o
			

This will create an executable called hello.

Running the program

To run the program, simply run:

./hello
			

You should see Hello world printed to the console.

Explanation

The program is divided into two sections: .data and .text. The .data section contains the string Hello world\n and the length of the string. The .text section contains the _start label which is the entry point of the program.

.section .data
    msg:
        .ascii "Hello world\n"
        len = . - msg
			

The _start label is the entry point of the program. and it is defined as a global symbol using the .globl directive.

.section .text
    .globl _start
			

In the _start section, we have the program instructions.

_start:
    mov x0, 1       // Set the file descriptor for stdout
    ldr x1, =msg    // Load the address of the message into x1 (parameter 1 for stdout)
    ldr x2, =len    // Load the length of the message into x2  (parameter 2 for stdout)
    mov x8, 64      // Set the syscall number for write
    svc 0           // Call the kernel
    mov x8, 93      // Set the syscall number for exit
    mov x0, 0       // Set the exit status (0 for success)
    svc 0           // Call the kernel
			

See also

References

Author

Made with by 0x4248