Think In Geek

In geek we trust

# ARM assembler in Raspberry Pi – Chapter 2

## Registers

At its core, a processor in a computer is nothing but a powerful calculator. Calculations can only be carried using values stored in very tiny memories called registers. The ARM processor in a Raspberry Pi has 16 integer registers and 32 floating point registers. A processor uses these registers to perform integer computations and floating point computations, respectively. We will put floating registers aside for now and eventually we will get back to them in a future installment. Let’s focus on the integer registers.

Those 16 integer registers in ARM have names from `r0` to `r15`. They can hold 32 bits. Of course these 32 bits can encode whatever you want. That said, it is convenient to represent integers in two’s complement as there are instructions which perform computations assuming this encoding. So from now, except noted, we will assume our registers contain integer values encoded in two’s complement.

Not all registers from `r0` to `r15` are used equally but we will not care about this for now. Just assume what we do is “OK”.

## Basic arithmetic

Almost every processor can do some basic arithmetic computations using the integer registers. So do ARM processors. You can `ADD` two registers. Let’s retake our example from the first chapter.

```1 2 3 4 5 6 7 8 /* -- sum01.s */ .global main   main: mov r1, #3 /* r1 ← 3 */ mov r2, #4 /* r2 ← 4 */ add r0, r1, r2 /* r0 ← r1 + r2 */ bx lr```

If we compile and run this program, the error code is, as expected, 7.

```\$ ./sum01 ; echo \$?
7
```

Nothing prevents us to reuse `r0` in a more clever way.

```1 2 3 4 5 6 7 8 /* -- sum02.s */ .global main   main: mov r0, #3 /* r0 ← 3 */ mov r1, #4 /* r1 ← 4 */ add r0, r0, r1 /* r0 ← r0 + r1 */ bx lr```

Which behaves as expected.

```\$ ./sum02 ; echo \$?
7
```

That’s all for today.

### 4 thoughts on “ARM assembler in Raspberry Pi – Chapter 2”

• smart guide … go ahead …

• Andreas Gwilt says:

Can’t you just say `add r0, r1` ?

• rferrer says:

Yes, although I think would be less clear for novices.

Kind regards,

• SaberWriter says:

This continues to be a great series of articles. Thanks again for taking the time to write them. Great stuff.