We use byte, halfword, and word access to read or write different sizes of data in memory. This helps the processor handle data efficiently and correctly.
Byte, halfword, and word access in ARM Architecture
LDRB Rt, [Rn] ; Load byte LDRH Rt, [Rn] ; Load halfword LDR Rt, [Rn] ; Load word STRB Rt, [Rn] ; Store byte STRH Rt, [Rn] ; Store halfword STR Rt, [Rn] ; Store word
Rt is the register to load data into or store data from.
Rn is the base register holding the memory address.
LDRB R0, [R1]
LDRH R2, [R3]
LDR R4, [R5]
STRB R6, [R7]
This program loads a byte, halfword, and word from a data area, then stores them back at different offsets. It shows how to access different sizes of data in memory.
; Example: Load and store different data sizes AREA Example, CODE, READONLY ENTRY LDR R1, =Data ; Load address of Data LDRB R0, [R1] ; Load byte from Data into R0 LDRH R2, [R1, #1] ; Load halfword from Data+1 into R2 LDR R3, [R1, #3] ; Load word from Data+3 into R3 STRB R0, [R1, #7] ; Store byte from R0 into Data+7 STRH R2, [R1, #8] ; Store halfword from R2 into Data+8 STR R3, [R1, #10] ; Store word from R3 into Data+10 END Data DCB 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0x00, 0x00, 0x00, 0x00, 0x00
Byte access reads or writes 1 byte (8 bits) of data.
Halfword access reads or writes 2 bytes (16 bits) of data and must be aligned to even addresses.
Word access reads or writes 4 bytes (32 bits) of data and must be aligned to addresses divisible by 4.
Byte, halfword, and word access let you handle different data sizes in memory.
Use the correct instruction to match the data size you want to read or write.
Alignment matters: halfwords and words must be stored at addresses divisible by 2 and 4 respectively.
Practice
halfword represent in ARM architecture?Solution
Step 1: Understand data size terms in ARM
In ARM, a byte is 8 bits, a halfword is twice that size, and a word is 32 bits.Step 2: Identify halfword size
A halfword is 16 bits because it is two bytes (2 x 8 bits = 16 bits).Final Answer:
16 bits -> Option DQuick Check:
Halfword = 16 bits [OK]
- Confusing halfword with byte size
- Thinking halfword is 32 bits
- Mixing halfword with word size
Solution
Step 1: Identify load instructions for different sizes
In ARM,LDRBloads a byte,LDRHloads a halfword, andLDRloads a word.Step 2: Choose instruction for loading a byte
LDRB R0, [R1]loads a byte from memory address in R1 into R0.Final Answer:
LDRB R0, [R1] -> Option CQuick Check:
LDRB = Load Byte [OK]
- Using LDR instead of LDRB for byte load
- Confusing STRB (store) with load instructions
- Using LDRH which loads 16 bits, not 8
LDR R0, [R1] LDRH R2, [R1, #2] LDRB R3, [R1, #1]
If
R1 points to memory starting with bytes 0x12 0x34 0x56 0x78, what will be the value loaded into R2?Solution
Step 1: Understand memory layout and addressing
Memory bytes at R1: 0x12 (offset 0), 0x34 (offset 1), 0x56 (offset 2), 0x78 (offset 3). ARM is little-endian, so lower address holds least significant byte.Step 2: Calculate halfword loaded at offset #2
Halfword at offset 2 includes bytes at offsets 2 and 3: 0x56 (LSB), 0x78 (MSB). So value is 0x5678.Final Answer:
0x5678 -> Option BQuick Check:
Halfword at offset 2 = 0x5678 [OK]
- Reading bytes in wrong order (big-endian)
- Using wrong offset for halfword
- Confusing byte and halfword sizes
LDRH R0, [R1]. What is the likely problem?Solution
Step 1: Understand alignment requirements in ARM
Halfword accesses must be aligned to even addresses (multiples of 2). Loading from odd addresses causes alignment faults or unpredictable results.Step 2: Identify behavior on misaligned halfword load
ARM architecture typically raises an alignment fault or behaves unpredictably if halfword is loaded from an odd address.Final Answer:
Alignment fault or unpredictable behavior -> Option AQuick Check:
Halfword must be even address [OK]
- Assuming automatic address adjustment
- Thinking halfword loads from any address are safe
- Confusing halfword with byte alignment
Solution
Step 1: Understand alignment restrictions for word loads
ARM requires word (32-bit) loads to be aligned to 4-byte boundaries. Unaligned word loads cause faults or incorrect data.Step 2: Choose method to safely load unaligned word
Loading four bytes individually withLDRBand combining them in the register avoids alignment faults and correctly reconstructs the word.Final Answer:
Use four byte loads (LDRB) and combine them manually in the register -> Option AQuick Check:
Unaligned word load = combine bytes manually [OK]
- Using single LDR on unaligned address causing faults
- Assuming LDRH twice works for unaligned word
- Misusing STR which stores, not loads
Ready to prove it?
Pass the test and this pattern moves unknown
