CS 352 Lecture 23 – Hello ARM, For Real

Dear students,

Let’s have a look at some ARM instructions. We’ll start with the simplest ones:

add a, b, c   // a = b + c
sub a, b, c   // a = b - c

Identifiers a, b, and c are just placeholders, not legal operands. In their place, we can either have registers or immediates (constants). ARM32 supports 16 registers (r0 through r15) and ARM64 supports 31 registers (x0 through x30). We’ll be focusing on the 32-bit version. Let’s take a peak at how instructions are encoded. We’ll focus only on the data bits for the time being:

31     20   19   16   15       12   11    0
......... | source1 | destination | source2

With only 4 bits dedicated to the source1 and destination operands, what can you determine about them? source2 appears to have 12 bits available, but four of these are reserved to rotate the value, so what can you say about the values that source2 may take on?

This instruction computes r0 = r1 + r2:

add r0, r1, r2

Now let’s look at another instruction for initializing a register with an immediate value:


Okay, let’s turn things back to you for a moment with a little Program This:

Write an ARM program to compute f = (g + h) - (i + j), where g is 7, h is 9, i is 3, and j is 8.

We’ll test out your solutions on a Raspberry Pi. Here’s a template for our assembly files:

.global main

  // do some work

  bx lr

The .text label specifies the beginning of the code segment of our program. When our program is run and expanded into RAM, the code will live in the lower-addresses of our process’ memory space. We can also have a .data segment for globals, amongst others. When you get a segfault, you are trying to access memory in a segment of your process that you are not allowed to access.

The bx instruction is an branch/jump back to the instruction after the call. Effectively, it’s a return without a value. The value is fed back through r0. In the case of main, what should our return value be?

If we have time, we’ll look at how to declare global data and call C functions.

See you next class!


P.S. Here’s the code we wrote together…


.global main

  mov r0, #17
  add r0, r0, r0

  bx lr


.global main

  push {lr}

  mov r0, #7
  add r0, r0, #9
  mov r1, #3
  add r1, r1, #8
  sub r0, r0, r1

  mov r1, r0
  ldr r0, =message 
  bl printf

  pop {lr}

  bx lr


  .ascii "Hello. Your answer is %d.\n


Leave a Reply

Your email address will not be published. Required fields are marked *