CodemancerDotCoDotUK

ARM Memory Map / Basic Instructions

 ARM Memory Map

Section
(high address to low address)
Function
OS and Memory-Mapped IO
Dynamic DataStack and heap
BSSUninitialized global data
Zero-initialized global data
Static data
DataInitialized global data
TextMachine code (read only)
Exception Handlers

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

Compute the sum (or difference) of two registers, and store the value in a register.

Move the contents of one register to another

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:

Load/store Syntax

LDR r0, [r1]
STR r0, [r1]

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)
LDRSH r0, [r1] @ r1 points to 2 bytes (signed)
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

Struct

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
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, 2015

Protected: Download Mirror

This content is password protected. To view it please enter your password below:

Published on July 11, 2015

A Cultural Project

Here I made a cultural project about 1992 Los Angeles riots.

Capstone Project

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

Map of Los Angeles by Average Household Income

Map of Los Angeles by Average Household Income

Racial Dot Map In LA Highlights Segregation By Neighborhood

Racial Dot Map In LA Highlights Segregation By Neighborhood

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:

Videos:

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:

Published on May 20, 2015

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, 2015

Welcome

Welcome! This place is for some valuable codes.

Published on May 4, 2015