How is a do-while loop translated to assembly, and why is it considered the most fundamental loop form?
It becomes "label: Body; if (Test) goto label;" — body first, then a conditional jump back.
* do-while: body first, then one conditional back-edge — the raw hardware loop shape. *
A do-while runs its body, then checks the condition, looping back while it's true:
do { Body } while (Test);
Goto / assembly shape:
.L_loop:
# Body
cmpq ... # evaluate Test
jne .L_loop # if true, jump back and repeat
Why it's the canonical form: This maps exactly onto what hardware offers — straight-line code followed by one conditional backward branch. There's no separate "loop" instruction needed. Because of this, compilers convert every while and for loop into do-while form internally (adding a guard test where needed). If you can recognize the "body, then conditional jump backward to the body's top" pattern in disassembly, you've found a loop.
Note: Test is an integer expression: 0 means false, anything non-zero means true — the same convention C uses.
Go deeper:
Do-while loop (Wikipedia) — the do-while (post-test) loop.
Control flow (Wikipedia) — loops as control flow.