ARM Memory Map / Basic Instructions
ARM Memory Map
Section (high address to low address) | Function |
---|---|
OS and Memory-Mapped IO | |
Dynamic Data | Stack and heap |
BSS | Uninitialized global data Zero-initialized global data Static data |
Data | Initialized global data |
Text | Machine code (read only) |
Exception Handlers |
- “Text” (instructions in machine language)
- “Data” contains any global or static variables which have a pre-defined value and can be modified. That is any variables that are not defined within a function (and thus can be accessed from anywhere) or are defined in a function but are defined as static so they retain their value across subsequent calls.
- “BSS” also known as uninitialized data, is usually adjacent to the data segment. The BSS segment contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code.
- “Heap” (for dynamically allocated data)
- “Stack” (for function local variables)
- Heap and stack change in size as the program executes.
View the memory map using “size”
$ gcc -O foo.c -c
$ size foo.o
text data bss dec hex filename
52 0 0 52 34 foo.o
Program Execution
Interactions between CPU, registers, and memory. Each instruction (4 bytes) handles the data from registers (more frequently) and memory.
Registers
Each ARM register is 32 bits wide. There are 30 general purpose registers (6 status registers, 1 program counter).
r0 to r12, general
r13, stack pointer,
r14, subroutine Link Register
r15, program counter
Basic Type of Instructions
- Arithmetic (involves only CPU and registers)
Compute the sum (or difference) of two registers, and store the value in a register.
Move the contents of one register to another
- Memory Instructions (transfer data between memory and registers)
- Control Transfer Instructions: Change flow of execution
In C: a = b + 10; In ARM: ADD r0, r1, #10
#10 is an immediate (constant), which has fixed length of 4 bytes.
Assignment Instructions
In C: a = b; // a and b are integers a = 10; In ARM: mov r0, r1 @r0 = r1
Data Transfer
Separate instructions to transfer data between registers and memory:
- Memory to register (LOAD)
- Register to memory (STORE)
Load/store Syntax
LDR r0, [r1]
- r1 must be a register that contains a valid memory location (as a pointer).
- Copy 4 bytes from the location pointed by r1 into r0
- Equivalent to r0 = * r1; (in C)
STR r0, [r1]
- r1 stores the address of destination memory.
- Store contents of r0 to location pointed by r1.
- *r1 = r0; (in C)
In Little Endian machines (ARM and Intel x86), the least significant byte (like 5 in 0x00000005) goes into the lowest memory address (like 0x200 of 0x200 to 0x0203).
Variations on LDR/STR
LOAD
LDRH r0, [r1] @ r1 points to 2 bytes (unsigned)
- Fill the significant bits with zeros.
- Treat content pointed by r1 as unsigned.
LDRSH r0, [r1] @ r1 points to 2 bytes (signed)
- Treat content pointed by r1 as signed.
- Identify if the most significant bit is 0 or 1 (positive or negative) and do sign expansion.
- Fill F’s in the spare bits.
LDRB r0, [r1] @ r1 points to 1 byte (unsigned)
LDRSB r0, [r1] @ r1 points to 1 byte (signed)
STORE
STR r0, [r1] @ store 4 bytes STRH r0, [r1] @ store least significant 2 bytes STRB r0, [r1] @ store least significant byte
There’s no STRSH or STRSB, since the two bytes or one byte can be signed or unsigned. Raw data in r0 ought not be changed.
Published on October 19, 2015