From 60c0e084b159801a45570b56f152f0f5e9193d72 Mon Sep 17 00:00:00 2001 From: hachem Date: Tue, 8 Sep 2026 12:16:51 +0200 Subject: feat: add while loops --- docs/language.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'docs') diff --git a/docs/language.md b/docs/language.md index 3ccdab8..8556c8b 100644 --- a/docs/language.md +++ b/docs/language.md @@ -120,12 +120,14 @@ loop: // label goto loop if rcx != 0 // == != < <= > >= ; guards the next statement or a { block } goto loop +while rcx != 0 // same condition; repeats the next statement or { block } + rcx -= 1 syscall print_number(r12) // call; args go into the callee's parameter registers stack buf[Point.size] // stack buffer (size is any constant); buf is its base address ``` -## Branching (`if` / `else`) +## Control flow (`if` / `else` / `while`) `if ` guards either the single next statement or a `{ }` block, and an optional `else` takes its own statement or block. `else if` chains because the `else` body is itself a statement. Comparisons are `==` `!=` `<` `<=` `>` `>=`; a float compare needs an `xmm` register on the left (see [Floating point](#floating-point)). @@ -141,6 +143,17 @@ else rdi = -1 ``` +`while ` runs its statement or `{ }` block for as long as the condition holds, testing it before each pass. It is the same condition as `if`, and desugars to a label, the test, the body, and a jump back — the `loop:`/`goto` you would write by hand. Use `goto` to break out early. + +```hdass +rbx = 0 +while rcx > 0 +{ + rbx += rcx + rcx -= 1 +} +``` + ## Dereference (`^`) `^reg` is the memory at the address in `reg` — NASM's `[reg]`. On the left of `=` it stores there. The store width comes from the value operand, so a sized sub-register picks the size: -- cgit v1.3