sample_programs
Sixteen ARM64 assembly programs for afs-as, collected in one root-level directory so it is easy to poke at the assembler without hunting through test fixtures.
Build afs-as
Run this from the repo root:
cargo build -p afs-as
Assemble, Link, And Run One Sample
This example assembles and runs 02_hello_world.s without leaving generated files in the repo:
SDKROOT=$(xcrun --show-sdk-path)
target/debug/afs-as sample_programs/02_hello_world.s -o /tmp/02_hello_world.o
ld /tmp/02_hello_world.o -o /tmp/02_hello_world -lSystem -syslibroot "$SDKROOT" -e _main
/tmp/02_hello_world
For samples that report their result through the process exit code, check it with:
echo $?
Assemble And Link Any Sample
Replace NAME with a file stem like 09_array_sum or 15_fp_sqrt:
SDKROOT=$(xcrun --show-sdk-path)
NAME=09_array_sum
target/debug/afs-as "sample_programs/$NAME.s" -o "/tmp/$NAME.o"
ld "/tmp/$NAME.o" -o "/tmp/$NAME" -lSystem -syslibroot "$SDKROOT" -e _main
"/tmp/$NAME"
echo $?
16_snake_live.s is a live terminal program, so run it directly in an interactive terminal instead of expecting a useful exit-code script.
Assemble Everything
mkdir -p /tmp/afs-samples
for src in sample_programs/*.s; do
name=$(basename "${src%.s}")
target/debug/afs-as "$src" -o "/tmp/afs-samples/$name.o"
done
Assemble, Link, And Run Everything
mkdir -p /tmp/afs-samples
SDKROOT=$(xcrun --show-sdk-path)
for src in sample_programs/*.s; do
name=$(basename "${src%.s}")
target/debug/afs-as "$src" -o "/tmp/afs-samples/$name.o"
ld "/tmp/afs-samples/$name.o" -o "/tmp/afs-samples/$name" -lSystem -syslibroot "$SDKROOT" -e _main
printf "%s: " "$name"
"/tmp/afs-samples/$name"
status=$?
printf "exit=%s\n" "$status"
done
Live Snake
SDKROOT=$(xcrun --show-sdk-path)
target/debug/afs-as sample_programs/16_snake_live.s -o /tmp/16_snake_live.o
ld /tmp/16_snake_live.o -o /tmp/16_snake_live -lSystem -syslibroot "$SDKROOT" -e _main
/tmp/16_snake_live
Controls:
W A S D move
q quits
Sample Index
| File | What It Shows | Expected Result |
|---|---|---|
01_exit_0.s |
Smallest possible _main with exit(0) |
exit 0 |
02_hello_world.s |
Direct write syscall with a .asciz string |
stdout Hello, World!\n, exit 0 |
03_arithmetic_exit_42.s |
Integer multiply and exit status | exit 42 |
04_sum_1_to_10.s |
Numeric local labels and a counted loop | exit 55 |
05_factorial_5.s |
Repeated multiply in a loop | exit 120 |
06_fibonacci_10.s |
Iterative Fibonacci with cbz |
exit 55 |
07_dot_product.s |
Two arrays, memory loads, multiply-accumulate | exit 70 |
08_gcd_48_18.s |
Euclid by repeated subtraction | exit 6 |
09_array_sum.s |
Walking a .quad array in memory |
exit 36 |
10_string_length.s |
ldrb over a C string in __cstring |
exit 9 |
11_copy_qword_and_write.s |
Copying 8 bytes into __bss and writing them |
stdout Copy OK\n, exit 0 |
12_puts_call.s |
External call relocation against _puts |
stdout Hello from puts()\n, exit 0 |
13_helper_function.s |
Internal bl, stack frame, and ret |
exit 21 |
14_literal_pool.s |
ldr literal load from an inline .quad |
exit 77 |
15_fp_sqrt.s |
scvtf, fadd, fsqrt, and fcvtzs |
exit 5 |
16_snake_live.s |
Bonus live snake with raw terminal input, redraws, and nonblocking reads | interactive game in a real terminal |
View source
| 1 | # sample_programs |
| 2 | |
| 3 | Sixteen ARM64 assembly programs for `afs-as`, collected in one root-level directory so it is easy to poke at the assembler without hunting through test fixtures. |
| 4 | |
| 5 | ## Build `afs-as` |
| 6 | |
| 7 | Run this from the repo root: |
| 8 | |
| 9 | ```bash |
| 10 | cargo build -p afs-as |
| 11 | ``` |
| 12 | |
| 13 | ## Assemble, Link, And Run One Sample |
| 14 | |
| 15 | This example assembles and runs `02_hello_world.s` without leaving generated files in the repo: |
| 16 | |
| 17 | ```bash |
| 18 | SDKROOT=$(xcrun --show-sdk-path) |
| 19 | target/debug/afs-as sample_programs/02_hello_world.s -o /tmp/02_hello_world.o |
| 20 | ld /tmp/02_hello_world.o -o /tmp/02_hello_world -lSystem -syslibroot "$SDKROOT" -e _main |
| 21 | /tmp/02_hello_world |
| 22 | ``` |
| 23 | |
| 24 | For samples that report their result through the process exit code, check it with: |
| 25 | |
| 26 | ```bash |
| 27 | echo $? |
| 28 | ``` |
| 29 | |
| 30 | ## Assemble And Link Any Sample |
| 31 | |
| 32 | Replace `NAME` with a file stem like `09_array_sum` or `15_fp_sqrt`: |
| 33 | |
| 34 | ```bash |
| 35 | SDKROOT=$(xcrun --show-sdk-path) |
| 36 | NAME=09_array_sum |
| 37 | target/debug/afs-as "sample_programs/$NAME.s" -o "/tmp/$NAME.o" |
| 38 | ld "/tmp/$NAME.o" -o "/tmp/$NAME" -lSystem -syslibroot "$SDKROOT" -e _main |
| 39 | "/tmp/$NAME" |
| 40 | echo $? |
| 41 | ``` |
| 42 | |
| 43 | `16_snake_live.s` is a live terminal program, so run it directly in an interactive terminal instead of expecting a useful exit-code script. |
| 44 | |
| 45 | ## Assemble Everything |
| 46 | |
| 47 | ```bash |
| 48 | mkdir -p /tmp/afs-samples |
| 49 | for src in sample_programs/*.s; do |
| 50 | name=$(basename "${src%.s}") |
| 51 | target/debug/afs-as "$src" -o "/tmp/afs-samples/$name.o" |
| 52 | done |
| 53 | ``` |
| 54 | |
| 55 | ## Assemble, Link, And Run Everything |
| 56 | |
| 57 | ```bash |
| 58 | mkdir -p /tmp/afs-samples |
| 59 | SDKROOT=$(xcrun --show-sdk-path) |
| 60 | |
| 61 | for src in sample_programs/*.s; do |
| 62 | name=$(basename "${src%.s}") |
| 63 | target/debug/afs-as "$src" -o "/tmp/afs-samples/$name.o" |
| 64 | ld "/tmp/afs-samples/$name.o" -o "/tmp/afs-samples/$name" -lSystem -syslibroot "$SDKROOT" -e _main |
| 65 | printf "%s: " "$name" |
| 66 | "/tmp/afs-samples/$name" |
| 67 | status=$? |
| 68 | printf "exit=%s\n" "$status" |
| 69 | done |
| 70 | ``` |
| 71 | |
| 72 | ## Live Snake |
| 73 | |
| 74 | ```bash |
| 75 | SDKROOT=$(xcrun --show-sdk-path) |
| 76 | target/debug/afs-as sample_programs/16_snake_live.s -o /tmp/16_snake_live.o |
| 77 | ld /tmp/16_snake_live.o -o /tmp/16_snake_live -lSystem -syslibroot "$SDKROOT" -e _main |
| 78 | /tmp/16_snake_live |
| 79 | ``` |
| 80 | |
| 81 | Controls: |
| 82 | |
| 83 | ```text |
| 84 | W A S D move |
| 85 | q quits |
| 86 | ``` |
| 87 | |
| 88 | ## Sample Index |
| 89 | |
| 90 | | File | What It Shows | Expected Result | |
| 91 | |------|----------------|-----------------| |
| 92 | | `01_exit_0.s` | Smallest possible `_main` with `exit(0)` | exit `0` | |
| 93 | | `02_hello_world.s` | Direct `write` syscall with a `.asciz` string | stdout `Hello, World!\n`, exit `0` | |
| 94 | | `03_arithmetic_exit_42.s` | Integer multiply and exit status | exit `42` | |
| 95 | | `04_sum_1_to_10.s` | Numeric local labels and a counted loop | exit `55` | |
| 96 | | `05_factorial_5.s` | Repeated multiply in a loop | exit `120` | |
| 97 | | `06_fibonacci_10.s` | Iterative Fibonacci with `cbz` | exit `55` | |
| 98 | | `07_dot_product.s` | Two arrays, memory loads, multiply-accumulate | exit `70` | |
| 99 | | `08_gcd_48_18.s` | Euclid by repeated subtraction | exit `6` | |
| 100 | | `09_array_sum.s` | Walking a `.quad` array in memory | exit `36` | |
| 101 | | `10_string_length.s` | `ldrb` over a C string in `__cstring` | exit `9` | |
| 102 | | `11_copy_qword_and_write.s` | Copying 8 bytes into `__bss` and writing them | stdout `Copy OK\n`, exit `0` | |
| 103 | | `12_puts_call.s` | External call relocation against `_puts` | stdout `Hello from puts()\n`, exit `0` | |
| 104 | | `13_helper_function.s` | Internal `bl`, stack frame, and `ret` | exit `21` | |
| 105 | | `14_literal_pool.s` | `ldr` literal load from an inline `.quad` | exit `77` | |
| 106 | | `15_fp_sqrt.s` | `scvtf`, `fadd`, `fsqrt`, and `fcvtzs` | exit `5` | |
| 107 | | `16_snake_live.s` | Bonus live snake with raw terminal input, redraws, and nonblocking reads | interactive game in a real terminal | |