Memory Layout of C Program on ARM Processor
Memory Layout of C Program on ARM Processor
Lecture 11:
Memory map of C programs
ARM Programmer’s Model
Diba Mirza
University of California, San Diego
1
Typical ARM Memory Map
0xFFFFFFFC
OS and Memory-Mapped IO
Dynamic Data
BSS
Data
Text
0x0000000
Exception Handlers 2
Program Memory Map
• “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
3
Viewing the memory map with ‘size’
• You can use the size command to check the
memory map of your executable
mem.c:
void foo() {
}
4
Checking the memory map
void foo() {
int i=10; //line 1
}
Output of size before adding line 1
Stack
Heap
Data
PC:0x4000 Memory
CPU Instructions
9
The ARM Register Set
r0 Registers: (Very) Small amount of memory inside the CPU
r1 Each ARM register is 32 bits wide
r2 30 general purpose registers
r3 6 status registers
r4
1 program counter
r5
r6
r7
r8
r9
r10 General Purpose Registers
r11
r12
r13 (sp)
r14 (lr) Special Purpose Registers
r15 (pc)
cpsr
10
ARM Assembly Variables: Registers
Unlike HLL like C or Java, assembly cannot use variables
Why not? Keep Hardware Simple
Data is put into a register before it is used for arithmetic, tested, etc.
Result is stored in a register (later stored to memory)
Benefit: Since registers are directly in hardware, they are very fast
In C (and most High Level Languages) variables declared first and given a
type
Example:
int fahr, celsius;
char a, b, c, d, e;
Each variable can ONLY represent a value of the type it was declared as
(cannot mix and match int and char variables)
11
PA2 ARM Skeleton code
.syntax unified
.text
.align 8
.global get_min_ARM
.func get_min_ARM, get_min_ARM
.type get_min_ARM, %function
get_min_ARM:
@ Save caller's registers on the stack
push {r4-r11, ip, lr}
13
Arithmetic Instructions
In C:
a=b+c;
In ARM:
ADD r0, r1, r2
14
Specifying constants in Arithmetic
Instructions
In C:
a=b+10;
In ARM:
ADD r0, r1, #10
16
Assignment Instructions
In C: In ARM:
a=b;
a=10;
17
Data transfer (memory) Instructions
18
Base Register Addressing Mode
The memory location to be accessed is held in a base register
STR r0, [r1] @Store contents of r0 to location pointed to
@ by contents of r1.
LDR r2, [r1] @Load r2 with contents of memory location
@pointed to by contents of r1.
r0 Memory
Source 0x5
Register
for STR
r1 r2
Base Destination
0x200 0x200 0x5
Register Register
for LDR
Data Transfer Instructions
In C:
void foo (int *p){
*p=10;
}
In ARM:
20
Data Transfer Instructions
In C:
void foo (int *p){
int a=*p;
}
In ARM:
21