diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/arithmetic.hdass | 13 | ||||
| -rw-r--r-- | examples/branch.hdass | 17 | ||||
| -rw-r--r-- | examples/call.hdass | 13 | ||||
| -rw-r--r-- | examples/exit_code.hdass | 9 | ||||
| -rw-r--r-- | examples/greet.hdass | 20 | ||||
| -rw-r--r-- | examples/loop_sum.hdass | 18 |
6 files changed, 90 insertions, 0 deletions
diff --git a/examples/arithmetic.hdass b/examples/arithmetic.hdass new file mode 100644 index 0000000..359653e --- /dev/null +++ b/examples/arithmetic.hdass @@ -0,0 +1,13 @@ +// Computes (5 + 3) * 2 - 1 = 15 and returns it as the exit status. +const SYS_EXIT = 60 + +proc main +{ + rdi = 5 + rdi += 3 + rdi *= 2 + rdi -= 1 + + rax = SYS_EXIT + syscall +} diff --git a/examples/branch.hdass b/examples/branch.hdass new file mode 100644 index 0000000..1218773 --- /dev/null +++ b/examples/branch.hdass @@ -0,0 +1,17 @@ +// Returns the larger of two values (max(3, 8) = 8) as the exit status. +const SYS_EXIT = 60 + +proc main +{ + rbx = 3 + rcx = 8 + + rdi = rbx + if rcx <= rbx + goto done + rdi = rcx + +done: + rax = SYS_EXIT + syscall +} diff --git a/examples/call.hdass b/examples/call.hdass new file mode 100644 index 0000000..239a26b --- /dev/null +++ b/examples/call.hdass @@ -0,0 +1,13 @@ +// Passes an argument through a proc parameter, which exits with that value. +const SYS_EXIT = 60 + +proc exit_with(code: rdi) +{ + rax = SYS_EXIT + syscall +} + +proc main +{ + exit_with(21) +} diff --git a/examples/exit_code.hdass b/examples/exit_code.hdass new file mode 100644 index 0000000..7c44df9 --- /dev/null +++ b/examples/exit_code.hdass @@ -0,0 +1,9 @@ +// Exits with status 42 and no output. +const SYS_EXIT = 60 + +proc main +{ + rax = SYS_EXIT + rdi = 42 + syscall +} diff --git a/examples/greet.hdass b/examples/greet.hdass new file mode 100644 index 0000000..a253b5e --- /dev/null +++ b/examples/greet.hdass @@ -0,0 +1,20 @@ +// Writes a fixed string to stdout, then exits cleanly. +const SYS_WRITE = 1 +const SYS_EXIT = 60 + +const STDOUT = 1 + +data greeting = "hdass works!\n" + +proc main +{ + rax = SYS_WRITE + rdi = STDOUT + rsi = greeting + rdx = greeting.len + syscall + + rax = SYS_EXIT + rdi = 0 + syscall +} diff --git a/examples/loop_sum.hdass b/examples/loop_sum.hdass new file mode 100644 index 0000000..bb8d115 --- /dev/null +++ b/examples/loop_sum.hdass @@ -0,0 +1,18 @@ +// 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 + +loop: + rbx += rcx + rcx -= 1 + if rcx != 0 + goto loop + + rdi = rbx + rax = SYS_EXIT + syscall +} |
