DEV Community

Cover image for Hello World! in ARM ISAs
Tomas Sirio
Tomas Sirio

Posted on

Hello World! in ARM ISAs

Hey there!

Here is a second chapter on this ASM's series. This time, I'm bringing you a similar post to the last one, only this time we are changing the ISA.

You may be wondering, ARM? ISA? what's going on here?

Let's go step by step.

❓ What is ARM?

ARM is a type of processors licensed by a Cambridge company named Acorn RISC Machine (ARM). The main differences between an ARM processor and an Intel processor for example are two.

The first one is that ARM doesn't manufacture it's own processors, they mostly design and create Licenses to their technology and sell it as Intellectual property, unlike Intel which designs, manufactures and sell their processors

The other main difference is in their design philosophy. Intel fabricates CISC processors while ARM designs RISC processors. I'm planning to delve a little bit further on CISC vs RISC in another post, however here is a brief explanation about the differences:

  • RISC: Reduced Instruction Set Computer
  • CISC: Complex Instruction Set Computing

RISC basically is composed of as few instructions as possible which makes it easier for the developer to learn how to program on that ISA while CISC has a ton of functions (or instructions) built hardware-style on the processor. The basic advantage CISC has over RISC is that it consumes less memory on it's instructions execution. RISC however is far easier to understand.

Alt Text

❓ What is an ISA?

An ISA is the Instruction Set Architecture which the processor provides to the user in order to let him/her communicate with the processor.

Everything must be clicking in concept by now, so let's move on. (If it's not clear by now, it's okay, it's a really complicated matter)

Alt Text

🎉 Try it yourself!

If you want to try this example, you are going to need either an ARM emulator, a Raspberry Pi, an Android phone, or whatever machine with an ARM processor that you possess. If you want to try an emulator, you can give this one a go

🌎 Hello World!

If you haven't read my previous post on this series (Which you totally should by now), an ASM file is composed of various sections where the variables are defined and/or initialized and a .text section where the code is declared.

msg: .ascii      "Hello World!\n"
len = . - msg           /* This means: take your Stack Pointer actual position and subtract the size of the msg variable */
Enter fullscreen mode Exit fullscreen mode

Just like last time, we define a variable msg where our message will be declared (duh) and a len variable which will store the length of the message (this is extremely necessary on ASM programming since the Write syscall needs the message length as it's the third parameter)


.globl _start           /* We are declaring _start as a global function for the machine */
Enter fullscreen mode Exit fullscreen mode

The next step is to define the .text section in which our code will be written. We are also telling the program that the _start function is going to be a global function. This is telling the computer: Hey!, you can use this function wherever you want from now on.

  /* Printing the message */
    mov     %r0, $1     /* We set the number 1 on r0, which will represent STDOUT */
    ldr     %r1, =msg   /* ldr loads a 32 bit constant on the dst register. In this case r1 */
    ldr     %r2, =len   /* In r2 we are loading the length of the message */
    mov     %r7, $4     /* ARM processors use r7 for the syscalls. Syscall 4 is 'write' */
    swi     $0          /* Software interrupt. AKA syscall */
Enter fullscreen mode Exit fullscreen mode

This section, which seems really intimidating, is just passing the parameters needed for the Write Syscall on Linux. Let's remember what that syscall needs on it's signature (This means, let's look on Wikipedia):

  1. The file code (file descriptor or fd).
  2. The pointer to a buffer where the data is stored (buf).
  3. The number of bytes to write from the buffer (nbytes).

Really straightforward. On r0 we are using the STDOUT as our file code. r1 will take the message string while r2 loads the string's length. ARM uses r7 to get the syscall which will be used. In this case, it's SYSCALL 4 (write). Lastly, we use swi (SoftWare Interrupt) to call Write.

    /* Now we need to exit the program */
    mov     %r0, $0     /* We are going to return a 0 because the program was ran correctly */
    mov     %r7, $1     /* Again a syscall. This time Syscall 1 is 'exit' */
    swi     $0          /* invoke syscall */
Enter fullscreen mode Exit fullscreen mode

Our last step is to exit the program with another syscall. This time is Syscall 1 (exit) which only get's one parameter as it's return code.

We need to run this program now. Let's compile it then. I saved the file as helloWorld.S

arm-linux-gnueabihf-as -o helloWorld.o helloWorld.S
arm-linux-gnueabihf-ld -s -o helloWorld helloWorld.o

Enter fullscreen mode Exit fullscreen mode

So, we created our .o object with the first command and then we link it on the second one while creating an executable named helloWorld which we run on our third line. You should get a lovely Hello World! message if everything is ok.

Alt Text

As always, I'll leave you with the repository so you can check it out

Top comments (0)