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, 2015Struct
A struct is a data structure composed of simpler data types.
There is no methods or inheritance.
struct point { int x; int y; }
main(){ struct point p1; // get memory allocated for two int p1.x = 5; p2.y = 10; }
void printPoint(struct point p){ printf("(%d,%d)", p.x, p.y); }
struct point *p; p = malloc(sizeof(struct point)); printf("x is %d\n",(*p).x); printf("x is %d\n", p->x);Published on October 16, 2015
Dangling Pointers & Memory Leaks
Dangling pointer
Pointer points to a memory location that no longer exists.
Memory Leaks
Memory allocated by a program is not freed.
The program is particularly acute if memory allocated in heap can no longer be accessed.
void foo(){ int * p = malloc(8); p = NULL; /* memory previously pointed by p can never * be freed. */ }
int * f2(int null){ int mem2[num]; return mem2; } // DANGLING POINTER // never return the address of a local variable.Published on October 16, 2015
Dynamic Memory Allocation
malloc
void * malloc (size_t s) //size_t defined in stdlib.h
- Allocate a block of ‘s’ on the heap and return the starting address of that block, return a void pointer
- Return NULL if memory can’t be allocated.
int * arr = malloc(5 * sizeof(int)); //allocate memory for an int array of size 5
calloc
calloc() works like malloc, but initializes the memory to zero if possible. The prototype is:
void *calloc(size_t num, size_t size); // equivalent to malloc() void *malloc(size_t num * size)
Num is the number of elements, and size the byte size of each element.
realloc
void* realloc (void * p, size_t s);
Extend or shrink the block pointed by p to s bytes
Safe instructions to use realloc
int *tmp; if ((tmp = realloc(ip, sizeof(int) * (INITIAL_ARRAY_SIZE + 5))) == NULL) { fprintf(stderr, "ERROR: realloc failed"); } ip = tmp;
If realloc fails to extend memory pointed by ptr, ptr will point to NULL, and its previous allocated memory will never be accessed (memory leak).
bzero
bzero(3)
fills the first n bytes of the pointer to zero. Prototype:
void bzero(void *s, size_t n);
memset
To set the value to some other value (or just as a general alternative to bzero).
void *memset(void *s, int c, size_t n);
Specify c as the value to fill for n bytes of pointer s.
free
void free(void * ptr)
Free the heap block pointed by p
int *p = malloc(8); // allocated 8 bytes to p free(p); free(p); //program crashes [runtime error]
After freeing a pointer, should point it to NULL.
Published on October 16, 2015Protected: Download Mirror
Published on July 11, 2015
A Cultural Project
Here I made a cultural project about 1992 Los Angeles riots.
Purpose
This project was designed to recall the history in the perspectives from different ethnic groups, with a mainstream media timeline for comparison. The map can you walk you through the event in a flat LA space layout.
To see LA Times timeline for comparison: Click here
Map Comparison
The two maps above indicating racial and economic dispersion of LA can be compared with the map provided by this site. The violence of the riots in 1992 happened most in areas of the non-white, low-income households.
References
Images:
- AP Images: Click here
- LA Times: LA Times Timeline Photos
- Other websites: stolinsky.com
Videos:
- YouTube (links embedded in player)
- LA Times: LA Times Timeline
Maps:
The maps uses Google Maps data with customized markers. Coordinates are parsed manually.
Texts:
Most texts are hand-written. There are textual explanations from the following sources:
- AP Images: Click here
- LA Times: LA Times Timeline
- Wikipedia: Click here
- Anna Smith’s Twilight Los Angeles, 1992
You Have An Accent!
Yes, I have an accent. Everybody has. If you are told you have an accent, that means they way what you speak is different to most people in this area do.
In a university, talking to people from everywhere, I find it easy to identify an accent and thus have an idea of where he or she comes from. Nevertheless, most people don’t, because they are not careful enough. If you can distinguish Indian English or Japanese English, it is not hard to tell the difference among America’s mid-west, California, and the Eastern coast. It is also not a problem to pinpoint which part of UK a person comes from, Scotland, Yorkshire, London, or London’s east-end. The fact is, listen.
Accents shift frequently. I can speak many English accents in different contexts. The variation is not exclusive to English. People in China typically have two accents, the standard Mandarin and their own dialect.
Language and its accents are intersting objects as they carry information about the speakers’ whereabouts and enrich an distinct local culture. As people migrate, accents are also an intangible mark they take with them.
Published on May 6, 2015Welcome
Welcome! This place is for some valuable codes.
Published on May 4, 2015