From c020675cd7287cbad80ce405ccf013dfc878885c Mon Sep 17 00:00:00 2001 From: Perry Kivolowitz Date: Tue, 20 Feb 2024 13:59:39 -0600 Subject: [PATCH] wrote apple-exit.c and s --- section_1/funcs/apple-exit.c | 8 ++ section_1/funcs/apple-exit.s | 20 +++ section_1/funcs/apple-linux-convergence.S | 156 ++++++++++++++++++++++ section_1/funcs/direct-syscall.S | 23 ++++ 4 files changed, 207 insertions(+) create mode 100644 section_1/funcs/apple-exit.c create mode 100644 section_1/funcs/apple-exit.s create mode 100644 section_1/funcs/apple-linux-convergence.S create mode 100644 section_1/funcs/direct-syscall.S diff --git a/section_1/funcs/apple-exit.c b/section_1/funcs/apple-exit.c new file mode 100644 index 0000000..c4dc8bc --- /dev/null +++ b/section_1/funcs/apple-exit.c @@ -0,0 +1,8 @@ +/* +*/ + +#include + +int main() { + exit(3510); +} diff --git a/section_1/funcs/apple-exit.s b/section_1/funcs/apple-exit.s new file mode 100644 index 0000000..f62e98b --- /dev/null +++ b/section_1/funcs/apple-exit.s @@ -0,0 +1,20 @@ + .section __TEXT,__text,regular,pure_instructions + .build_version macos, 14, 0 sdk_version 14, 2 + .globl _main ; -- Begin function main + .p2align 2 +_main: ; @main + .cfi_startproc +; %bb.0: + sub sp, sp, #32 + .cfi_def_cfa_offset 32 + stp x29, x30, [sp, #16] ; 16-byte Folded Spill + add x29, sp, #16 + .cfi_def_cfa w29, 16 + .cfi_offset w30, -8 + .cfi_offset w29, -16 + stur wzr, [x29, #-4] + mov w0, #3510 + bl _exit + .cfi_endproc + ; -- End function +.subsections_via_symbols diff --git a/section_1/funcs/apple-linux-convergence.S b/section_1/funcs/apple-linux-convergence.S new file mode 100644 index 0000000..8827423 --- /dev/null +++ b/section_1/funcs/apple-linux-convergence.S @@ -0,0 +1,156 @@ +/* Macros to permit the "same" assembly language to build on ARM64 + Linux systems as well as Apple Silicon systems. + + See the fuller documentation at: + https://github.com/pkivolowitz/asm_book/blob/main/macros/README.md + + Perry Kivolowitz + A Gentle Introduction to Assembly Language +*/ + +.macro GLD_PTR xreg, label +#if defined(__APPLE__) + adrp \xreg, _\label@GOTPAGE + ldr \xreg, [\xreg, _\label@GOTPAGEOFF] +#else + ldr \xreg, =\label + ldr \xreg, [\xreg] +#endif +.endm + +.macro GLD_ADDR xreg, label // Get a global address +#if defined(__APPLE__) + adrp \xreg, _\label@GOTPAGE + add \xreg, \xreg, _\label@GOTPAGEOFF +#else + ldr \xreg, =\label +#endif +.endm + +.macro LLD_ADDR xreg, label +#if defined(__APPLE__) + adrp \xreg, \label@PAGE + add \xreg, \xreg, \label@PAGEOFF +#else + ldr \xreg, =\label +#endif +.endm + +.macro LLD_DBL xreg, dreg, label +#if defined(__APPLE__) + adrp \xreg, \label@PAGE + add \xreg, \xreg, \label@PAGEOFF + ldur \dreg, [\xreg] +// fmov \dreg, \xreg +#else + ldr \xreg, =\label + ldur \dreg, [\xreg] +#endif +.endm + +.macro LLD_FLT xreg, sreg, label +#if defined(__APPLE__) + adrp \xreg, \label@PAGE + add \xreg, \xreg, \label@PAGEOFF + ldur \sreg, [\xreg] +#else + ldr \xreg, =\label + ldur \sreg, [\xreg] +#endif +.endm + +.macro GLABEL label +#if defined(__APPLE__) + .global _\label +#else + .global \label +#endif +.endm + +.macro MAIN +#if defined(__APPLE__) +_main: +#else +main: +#endif +.endm + +/* Fetching the address of the externally defined errno is quite + different on Apple and Linux. This macro leaves the address of + errno in x0. +*/ +.macro ERRNO_ADDR +#if defined(__APPLE__) + bl ___error +#else + bl __errno_location +#endif +.endm + +.macro CRT label +#if defined(__APPLE__) + bl _\label +#else + bl \label +#endif +.endm + +.macro START_PROC // after starting label + .cfi_startproc +.endm + +.macro END_PROC // after the return + .cfi_endproc +.endm + +.macro PUSH_P a, b + stp \a, \b, [sp, -16]! +.endm + +.macro PUSH_R a + str \a, [sp, -16]! +.endm + +.macro POP_P a, b + ldp \a, \b, [sp], 16 +.endm + +.macro POP_R a + ldr \a, [sp], 16 +.endm + +/* The smaller of src_a and src_b is put into dest. A cmp instruction + or other instruction that sets the flags must be performed first. + This macro makes it easy to remember which register does what in the + csel. + + Thank you to u/TNorthover for nudge to add the cmp. +*/ + +.macro MIN src_a, src_b, dest + cmp \src_a, \src_b + csel \dest, \src_a, \src_b, LT +.endm + +/* The larger of src_a and src_b is put into dest. A cmp instruction + or other instruction that sets the flags must be performed first. + This macro makes it easy to remember which register does what in the + csel. + + Thank you to u/TNorthover for nudge to add the cmp. +*/ + +.macro MAX src_a, src_b, dest + cmp \src_a, \src_b + csel \dest, \src_a, \src_b, GT +.endm + +.macro AASCIZ label, string + .p2align 2 +\label: .asciz "\string" +.endm + +.macro MOD src_a, src_b, dest, scratch + sdiv \scratch, \src_a, \src_b + msub \dest, \scratch, \src_b, \src_a +.endm diff --git a/section_1/funcs/direct-syscall.S b/section_1/funcs/direct-syscall.S new file mode 100644 index 0000000..db8514c --- /dev/null +++ b/section_1/funcs/direct-syscall.S @@ -0,0 +1,23 @@ +/* Linux-only by intention as Apple does not conform the the ARM Linux + system call conventions. Here, Apple would use x16 where this code + uses x8 and a value of 1 where this code uses 93. + + Both platforms are handled correctly if you use the libcrt. + + Also note that the return from main is limited to 8 bits. Thus, the + value used here (3510) will not yield that value, but 182 instead. +*/ + + .p2align 2 + .text + .global main + +main: + stp x29, x30, [sp, -16]! + mov x0, 3510 + mov x8, 93 + svc 0 + ldp x29, x30, [sp], 16 + ret + + .end