Embedded C and assembly
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| memory-mapped register | register ánh xạ bộ nhớ |
| assembly | assembly |
| read-modify-write | read-modify-write |
Registers are addresses
- A memory-mapped register is just an address; C reaches it through a
volatilepointer — volatile tells the compiler this memory changes outside its view, so touch it every time. - Assembly shows the same program one instruction per line: load, store, arithmetic, branch.

*GPIO_OUT |= (1u << 5); is load, OR with 0x20, store — three bus actions, because bits are not individually addressable.
{exercise:uni-ee0821-rmw}
What does *GPIO_OUT |= (1u << 5); do?
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
The |= is a load-mask-store: read the register, OR in 0x20, write it back — exactly the three bus actions of read-modify-write.
Dropping volatile from the register pointer would let the compiler —
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
Without volatile the compiler may cache the register in a CPU register — the loop then watches a snapshot, not the hardware.
Trace it in hex
- To clear bit 3 and flip bits 5, 6 of
0xCE:0xCE & ~0x08 = 0xC6, then0xC6 ^ 0x60 = 0xA6— one read-modify-write, verified on paper. - The hex trace is the fastest debugger in this unit.
{exercise:uni-ee0822-hex}
Starting from 0xCE, apply AND ~0x08 then XOR 0x60. The result is —
Độ khó 1/5 · 100 xu + phần thưởng nhỏ khi hoàn thành lần đầu tiên hôm nay
0xCE & ~0x08 clears bit 3 giving 0xC6; 0xC6 ^ 0x60 flips bits 5 and 6 (110 → 010) giving 0xA6.