Loads, Stores, Alignment & Offsets
The previous lesson used full-width i32.load and i32.store. WebAssembly also has narrow accesses for
bytes and 16-bit values, and every memory instruction takes two optional immediates: an offset and an
alignment hint.
The full family
| Load | Store | Bytes | Notes |
|---|---|---|---|
i32.load / i64.load / f32.load / f64.load |
….store |
4 / 8 / 4 / 8 | full width |
i32.load8_s i32.load8_u |
i32.store8 |
1 | _s sign-extends, _u zero-extends |
i32.load16_s i32.load16_u |
i32.store16 |
2 | |
i64.load8_s/_u i64.load16_s/_u i64.load32_s/_u |
i64.store8/16/32 |
1/2/4 |
Narrow stores keep only the low bits of the value. Narrow loads must say how to fill the rest of the
register: sign-extend (_s) or zero-extend (_u).
(module
(import "console" "log" (func $log (param i32)))
(memory 1)
(func (export "main")
(i32.store8 (i32.const 0) (i32.const 0x1F0)) ;; only the low byte 0xF0 is stored
(call $log (i32.load8_u (i32.const 0))) ;; 240
(call $log (i32.load8_s (i32.const 0))) ;; 0xF0 as a signed byte = -16
(i32.store16 (i32.const 2) (i32.const -2)) ;; 0xFFFE
(call $log (i32.load16_u (i32.const 2)))
(call $log (i32.load16_s (i32.const 2)))))240
-16
65534
-2
The offset immediate
Memory instructions compute the effective address as address operand + offset. The offset is a constant
encoded in the instruction, which is perfect for struct fields: keep a pointer to the struct and use a different
offset for each field.
(module
(import "console" "log" (func $log (param i32)))
(memory 1)
;; struct Point3 { i32 x; i32 y; i32 z; } -> fields at offsets 0, 4, 8 (12 bytes total)
(func $make_point (param $p i32) (param $x i32) (param $y i32) (param $z i32)
(i32.store offset=0 (local.get $p) (local.get $x))
(i32.store offset=4 (local.get $p) (local.get $y))
(i32.store offset=8 (local.get $p) (local.get $z)))
(func $sum_fields (param $p i32) (result i32)
(i32.add
(i32.add (i32.load offset=0 (local.get $p)) (i32.load offset=4 (local.get $p)))
(i32.load offset=8 (local.get $p))))
(func (export "main")
(call $make_point (i32.const 100) (i32.const 3) (i32.const 4) (i32.const 5))
(call $make_point (i32.const 112) (i32.const 10) (i32.const 20) (i32.const 30))
(call $log (call $sum_fields (i32.const 100)))
(call $log (call $sum_fields (i32.const 112)))
(call $log (i32.load (i32.const 116))))) ;; y of the second point12
60
20
The addition address + offset is done without wrapping: an address near 0xFFFFFFFF plus an offset cannot
wrap around to a small address. It simply goes out of bounds and traps.
The align immediate
align=N is a hint that the effective address is a multiple of N bytes. It may be 1, 2, 4, … up to the
access's natural size (4 for i32.load), and the default is the natural size.
i32.load align=4 ;; default: "this address is a multiple of 4"
i32.load align=1 ;; "this address may be anything"
i32.load align=8 ;; invalid: larger than the 4-byte accessUnlike on some CPUs, a misaligned access never traps in WebAssembly. The hint only lets the engine pick faster code on hardware where it matters. Lying in the hint is allowed and still correct, merely potentially slower:
(module
(import "console" "log" (func $log (param i32)))
(memory 1)
(func (export "main")
(i32.store (i32.const 1) (i32.const 0x01020304)) ;; address 1: not a multiple of 4
(call $log (i32.load (i32.const 1))) ;; still works
(call $log (i32.load align=1 (i32.const 1)))
(call $log (i32.load8_u (i32.const 1))))) ;; lowest byte: 416909060
16909060
4
An alignment larger than the natural one is rejected at validation:
(module
(memory 1)
(func (export "main") (result i32)
(i32.load align=8 (i32.const 0))))main.wat:4:6: error: alignment must not be larger than natural alignment (4)
(i32.load align=8 (i32.const 0))))
^^^^^^^^
Floats in memory
f32.store/f64.store write the IEEE bit patterns. Loading them with an integer instruction is a reinterpretation
through memory:
(module
(import "console" "log" (func $logf (param f64)))
(import "console" "log" (func $log (param i32)))
(memory 1)
(func (export "main")
(local $i i32)
;; an array of 4 doubles at address 0: 1.5, 2.5, 3.5, 4.5
(loop $fill
(f64.store (i32.shl (local.get $i) (i32.const 3))
(f64.add (f64.convert_i32_u (local.get $i)) (f64.const 1.5)))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br_if $fill (i32.lt_u (local.get $i) (i32.const 4))))
(call $logf (f64.load offset=16 (i32.const 0))) ;; element 2
(call $logf (f64.add (f64.load (i32.const 0)) (f64.load offset=24 (i32.const 0))))
(call $log (i32.load offset=4 (i32.const 0))))) ;; high word of 1.5 = 0x3FF800003.5
6
1073217536
0x3FF80000 is 1073217536: the sign, exponent and top mantissa bits of 1.5.
Common mistakes
- Forgetting to scale the index.
arr[i]for 8-byte doubles isbase + i * 8, notbase + i. - Overlapping data. Two "arrays" placed too close together silently overwrite each other: nothing in Wasm separates them. Plan your memory layout (or let a compiler do it).
- Mixing up
_sand_uwhen loading bytes. Text and pixel data are almost always unsigned: useload8_u.
Test yourself
What does i32.load8_s return for a stored byte 0xFF?
- 255
- -1
- 0
- It traps
What is the effective address of (i32.load offset=8 (i32.const 100))?
- 8
- 100
- 108
- 800
What happens on a misaligned i32.load (address 3, default alignment)?
- It traps
- It reads the wrong bytes
- It works correctly; alignment is only a performance hint
- Validation fails