Lesson 6 of 28

Data Sizes

Working with Different Data Sizes

Real-world data comes in different sizes. ARM64 provides load and store instructions for each size, just like Starfleet has different vessel classes for different missions -- from 8-bit shuttlecraft to 64-bit Galaxy-class starships.

SizeBitsLoadStoreDirective
Byte8LDRBSTRB.byte
Halfword16LDRHSTRH.hword
Word32LDR WSTR W.word
Doubleword64LDR XSTR X.quad

LDRH and STRH -- Halfword (16-bit)

LDRH loads a 16-bit value from memory, zero-extending it to fill the register. STRH stores the lower 16 bits of a register:

LDRH W0, [X1]         // Load 16-bit value from [X1] into W0
STRH W0, [X1, #2]     // Store low 16 bits of W0 to [X1+2]

Halfwords are 2 bytes, so consecutive halfwords are spaced 2 bytes apart.

The .hword Directive

The .hword directive stores 16-bit values (2 bytes each) in the data section:

.data
values:
    .hword 100, 200, 300    // Three 16-bit values

Each .hword value occupies 2 bytes. You access them using LDRH:

LDR X0, =values
LDRH W1, [X0]          // W1 = 100
LDRH W2, [X0, #2]      // W2 = 200 (2 bytes later)
LDRH W3, [X0, #4]      // W3 = 300 (4 bytes later)

The .word Directive

The .word directive stores 32-bit values (4 bytes each):

.data
counts:
    .word 100000, 200000    // Two 32-bit values

Each .word value occupies 4 bytes. You access them using W registers:

LDR X0, =counts
LDR W1, [X0]          // W1 = 100000
LDR W2, [X0, #4]      // W2 = 200000 (4 bytes later)

Zero Extension vs Sign Extension

When loading a value smaller than 64 bits, the upper bits must be filled somehow:

  • Zero extension (LDRB, LDRH, LDR W): Upper bits are set to 0. Used for unsigned values.
  • Sign extension (LDRSW): The sign bit is copied into the upper bits. Used for signed values.
// If memory at [X1] contains 0xFF80 (65408 unsigned, -128 signed):
LDRH W0, [X1]         // W0 = 0x0000FF80 (zero-extended, positive)
// LDRSW sign-extends 32-bit values to 64 bits.

Practical Use Case: Summing an Array

Arrays of 16-bit values are common in audio processing, sensor data, and network protocols. To sum 16-bit values:

LDR X0, =halfwords    // pointer to array
MOV X1, #0            // sum = 0
MOV X2, #4            // count = 4
loop:
    CBZ X2, done
    LDRH W3, [X0], #2 // load halfword, advance pointer by 2
    ADD X1, X1, X3    // sum += value
    SUB X2, X2, #1
    B loop
done:

Your Task

An array of four 16-bit halfword values is defined: 100, 200, 300, 400. Load each value using LDRH, sum them (the total is 1000), and print the result followed by a newline.

Hint: You will need to convert a 4-digit number (1000) to ASCII. Extract each digit by dividing by 1000, 100, 10, and using the remainder.

ARM64 runtime loading...
Loading...
Click "Run" to execute your code.