From 49ad11a8819ebeb78895a7fd6e1c7664f2ad08b4 Mon Sep 17 00:00:00 2001 From: Hunter Kvalevog Date: Tue, 9 Jun 2026 21:20:49 -0500 Subject: --- aarch64/kusswurm_ch11.S | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ aarch64/kusswurm_ch11.c | 61 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 114 insertions(+), 7 deletions(-) diff --git a/aarch64/kusswurm_ch11.S b/aarch64/kusswurm_ch11.S index 0407e92..fab99db 100644 --- a/aarch64/kusswurm_ch11.S +++ b/aarch64/kusswurm_ch11.S @@ -63,3 +63,63 @@ SYM(i32_mul_safe): // 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 + diff --git a/aarch64/kusswurm_ch11.c b/aarch64/kusswurm_ch11.c index 4edfba0..58ca79f 100644 --- a/aarch64/kusswurm_ch11.c +++ b/aarch64/kusswurm_ch11.c @@ -1,3 +1,17 @@ +// ================================================================================================ +// Code written while reading chapter 11 of Modern Arm Assembly Language Programming +// +// License: +// SPDX-License-Identifier: 0BSD +// 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 @@ -9,18 +23,51 @@ int64_t i64_add_sub(int64_t a, int64_t b, int64_t c); int32_t i32_mul(int32_t a, int32_t b); int64_t i32_mul_safe(int32_t a, int32_t b); -#define PRINT_LOG(FMT, CODE) printf(#CODE ": " FMT "\n", CODE) +// 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); int main(int argc, const char* argv[]) { +#define X(FMT, CODE) printf(#CODE ": " FMT "\n", CODE) + // example 1 - PRINT_LOG("%d", i32_add_sub(5, 3, 1)); - PRINT_LOG("%lld", i64_add_sub(5, 3, 1)); + X("%d", i32_add_sub(5, 3, 1)); + X("%lld", i64_add_sub(5, 3, 1)); // example 2 - PRINT_LOG("%d", i32_mul(5, 6)); - PRINT_LOG("%d", i32_mul(2000000000, 4)); - PRINT_LOG("%lld", i32_mul_safe(5, 6)); - PRINT_LOG("%lld", i32_mul_safe(2000000000, 4)); + X("%d", i32_mul(5, 6)); + X("%d", i32_mul(2000000000, 4)); + X("%lld", i32_mul_safe(5, 6)); + X("%lld", 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]); } -- cgit v1.2.3