aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHunter Kvalevog <hunter@kvog.sh>2026-10-12 21:26:35 -0500
committerHunter Kvalevog <hunter@kvog.sh>2026-10-16 18:56:17 -0500
commitfeab53020b21fed734faac2c09bd7d520849d813 (patch)
tree2fe0d8fdc2b87b4e0257cd5a4d61b118b31ef468
parentb7bd430a2ceccc06dea5cc82ef2518797ab342ad (diff)
downloaddemos-feab53020b21fed734faac2c09bd7d520849d813.tar.gz
demos-feab53020b21fed734faac2c09bd7d520849d813.zip
aarch64: start on Kusswurm chapter 12
-rw-r--r--aarch64/kusswurm_ch12.S97
-rw-r--r--aarch64/kusswurm_ch12.c42
-rw-r--r--aarch64/meson.build1
3 files changed, 140 insertions, 0 deletions
diff --git a/aarch64/kusswurm_ch12.S b/aarch64/kusswurm_ch12.S
new file mode 100644
index 0000000..27b29cb
--- /dev/null
+++ b/aarch64/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 "../_common/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
new file mode 100644
index 0000000..2626f6d
--- /dev/null
+++ b/aarch64/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/aarch64/meson.build b/aarch64/meson.build
index 440053a..f33a462 100644
--- a/aarch64/meson.build
+++ b/aarch64/meson.build
@@ -1,5 +1,6 @@
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')