ARM Memory
Sections Programs = Code Data
Sections Compiled program stored in sections .text : Executable code read/executable .rodata : Read Only Data read .data : Data read/write .bss : Uninitialized Data read/write
Sections Compile C++
Identifiers Identifier: Placed at name the next location (rest or or next line)
Data Directives .word X Store 32 bit value X in memory .space X Reserve X bytes in memory
Sections Sections placed in memory for execution:
Simplified .data Things to place into memory after code .text Code
Same Segment Access Can place data in .text Make sure doesn’t get executed!!!
Same Segment Access LDR : Load Register LDR rd, identifier Pseudo instruction LDR rd, identifier Load value at identifier
Same Segment Access Fun Fact LDR : Calculates location as currentLocation + 8 + immediate 1000 + 8 + C (810 + 1210) 1000 + 14 (2010) 1014
Same Segment Access Fun Fact LDR : Calculates location as currentLocation + 8 + immediate 1000 + 8 + 64 (810 + 10010) 1000 + 6C (10810) 106C
LDR Immediate LDR rd, =longConstant Puts immediate at end of .text Automatically loads that address
Cross Segment Access Can not directly load identifier in other segment May be too far
Cross Segment Access LDR rd, =identifier Load address of identifier
Cross Segment Access LDR rd, =identifier Load address of identifier
How It Works LDR rd, =identifier Stores address of target at end of current .text Calculates location as current + 8 + offset 1000 + 8 + C (810 + 1210) 1000 + 14 (2010) 1014 Loads real address from that location
Cross Segment Access LDR rd, [rs] Load value at the address stored in rs
Cross Segment Access LDR rd, [rs] : always second step First load address into rs Then load data from that address
STR STR rs, [rd] Store value from rs to address in rd NOTE: Reverse order of operands
STR Notes Process: Load address in register Store other register to that address
Memory Address Word = 32 bits = 4 bytes Word addresses = multiples of 4 C = 1210 10 = 1610 14 = 2010
Memory Address Bytes in word numbered right to left 1000 = 64 aka “Little Endian” 1000 = 64 1001 = FF 1003 = 00 1012 = 81 +3 +2 +1 +0
Memory Address Bytes in word numbered right to left 1000 = 64 1005 = 89 100F = 81 1010 = 81 +11 +10 +9 +8 +15 +14 +13 +12 +3 +2 +1 +0 +7 +6 +5 +4
Other Memory Directives .byte : Store 8 bit value .hword : Store 16 bit value (half-word)
Alignment .word should be at multiple of 4 .hword should be at multiple of 2 .byte anywhere BAD Alignment: D = 13!!!
Align .align : pad with 0’s until next word boundary
Alignment Tips Load/Store to unaligned address = silent failure! Unneeded .align’s ignored When in doubt, .align
Byte Packing Byte packing : ordering class/struct members for proper alignment
Load/Store Bytes LDRB rd, [rs] STRB rs, [rd] Load one byte from address rs into rd STRB rs, [rd] Store value in rs to address in rd
Sign Issues 34 + -1 = 289???? -1 as byte = 0xFF 0xFF as 32 bit value = 255 34 + -1 = 289????
Sign Extension LDRSB rd, [rs] Load one byte from address rs into rd; extend sign bit
LDR Variants LDR rd, identifier LDR rd, [rs] LDR rd, =immediate Load value at identifier; same section only Load value at address in rs LDR r3, [r2] LDR r1, x LDR rd, =immediate LDR rd, =identifier Put constant into .text; Load that constant Load address of identifier LDR r2, =x LDR r3, =0x1234ABCD