Embedded Systems Fundamentals · Interview question

How does writing to a C variable end up controlling hardware?

A strong answer

Through memory-mapped I/O: peripheral registers occupy fixed addresses in the CPU's address space, so a store instruction to the right address physically acts on the peripheral. In C you express that by casting the address to a volatile pointer of the correct width and dereferencing it, *(volatile uint32_t*)0x40020014 = 1, or, idiomatically, through a register-overlay struct (GPIOA->ODR = 1) whose members are laid out to match the datasheet offsets. The store isn't "saving a value" in the normal sense; the hardware decodes that address and, for an output-data register, drives the corresponding pins. The volatile qualifier tells the compiler the access has side effects and the value can change outside the program, so it must perform exactly the loads and stores written, in order, without caching or eliding them. That combination, a known address, a load/store, and volatile, is the foundation of every driver.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Registers & Memory-Mapped I/O

How writing a memory address turns on an LED: control/status/data registers, the volatile register-overlay struct, and the write-1-to-clear and atomic set/reset traps.

More Registers & Memory-Mapped I/O questions

Browse all 472 interview questions
How does writing to a C variable end up controlling hardware? | EmbeddedPrep.io