hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/arithmetic.hdass1
-rw-r--r--examples/arm64/exit.hdass7
-rw-r--r--examples/arm64/sum.hdass2
-rw-r--r--examples/branch.hdass1
-rw-r--r--examples/call.hdass1
-rw-r--r--examples/circle.hdass12
-rw-r--r--examples/constants.hdass2
-rw-r--r--examples/exit_code.hdass1
-rw-r--r--examples/fibonacci.hdass2
-rw-r--r--examples/greet.hdass1
-rw-r--r--examples/load.hdass7
-rw-r--r--examples/logical.hdass13
-rw-r--r--examples/loop_sum.hdass5
-rw-r--r--examples/mandelbrot.hdass26
-rw-r--r--examples/modulo.hdass1
-rw-r--r--examples/mul_div.hdass6
-rw-r--r--examples/records.hdass14
17 files changed, 36 insertions, 66 deletions
diff --git a/examples/arithmetic.hdass b/examples/arithmetic.hdass
index 1534146..f3da8ba 100644
--- a/examples/arithmetic.hdass
+++ b/examples/arithmetic.hdass
@@ -1,6 +1,5 @@
[entry: main]
-// Computes (5 + 3) * 2 - 1 = 15 and returns it as the exit status.
const SYS_EXIT = 60
proc main
diff --git a/examples/arm64/exit.hdass b/examples/arm64/exit.hdass
index a86e5e2..a7e89df 100644
--- a/examples/arm64/exit.hdass
+++ b/examples/arm64/exit.hdass
@@ -1,14 +1,11 @@
[entry: main]
[enable: logical_registers]
-// Exits with status 42 on AArch64 Linux. Logical registers map r1 -> x0 and
-// r9 -> x8, so the exit code goes in x0 and the syscall number in x8; `syscall`
-// lowers to `svc #0`.
const SYS_EXIT = 93
proc main
{
- r1 = 42 // x0 = exit code
- r9 = SYS_EXIT // x8 = syscall number
+ r1 = 42
+ r9 = SYS_EXIT
syscall
}
diff --git a/examples/arm64/sum.hdass b/examples/arm64/sum.hdass
index 85820c9..6e93133 100644
--- a/examples/arm64/sum.hdass
+++ b/examples/arm64/sum.hdass
@@ -1,8 +1,6 @@
[entry: main]
[enable: logical_registers]
-// Sums 5 + 4 + 3 + 2 + 1 = 15 with a named countdown loop, returns it as the
-// exit status. Shows three-operand arithmetic (r1 = r1 + r2 -> add x0, x0, x1).
const SYS_EXIT = 93
proc main
diff --git a/examples/branch.hdass b/examples/branch.hdass
index 15f8d1b..9a31463 100644
--- a/examples/branch.hdass
+++ b/examples/branch.hdass
@@ -1,6 +1,5 @@
[entry: main]
-// Returns the larger of two values (max(3, 8) = 8) as the exit status.
const SYS_EXIT = 60
proc main
diff --git a/examples/call.hdass b/examples/call.hdass
index bf0c515..63eb957 100644
--- a/examples/call.hdass
+++ b/examples/call.hdass
@@ -1,6 +1,5 @@
[entry: main]
-// Passes an argument through a proc parameter, which exits with that value.
const SYS_EXIT = 60
proc exit_with(code: rdi)
diff --git a/examples/circle.hdass b/examples/circle.hdass
index 8a4edaa..71b204b 100644
--- a/examples/circle.hdass
+++ b/examples/circle.hdass
@@ -1,17 +1,15 @@
[entry: main]
-// Floating-point math on the SSE registers. Computes the area of a circle
-// (pi * r * r) with r = 3, then exits with the truncated result (28).
const SYS_EXIT = 60
proc main
{
- xmm0 = 3.0 // radius
- xmm0 *= xmm0 // r * r = 9.0
- xmm1 = 3.14159 // pi
- xmm0 *= xmm1 // area ~= 28.27
+ xmm0 = 3.0 // radius
+ xmm0 *= xmm0 // r * r = 9.0
+ xmm1 = 3.14159 // pi
+ xmm0 *= xmm1 // area ~= 28.27
- rdi = xmm0 // truncate to int -> 28
+ rdi = xmm0 // truncate
rax = SYS_EXIT
syscall
}
diff --git a/examples/constants.hdass b/examples/constants.hdass
index 1bb85eb..227c83b 100644
--- a/examples/constants.hdass
+++ b/examples/constants.hdass
@@ -1,7 +1,5 @@
[entry: main]
-// The exit status is folded from constant arithmetic, and SYS_EXIT is a hex
-// literal. STATUS = 6 * 7 = 42.
const A = 6
const B = 7
const STATUS = A * B
diff --git a/examples/exit_code.hdass b/examples/exit_code.hdass
index cabe634..0d6706b 100644
--- a/examples/exit_code.hdass
+++ b/examples/exit_code.hdass
@@ -1,6 +1,5 @@
[entry: main]
-// Exits with status 42 and no output.
const SYS_EXIT = 60
proc main
diff --git a/examples/fibonacci.hdass b/examples/fibonacci.hdass
index 47224fb..7f93c12 100644
--- a/examples/fibonacci.hdass
+++ b/examples/fibonacci.hdass
@@ -43,7 +43,7 @@ proc main
{
r12 = 0
r13 = 1
- r15 = 10 // syscall clobbers rcx/r11, so keep the counter in r15
+ r15 = 10
while .sequence r15 > 0
{
diff --git a/examples/greet.hdass b/examples/greet.hdass
index da9f790..ebcf642 100644
--- a/examples/greet.hdass
+++ b/examples/greet.hdass
@@ -1,6 +1,5 @@
[entry: main]
-// Writes a fixed string to stdout, then exits cleanly.
const SYS_WRITE = 1
const SYS_EXIT = 60
diff --git a/examples/load.hdass b/examples/load.hdass
index 8d29358..8f027ea 100644
--- a/examples/load.hdass
+++ b/examples/load.hdass
@@ -1,6 +1,5 @@
[entry: main]
-// Stores 7 into a stack cell, loads it back through a pointer, and exits with it.
const SYS_EXIT = 60
proc main
@@ -8,9 +7,9 @@ proc main
stack cell[8]
rbx = 7
- rsi = cell // address of the cell
- ^rsi = rbx // store
- rdi = ^rsi // load it back
+ rsi = cell
+ ^rsi = rbx
+ rdi = ^rsi
rax = SYS_EXIT
syscall
diff --git a/examples/logical.hdass b/examples/logical.hdass
index 5fb27fc..1b7dd2a 100644
--- a/examples/logical.hdass
+++ b/examples/logical.hdass
@@ -1,19 +1,16 @@
[entry: main]
[enable: logical_registers]
-// With the logical_registers extension, r1..r14 name the general-purpose
-// registers (r1 = rax, r2 = rbx, ...) instead of the architecture names.
-// Computes (4 + 3) * 3 = 21 and returns it as the exit status.
const SYS_EXIT = 60
proc main
{
- r1 = 4 // rax
- r2 = 3 // rbx
- r1 += r2 // 7
- r1 *= r2 // 21
+ r1 = 4
+ r2 = 3
+ r1 += r2
+ r1 *= r2
- r6 = r1 // rdi = 21 (the exit status)
+ r6 = r1
r1 = SYS_EXIT
syscall
}
diff --git a/examples/loop_sum.hdass b/examples/loop_sum.hdass
index 130f055..7d9acdd 100644
--- a/examples/loop_sum.hdass
+++ b/examples/loop_sum.hdass
@@ -1,12 +1,11 @@
[entry: main]
-// Sums 5 + 4 + 3 + 2 + 1 = 15 with a countdown loop, returns it as the status.
const SYS_EXIT = 60
proc main
{
- rbx = 0 // running total
- rcx = 5 // counter
+ rbx = 0
+ rcx = 5
while .countdown rcx > 0
{
diff --git a/examples/mandelbrot.hdass b/examples/mandelbrot.hdass
index 9054c15..9a1fd45 100644
--- a/examples/mandelbrot.hdass
+++ b/examples/mandelbrot.hdass
@@ -1,11 +1,6 @@
[entry: main]
[enable: logical_registers]
-// Renders the Mandelbrot set as ASCII art. The evolving z value is kept in a
-// Complex struct on the stack (accessed through its field offsets), while the
-// per-pixel constant c stays in xmm2/xmm3. Uses double-precision floats and the
-// logical_registers extension (r1..r14 name the general-purpose registers).
-
const SYS_WRITE = 1
const SYS_EXIT = 60
const STDOUT = 1
@@ -27,7 +22,7 @@ proc main
stack z[Complex.size]
stack row[W + 1]
- r11 = 0 // py
+ r11 = 0 // py
row_loop:
// ci = py * 0.08 - 1.2
@@ -37,7 +32,7 @@ row_loop:
xmm7 = 1.2
xmm3 -= xmm7
- r12 = 0 // px
+ r12 = 0 // px
col_loop:
// cr = px * 0.04375 - 2.5
@@ -51,12 +46,12 @@ col_loop:
xmm0 = 0.0
r5 = z
r5 += Complex.re
- ^r5 = xmm0 // z.re = 0
+ ^r5 = xmm0 // z.re = 0
r5 = z
r5 += Complex.im
- ^r5 = xmm0 // z.im = 0
+ ^r5 = xmm0 // z.im = 0
- r2 = 0 // iteration count
+ r2 = 0 // iteration count
iter_loop:
// load z into xmm0 (zr) and xmm1 (zi)
@@ -79,12 +74,12 @@ iter_loop:
if xmm6 > 4.0
goto plot
- // new_zi = 2*zr*zi + ci (uses the old zr and zi)
+ // new_zi = 2*zr*zi + ci
xmm6 = xmm0
xmm6 *= xmm1
xmm6 += xmm6
xmm6 += xmm3
- xmm7 = xmm6 // stash new_zi
+ xmm7 = xmm6
// new_zr = zr2 - zi2 + cr
xmm4 -= xmm5
@@ -93,17 +88,17 @@ iter_loop:
// store the new z back into the struct
r5 = z
r5 += Complex.re
- ^r5 = xmm4 // z.re = new_zr
+ ^r5 = xmm4 // z.re = new_zr
r5 = z
r5 += Complex.im
- ^r5 = xmm7 // z.im = new_zi
+ ^r5 = xmm7 // z.im = new_zi
r2 += 1
if r2 < MAX_ITER
goto iter_loop
plot:
- // index = iter * 9 / MAX_ITER, then char = palette[index]
+ // index = iter * 9 / MAX_ITER
r1 = r2
r1 *= 9
r3 = MAX_ITER
@@ -121,7 +116,6 @@ plot:
if r12 < W
goto col_loop
- // terminate the row with a newline and write it
r6 = row
r6 += W
r1 = 10
diff --git a/examples/modulo.hdass b/examples/modulo.hdass
index 44c290d..a437080 100644
--- a/examples/modulo.hdass
+++ b/examples/modulo.hdass
@@ -1,6 +1,5 @@
[entry: main]
-// Modulo: 2024 % 100 = 24, returned as the exit status.
const SYS_EXIT = 60
proc main
diff --git a/examples/mul_div.hdass b/examples/mul_div.hdass
index 8c813b6..43a1357 100644
--- a/examples/mul_div.hdass
+++ b/examples/mul_div.hdass
@@ -1,17 +1,15 @@
[entry: main]
-// (7 * 6) / 5 = 8 with a value-expression multiply and a non-rax division,
-// returned as the exit status.
const SYS_EXIT = 60
proc main
{
rbx = 7
rcx = 6
- rbx = rbx * rcx // 42
+ rbx = rbx * rcx
rcx = 5
- rbx /= rcx // 8
+ rbx /= rcx
rdi = rbx
rax = SYS_EXIT
diff --git a/examples/records.hdass b/examples/records.hdass
index 8e87a4c..a6d5659 100644
--- a/examples/records.hdass
+++ b/examples/records.hdass
@@ -1,7 +1,5 @@
[entry: main]
-// An enum of status codes and a struct describing a memory layout. Stores two
-// fields into a stack instance, reads them back, and exits with their sum.
enum Status
{
Ok,
@@ -24,20 +22,20 @@ proc main
rsi = pair
rsi += Pair.a
rbx = 40
- ^rsi = rbx // pair.a
+ ^rsi = rbx
rsi = pair
rsi += Pair.b
- rcx = Status.Fail // 2
- ^rsi = rcx // pair.b
+ rcx = Status.Fail
+ ^rsi = rcx
rsi = pair
rsi += Pair.a
- rdi = ^rsi // load a = 40
+ rdi = ^rsi
rsi = pair
rsi += Pair.b
- rax = ^rsi // load b = 2
- rdi += rax // 42
+ rax = ^rsi
+ rdi += rax
rax = SYS_EXIT
syscall