From 5a0619780e4ae647307136f533d666be15bb9843 Mon Sep 17 00:00:00 2001 From: Hunter Kvalevog Date: Wed, 28 Oct 2026 16:05:43 -0500 Subject: aarch64 -> arm64 --- README | 2 +- _/aarch64/common.S | 21 ------- _/arm64/common.S | 21 +++++++ aarch64/kusswurm_ch11.S | 145 -------------------------------------------- aarch64/kusswurm_ch11.c | 87 -------------------------- aarch64/kusswurm_ch12.S | 97 ----------------------------- aarch64/kusswurm_ch12.c | 42 ------------- aarch64/mariokartwii_ch07.S | 46 -------------- aarch64/mariokartwii_ch08.S | 38 ------------ aarch64/meson.build | 6 -- arm64/kusswurm_ch11.S | 145 ++++++++++++++++++++++++++++++++++++++++++++ arm64/kusswurm_ch11.c | 87 ++++++++++++++++++++++++++ arm64/kusswurm_ch12.S | 97 +++++++++++++++++++++++++++++ arm64/kusswurm_ch12.c | 42 +++++++++++++ arm64/mariokartwii_ch07.S | 46 ++++++++++++++ arm64/mariokartwii_ch08.S | 38 ++++++++++++ arm64/meson.build | 6 ++ rust/asm/build.sh | 2 +- rust/asm/main_aarch64.S | 35 ----------- rust/asm/main_arm64.S | 35 +++++++++++ 20 files changed, 519 insertions(+), 519 deletions(-) delete mode 100644 _/aarch64/common.S create mode 100644 _/arm64/common.S delete mode 100644 aarch64/kusswurm_ch11.S delete mode 100644 aarch64/kusswurm_ch11.c delete mode 100644 aarch64/kusswurm_ch12.S delete mode 100644 aarch64/kusswurm_ch12.c delete mode 100644 aarch64/mariokartwii_ch07.S delete mode 100644 aarch64/mariokartwii_ch08.S delete mode 100644 aarch64/meson.build create mode 100644 arm64/kusswurm_ch11.S create mode 100644 arm64/kusswurm_ch11.c create mode 100644 arm64/kusswurm_ch12.S create mode 100644 arm64/kusswurm_ch12.c create mode 100644 arm64/mariokartwii_ch07.S create mode 100644 arm64/mariokartwii_ch08.S create mode 100644 arm64/meson.build delete mode 100644 rust/asm/main_aarch64.S create mode 100644 rust/asm/main_arm64.S diff --git a/README b/README index ef3fd46..2151aab 100644 --- a/README +++ b/README @@ -7,7 +7,7 @@ Home page https://git.kvog.sh/demos Mirror https://codeberg.org/kvog/demos Mirror https://github.com/kvog-git/demos -aarch64/ AArch64 assembly experiments +arm64/ ARM64 assembly experiments ffshadertoy Basic offline Shadertoy renderer flacdec FLAC decoder gl-asylum Procedurally generated 3D OpenGL scene diff --git a/_/aarch64/common.S b/_/aarch64/common.S deleted file mode 100644 index 616af28..0000000 --- a/_/aarch64/common.S +++ /dev/null @@ -1,21 +0,0 @@ -// ================================================================================================ -// Shared macros -// -// 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. -// ================================================================================================ - -#ifdef __APPLE__ -# define SYM(LABEL) _##LABEL -# define TEXT_SEGMENT .section __TEXT,__text -#else -# define SYM(LABEL) LABEL -# define TEXT_SEGMENT .section .text -#endif - diff --git a/_/arm64/common.S b/_/arm64/common.S new file mode 100644 index 0000000..616af28 --- /dev/null +++ b/_/arm64/common.S @@ -0,0 +1,21 @@ +// ================================================================================================ +// Shared macros +// +// 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. +// ================================================================================================ + +#ifdef __APPLE__ +# define SYM(LABEL) _##LABEL +# define TEXT_SEGMENT .section __TEXT,__text +#else +# define SYM(LABEL) LABEL +# define TEXT_SEGMENT .section .text +#endif + diff --git a/aarch64/kusswurm_ch11.S b/aarch64/kusswurm_ch11.S deleted file mode 100644 index 16cc840..0000000 --- a/aarch64/kusswurm_ch11.S +++ /dev/null @@ -1,145 +0,0 @@ -// ================================================================================================ -// 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 "../_/aarch64/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/aarch64/kusswurm_ch11.c b/aarch64/kusswurm_ch11.c deleted file mode 100644 index ec832f4..0000000 --- a/aarch64/kusswurm_ch11.c +++ /dev/null @@ -1,87 +0,0 @@ -// ================================================================================================ -// 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 -#include -#include - -// 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/aarch64/kusswurm_ch12.S b/aarch64/kusswurm_ch12.S deleted file mode 100644 index 0e321d3..0000000 --- a/aarch64/kusswurm_ch12.S +++ /dev/null @@ -1,97 +0,0 @@ -// ================================================================================================ -// 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 "../_/aarch64/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/aarch64/kusswurm_ch12.c b/aarch64/kusswurm_ch12.c deleted file mode 100644 index 2626f6d..0000000 --- a/aarch64/kusswurm_ch12.c +++ /dev/null @@ -1,42 +0,0 @@ -// ================================================================================================ -// 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 -#include - -// 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/aarch64/mariokartwii_ch07.S b/aarch64/mariokartwii_ch07.S deleted file mode 100644 index fa1c082..0000000 --- a/aarch64/mariokartwii_ch07.S +++ /dev/null @@ -1,46 +0,0 @@ -// ================================================================================================ -// 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 "../_/aarch64/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/aarch64/mariokartwii_ch08.S b/aarch64/mariokartwii_ch08.S deleted file mode 100644 index f4b4cab..0000000 --- a/aarch64/mariokartwii_ch08.S +++ /dev/null @@ -1,38 +0,0 @@ -// ================================================================================================ -// 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 "../_/aarch64/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/aarch64/meson.build b/aarch64/meson.build deleted file mode 100644 index f33a462..0000000 --- a/aarch64/meson.build +++ /dev/null @@ -1,6 +0,0 @@ -project('aarch64', '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') 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 +#include +#include + +// 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 +#include + +// 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') diff --git a/rust/asm/build.sh b/rust/asm/build.sh index 0d51849..569985f 100755 --- a/rust/asm/build.sh +++ b/rust/asm/build.sh @@ -1,5 +1,5 @@ #!/bin/sh mkdir -p bin -cc -c main_aarch64.S -o bin/asm.o +cc -c main_arm64.S -o bin/asm.o ar rcs bin/libasm.a bin/asm.o rustc --edition 2024 main.rs -L bin -l static=asm --out-dir bin diff --git a/rust/asm/main_aarch64.S b/rust/asm/main_aarch64.S deleted file mode 100644 index 191df8c..0000000 --- a/rust/asm/main_aarch64.S +++ /dev/null @@ -1,35 +0,0 @@ -// ================================================================================================ -// Rust->assembly interop demo -// -// 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 "../../_common/aarch64/common.S" - -TEXT_SEGMENT - -.global SYM(add_u32) -.global SYM(dot_f32v3) - -SYM(add_u32): - add w0, w0, w1 - ret - -SYM(dot_f32v3): - ldp s1, s2, [x0] // a.x, a.y - ldp s3, s4, [x1] // b.x, b.y - ldr s5, [x0, #8] // a.z - ldr s6, [x1, #8] // b.z - - fmul s0, s1, s3 // a.x * b.x - fmadd s0, s2, s4, s0 // + a.y * b.y - fmadd s0, s5, s6, s0 // + a.z * b.z - - ret diff --git a/rust/asm/main_arm64.S b/rust/asm/main_arm64.S new file mode 100644 index 0000000..03c2e12 --- /dev/null +++ b/rust/asm/main_arm64.S @@ -0,0 +1,35 @@ +// ================================================================================================ +// Rust->assembly interop demo +// +// 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(add_u32) +.global SYM(dot_f32v3) + +SYM(add_u32): + add w0, w0, w1 + ret + +SYM(dot_f32v3): + ldp s1, s2, [x0] // a.x, a.y + ldp s3, s4, [x1] // b.x, b.y + ldr s5, [x0, #8] // a.z + ldr s6, [x1, #8] // b.z + + fmul s0, s1, s3 // a.x * b.x + fmadd s0, s2, s4, s0 // + a.y * b.y + fmadd s0, s5, s6, s0 // + a.z * b.z + + ret -- cgit v1.2.3