diff options
| author | Hunter Kvalevog <hunter@kvog.sh> | 2026-10-28 16:05:43 -0500 |
|---|---|---|
| committer | Hunter Kvalevog <hunter@kvog.sh> | 2026-10-28 16:06:50 -0500 |
| commit | 64321f1b4b50c46ae57f8c083bc110a75849dd9d (patch) | |
| tree | 017225eba0fe70a0e0c24eef35d3cfb73d7e9812 /arm64 | |
| parent | b0a21fa91599c632e0acb054e25f4d8e3c5a08e5 (diff) | |
| download | demos-64321f1b4b50c46ae57f8c083bc110a75849dd9d.tar.gz demos-64321f1b4b50c46ae57f8c083bc110a75849dd9d.zip | |
Diffstat (limited to 'arm64')
| -rw-r--r-- | arm64/kusswurm_ch11.S | 145 | ||||
| -rw-r--r-- | arm64/kusswurm_ch11.c | 87 | ||||
| -rw-r--r-- | arm64/kusswurm_ch12.S | 97 | ||||
| -rw-r--r-- | arm64/kusswurm_ch12.c | 42 | ||||
| -rw-r--r-- | arm64/mariokartwii_ch07.S | 46 | ||||
| -rw-r--r-- | arm64/mariokartwii_ch08.S | 38 | ||||
| -rw-r--r-- | arm64/meson.build | 6 |
7 files changed, 461 insertions, 0 deletions
diff --git a/arm64/kusswurm_ch11.S b/arm64/kusswurm_ch11.S new file mode 100644 index 0000000..2819713 --- /dev/null +++ b/arm64/kusswurm_ch11.S @@ -0,0 +1,145 @@ +// ================================================================================================ +// Code written while reading chapter 11 of Modern Arm Assembly Language Programming +// +// License: +// Copyright (c) 2026 Hunter Kvalevog +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE. +// ================================================================================================ + +#include "../_/arm64/common.S" + +TEXT_SEGMENT + +// example 1 + +.global SYM(i32_add_sub) +.global SYM(i64_add_sub) + +SYM(i32_add_sub): + // w0: a + // w1: b + // w2: c +#if 0 + add w3, w0, w1 + sub w0, w3, w2 +#else + add w0, w0, w1 + sub w0, w0, w2 +#endif + // w0: return + ret + +SYM(i64_add_sub): + // x0: a + // x1: b + // x2: c + add x3, x0, x1 + sub x0, x3, x2 + // x0: return + ret + +// example 2 + +.global SYM(i32_mul) +.global SYM(i32_mul_safe) + +SYM(i32_mul): + // w0: a + // w1: b + mul w0, w0, w1 + // w0: return + ret + +SYM(i32_mul_safe): + // w0: a + // w1: b + smull x0, w0, w1 + // x0: ret + ret + +// example 3 + +.global SYM(i32_quo_rem) + +SYM(i32_quo_rem): + // w0: num + // w1: den + // x2: *quo + // x3: *den + + // calculate quotient + sdiv w4, w0, w1 + str w4, [x2] + + // calculate remainder + mul w5, w1, w4 + sub w6, w0, w5 + str w6, [x3] + + ret + +// example 4 + +.global SYM(test_ldr1) + +SYM(test_ldr1): + // w0: i + // x1: j + // x2: tab + + ldr w3, [x2, w0, uxtw #2] // tab[i] (32-bit index) + ldr w4, [x2, x1, lsl #2] // tab[j] (64-bit index) + + add w0, w3, w4 + + ret + +// example 5 + +.global SYM(test_mov1) + +SYM(test_mov1): + // x0: *out + + .equ BIGNUM,0xABCD1234 + .equ BIGNUM_LO16,((BIGNUM & 0x0000FFFF) >> 0) + .equ BIGNUM_HI16,((BIGNUM & 0xFFFF0000) >> 16) + + mov w2, #BIGNUM_LO16 + movk w2, #BIGNUM_HI16, lsl 16 + str w2, [x0] // out[0] = w2 + + movz w3, #BIGNUM_LO16, lsl 16 + str w3, [x0, #4] // out[1] = w3 + + movz w4, 0x1234 + str w4, [x0, #8] // out[2] = w4 + + ret + +// example 6 + +.global SYM(test_shift1) + +SYM(test_shift1): + // x0: *out + + ldr x9, [x0] // x9 = out[0] + ldr x10, [x0, #4] // x10 = out[1] + ldr x11, [x0, #8] // x11 = out[2] + + lsl x10, x9, x10 + lsr x11, x10, x11 + + str x10, [x0, #4] + str x11, [x0, #8] + + ret + +// skipping example 7 because it's very obvious + diff --git a/arm64/kusswurm_ch11.c b/arm64/kusswurm_ch11.c new file mode 100644 index 0000000..ec832f4 --- /dev/null +++ b/arm64/kusswurm_ch11.c @@ -0,0 +1,87 @@ +// ================================================================================================ +// Code written while reading chapter 11 of Modern Arm Assembly Language Programming +// +// License: +// Copyright (c) 2026 Hunter Kvalevog +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE. +// ================================================================================================ + +#include <inttypes.h> +#include <stdint.h> +#include <stdio.h> + +// example 1 +int32_t i32_add_sub(int32_t a, int32_t b, int32_t c); +int64_t i64_add_sub(int64_t a, int64_t b, int64_t c); + +// example 2 +int32_t i32_mul(int32_t a, int32_t b); +int64_t i32_mul_safe(int32_t a, int32_t b); + +// example 3 +void i32_quo_rem(int32_t num, int32_t den, int32_t *quo, int32_t *rem); + +// example 4 +int32_t test_ldr1(int32_t i, int64_t j, int32_t *tab); + +// example 5 +void test_mov1(uint32_t *tab); + +// example 6 +void test_shift1(uint32_t *tab); + +int main(int argc, const char* argv[]) +{ + (void)argc; (void)argv; +#define X(FMT, CODE) printf(#CODE ": " FMT "\n", CODE) + + // example 1 + X("%d", i32_add_sub(5, 3, 1)); + X("%" PRId64, i64_add_sub(5, 3, 1)); + + // example 2 + X("%d", i32_mul(5, 6)); + X("%d", i32_mul(2000000000, 4)); + X("%" PRId64, i32_mul_safe(5, 6)); + X("%" PRId64, i32_mul_safe(2000000000, 4)); + +#undef X +#define X(CODE) CODE; printf(#CODE ": quo=%d rem=%d\n", quo, rem) + + // example 3 + int32_t quo = 0; + int32_t rem = 0; + X(i32_quo_rem(6, 3, &quo, &rem)); + X(i32_quo_rem(7, 3, &quo, &rem)); + +#undef X +#define X(FMT, CODE) printf(#CODE ": " FMT "\n", CODE) + + // example 4 + int32_t tab[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + X("%d", test_ldr1(0, 0, tab)); + + // example 5 + uint32_t out[3] = { 0, 0, 0 }; + test_mov1(out); + printf("test_mov1(out):\n"); + printf(" out[0] = 0x%X\n", out[0]); + printf(" out[1] = 0x%X\n", out[1]); + printf(" out[2] = 0x%X\n", out[2]); + + // example 6 + out[0] = 1; // starting val + out[1] = 8; // shift left by + out[2] = 4; // shift right by + test_shift1(out); + printf("test_shift1(out):\n"); + printf(" out[0] = 0x%X\n", out[0]); + printf(" out[1] = 0x%X\n", out[1]); + printf(" out[2] = 0x%X\n", out[2]); +} + diff --git a/arm64/kusswurm_ch12.S b/arm64/kusswurm_ch12.S new file mode 100644 index 0000000..3a85504 --- /dev/null +++ b/arm64/kusswurm_ch12.S @@ -0,0 +1,97 @@ +// ================================================================================================ +// Code written while reading chapter 12 of Modern Arm Assembly Language Programming +// +// License: +// Copyright (c) 2026 Hunter Kvalevog +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE. +// ================================================================================================ + +#include "../_/arm64/common.S" + +TEXT_SEGMENT + +// example 1 + +.global SYM(test_sum10i32) + +SYM(test_sum10i32): + // [w0, w7] = [a, h] + +#ifdef __APPLE__ + .equ STK_i, 0 + .equ STK_j, 4 +#else + .equ STK_i, 0 + .equ STK_j, 8 +#endif + + ldr w8, [sp, STK_i] + ldr w9, [sp, STK_j] + +#if 0 + add w0, w0, w1 + add w0, w0, w2 + add w0, w0, w3 + add w0, w0, w4 + add w0, w0, w5 + add w0, w0, w6 + add w0, w0, w7 + add w0, w0, w8 + add w0, w0, w9 +#else + // better dependency chain: + add w0, w0, w1 + add w2, w2, w3 + add w4, w4, w5 + add w6, w6, w7 + add w8, w8, w9 + + add w0, w0, w2 + add w4, w4, w6 + + add w0, w0, w4 + add w0, w0, w8 +#endif + + ret + +// example 2 + +.global SYM(test_sum4i32) + +SYM(test_sum4i32): + // w0: i1 + // w1: i2 + // w2: i3 + // w3: i4 + + sub sp, sp, #32 // begin stack frame + stp x29, x30, [sp, #0] // save fp/lr + mov x29, sp // set frame pointer + stp x19, x20, [sp, #16] // save x19/x20 + + mov w19, w2 // w19 = i3 + mov w20, w3 // w20 = i4 + + bl SYM(c_sum2i32) // w0 = i1 + i2 + + mov w3, w0 // w3 = w0=(i1 + i2) + mov w0, w19 // w0 = i3 + mov w1, w20 // w1 = i4 + mov w19, w3 + bl SYM(c_sum2i32) // w0 = i3 + i4 + + mov w1, w19 // w1 = i1 + i2 + bl SYM(c_sum2i32) // w0 = i3 + i4 + i1 + i2 + + ldp x19, x20, [sp, #16] // restore x19/x20 + ldp x29, x30, [sp, #0] // restore fp/lr + add sp, sp, #32 // end stack frame + + ret + diff --git a/arm64/kusswurm_ch12.c b/arm64/kusswurm_ch12.c new file mode 100644 index 0000000..2626f6d --- /dev/null +++ b/arm64/kusswurm_ch12.c @@ -0,0 +1,42 @@ +// ================================================================================================ +// Code written while reading chapter 12 of Modern Arm Assembly Language Programming +// +// License: +// Copyright (c) 2026 Hunter Kvalevog +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE. +// ================================================================================================ + +#include <stdint.h> +#include <stdio.h> + +// example 1 +int32_t test_sum10i32(int32_t a, int32_t b, int32_t c, int32_t d, int32_t e, int32_t f, int32_t g, + int32_t h, int32_t i, int32_t j); + +// example 2 +int32_t test_sum4i32(int32_t i1, int32_t i2, int32_t i3, int32_t i4); + +int32_t c_sum2i32(int32_t i1, int32_t i2) +{ + // make this function complicated + printf("c_sum2i32(i1=%d, i2=%d)\n", i1, i2); + return i1 + i2; +} + +int main(int argc, const char* argv[]) +{ + (void)argc; (void)argv; +#define X(FMT, CODE) printf(#CODE ": " FMT "\n", CODE) + + // example 1 + X("%d", test_sum10i32(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); + + // example 2 + X("%d", test_sum4i32(1, 2, 3, 4)); +} + diff --git a/arm64/mariokartwii_ch07.S b/arm64/mariokartwii_ch07.S new file mode 100644 index 0000000..5221b1f --- /dev/null +++ b/arm64/mariokartwii_ch07.S @@ -0,0 +1,46 @@ +// ================================================================================================ +// Code written while following chapter 7 of the MarioKart Wii ARM64 assembly tutorial: +// https://mariokartwii.com/arm64/ch7.html +// +// License: +// Copyright (c) 2026 Hunter Kvalevog +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE. +// ================================================================================================ + +#include "../_/arm64/common.S" + +TEXT_SEGMENT + +.global SYM(main) + +SYM(main): + // Add + mov x0, #1 + mov x1, #2 + add x2, x0, x1 + + // Add immediate + add x3, x2, #0x100 + + // Subtract immediate + mov x4, x3 + sub x4, x4, #0x100 + + // Multiply + mov x5, #5 + mul x5, x4, x5 + + // Divide + udiv x6, x5, x4 + mov x7, #-1 + sdiv x7, x6, x7 + + neg x8, x7 + + brk #0 // (lldb) register read + diff --git a/arm64/mariokartwii_ch08.S b/arm64/mariokartwii_ch08.S new file mode 100644 index 0000000..e4853e1 --- /dev/null +++ b/arm64/mariokartwii_ch08.S @@ -0,0 +1,38 @@ +// ================================================================================================ +// Code written while following chapter 8 of the MarioKart Wii ARM64 assembly tutorial: +// https://mariokartwii.com/arm64/ch8.html +// +// License: +// Copyright (c) 2026 Hunter Kvalevog +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE. +// ================================================================================================ + +#include "../_/arm64/common.S" + +TEXT_SEGMENT + +.global SYM(main) + +SYM(main): + // move shift + mov x0, #0xFFFFFFFFFFFFFFFF + lsl x0, x0, #32 + + mov x1, #0xFFFFFFFFFFFFFFFF + movk x1, #0x1234, lsl 32 + + mov x2, #0xFFFFFFFFFFFFFFFF + movz x2, #0x1234, lsl 32 + + mov x3, #0xFFFFFFFFFFFFFFFF + movk x3, #0x1111, lsl 48 + movk x3, #0x2222, lsl 32 + movk x3, #0x3333, lsl 16 + movk x3, #0x4444 + + brk #0 // (lldb) register read diff --git a/arm64/meson.build b/arm64/meson.build new file mode 100644 index 0000000..686cff4 --- /dev/null +++ b/arm64/meson.build @@ -0,0 +1,6 @@ +project('arm64', 'c', default_options: [ 'warning_level=3' ]) + +executable('kusswurm_ch11', [ 'kusswurm_ch11.c', 'kusswurm_ch11.S' ]) +executable('kusswurm_ch12', [ 'kusswurm_ch12.c', 'kusswurm_ch12.S' ]) +executable('mariokartwii_ch07', 'mariokartwii_ch07.S') +executable('mariokartwii_ch08', 'mariokartwii_ch08.S') |
