Compile By Hand
Basic arithmetic
g = h + A[8]; // r0 stores A array; r1 stores h; r2 to store g.
ADD r0, r0, #32 LDR r3, [r0] @ r0 = &A + 8 * 4, since int is of 4 bytes.
LDR r3, [r0, #32]
Accessing arrays
void foo (int *p, int size){ // p stored in r2, size stored in r1 *p = 0; *(p + 1) = 0; *(p + size - 1) = 0; }
foo: MOV r2, #0; @ store 0 in *p STR r2, [r0] @ *p = 0 MOV r3, #4 ADD r4, r0, r3 @ p + 1 in C STR r2, [r4] @ store 0 in *(p + 1) SUB r1, r1, #1 @ r1 = size - 1 MUL r4, r1, r3 @ r4 = r1 * r3 STR r2, [r0, r4] @ store 0 in *(p + size - 1)
Data transfer instructions
void swap (int *x, int *y){ // x stored in r0, y stored in r1 int tmp = *x; *x = *y; *y = tmp; }
swap: LDR r2, [r0] @ Load *x from memory and store in r2 LDR r3, [r1] @ Load *y from memory and store in r3 STR r3, [r0] @ Store r3 in register to *x in memory MOV r2, [r1] @ Store r2 (tmp) in register to *y in memory
- For short, LDR -> LDRSH, STR -> STRSH
- For unsigned short, LDR -> LDRH, STR -> STRH
Linked lists
- head -> *next -> *next -> … -> value
head -> value = 100; head -> *next -> value = 100; // r0 stores the first value
MOV r1, #100 STR r1, [r0] @ store to memory ... LDR r3, [r0, #4] @ store 100 to next value STR r1, [r3]Published on October 26, 2015