Post

2. ARM Assembly Basics

2. ARM Assembly Basics

Basic Instruction Format:

The basic format of an ARM instruction follows this structure:

1
2
// <operation> <destination>, <operand1>, <operand2>
ADD R0, R1, R2   // R0 = R1 + R2
  • Operation: The type of operation (e.g., ADD, SUB, MOV).
  • Destination: The register where the result will be stored (e.g., R0).
  • Operand1 and Operand2: The source operands for the operation (e.g., R1 and R2).

In ARM, many instructions have an optional condition at the beginning (e.g., EQ, NE, LT, GT) that specifies when the instruction should be executed based on the status flags in the CPSR register.

Example with condition:

1
ADDEQ R0, R1, R2   // ADD R1 and R2 if Equal (Z flag is set)

Data Types (.byte, .word, .ascii)

In ARM assembly, you use directives to declare and initialize data. These are not instructions but are used by the assembler to allocate space and initialize variables in memory.

.byte:

  • The .byte directive is used to allocate a byte (8 bits) of memory.
1
2
.byte 0x1F       // Declare a byte with value 0x1F
.byte 0x2A, 0x3C // Declare two bytes with values 0x2A and 0x3C

.word:

  • The .word directive allocates 4 bytes (32 bits) in memory. Typically used for integers or pointers.
1
.word 0x12345678   // Declare a 32-bit word with value 0x12345678

.ascii:

  • The .ascii directive is used to define a string of characters. The string is stored as an array of bytes.
1
.ascii "Hello, World!"   // Declare a string "Hello, World!"
  • Unlike .asciz, .ascii does not append a null terminator at the end of the string.

Labels, Directives, and Comments

Labels

  • A label is used to mark a location in the code. It is essentially a placeholder or target for jumps or branches.
  • A label consists of a name followed by a colon (:).

Example:

1
2
start:
    MOV R0, #10   // Initialize R0 with value 10
  • In this example, start: is a label that marks the beginning of the code block. You can use this label to jump to this part of the code using a branch instruction like B or BL.

Directives

Directives are special commands to the assembler and do not produce machine code. They are used to manage memory, allocate variables, and define constants, strings, etc. Some important directives include:

  • .global: Marks a symbol as global, making it accessible to other files (e.g., functions).

Example:

1
.global _start  // Define _start as a global symbol
  • .text: Specifies that the following code is part of the code section (executable).

Example:

1
.text   // Start of code section
  • .data: Specifies the beginning of the data section (used to define variables).

Example:

1
.data   // Start of data section
  • .bss: Marks uninitialized variables (usually initialized to zero at runtime).

Example:

1
2
.bss
buffer: .skip 128   // Reserve 128 bytes for 'buffer'

Comments

  • Comments in ARM assembly are preceded by a semicolon (@ or //).
  • Comments can be placed at the end of a line or on their own.

```s title:01.s .global _start // Make the ‘_start’ label accessible globally (entry point) .text // Indicate the start of the code section

_start: MOV R0, #10 // Load 10 into R0 (first operand for addition) MOV R1, #20 // Load 20 into R1 (second operand for addition) ADD R2, R0, R1 // Add R0 and R1, store the result in R2 (R2 = 10 + 20)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
MOV R3, #5         // Load 5 into R3 (third operand for multiplication)
MUL R4, R2, R3     // Multiply R2 (30) and R3 (5), store the result in R4 (R4 = 30 * 5)

// End of the program
MOV R7, #1         // Exit system call (Linux)
SWI 0              // Software interrupt to invoke the syscall (exit)

// Data section
.data              // Start of the data section my_number:
.word 0x12345678   // Declare a word (4 bytes) with a specific value

my_string:
.ascii "Hello, ARM!" // Declare an ASCII string without null terminator


.bss               // Begin uninitialized data section buffer: 
.skip 128          // Allocate 128 bytes for 'buffer' ```
This post is licensed under CC BY 4.0 by the author.