diff options
| author | hachem <im@hachem.wtf> | 2026-09-08 12:16:51 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-09-08 12:16:51 +0200 |
| commit | 60c0e084b159801a45570b56f152f0f5e9193d72 (patch) | |
| tree | ad6c72d6782ab5d9edff336e1e852ca7b995bcdc /docs/language.md | |
| parent | 9d47486aac9cd1a4422b78ee273b7a27bf7b733a (diff) | |
feat: add while loops
Diffstat (limited to 'docs/language.md')
| -rw-r--r-- | docs/language.md | 15 |
1 files changed, 14 insertions, 1 deletions
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 <expr> <cmp> <expr>` 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 <expr> <cmp> <expr>` 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: |
