From f584354075d57ab5e27485835ed66a6219de66cd Mon Sep 17 00:00:00 2001 From: Perry Kivolowitz Date: Tue, 17 Jan 2023 12:04:36 -0600 Subject: [PATCH] renaming and changes to apple macros --- README.md | 35 ++++++---- macros/README.md | 1 + macros/apple-linux-convergence.S | 110 +++++++++++++++++++++++++++++++ more/apple_silicon/macros.S | 52 --------------- 4 files changed, 134 insertions(+), 64 deletions(-) create mode 100644 macros/README.md create mode 100644 macros/apple-linux-convergence.S delete mode 100644 more/apple_silicon/macros.S diff --git a/README.md b/README.md index c9b5efc..df9f3b7 100644 --- a/README.md +++ b/README.md @@ -47,20 +47,27 @@ and how parameters are passed. In this book we will use the ARM LINUX conventions. This means: * You *may* need to run a ARM Linux VM on the Macintosh - even on - ARM-based Macs. Why? Apple uses a different calling convention. + ARM-based Macs. Why? Apple uses a different calling convention. Keep + reading before you get upset. The convention used in this book should work on all ARM Linux machines while the Apple calling convention is specific to Apple Silicon-based machines. - This necessity did not sit well with some on reddit. We listened. + This necessity for a VM even when running on an Apple Silicon machine + did not sit well with some on reddit. We listened. We now have a chapter devoted to bringing Linux and Apple code - together to the degree possible. [This chapter](./more/apple_silicon/) - also provides a suite of macros that provide this help. If you're - willing to adjust how you code (and use the macros), you can - successfully write assembly language once and build it on both Linux - and Mac OS. + together to the degree possible. + + [This chapter](./more/apple_silicon/) provides a suite of macros that + provide this help. If you're willing to adjust how you code (and use + the macros), you can successfully write assembly language once and + build it on both Linux and Mac OS. + + The macros are a work in progress. [This + link](./macros/apple-linux-convergence.S) will lead to a current copy + of them. We will try to keep this file up to date. * You will need to run WSL (Windows Subsystem for Linux) on ARM-based Windows machines. These do exist! @@ -125,6 +132,10 @@ to only one step in a build sequence. What we talk about as being the `#include`. These commands are not part of C or C++. Rather they are commands to the preprocessor. + Note that `gcc` will invoke the C preprocessor only if your assembly + language file ends in `.S` - capital S. It may not be invoked if your + file ends in a lower case s or any other file extension. + * The *actual* compiler, whose job it is turn high level languages such as C and C++ into assembly language. @@ -265,14 +276,14 @@ own section: | Chapter | Markdown | PDF | | ------- | -------- | --- | | 1 | Floating Point | | -| .... a | [.... What Are Floating Point Numbers?](./section_2/float/what.md) | NA | -| .... b | [.... Registers (simplified)](./section_2/float/working.md) | NA | -| .... c | [.... Literals](./section_2/float/literals.md) | NA | +| .... a | [.... What Are Floating Point Numbers?](./section_2/float/what.md) | [Link](./section_2/float/what.pdf) | +| .... b | [.... Registers (simplified)](./section_2/float/working.md) | [Link](./section_2/float/working.pdf) | +| .... c | [.... Literals](./section_2/float/literals.md) | [Link](./section_2/float/literals.pdf) | | .... d | [.... `fmov` Not Yet Written](./section_2/float/) | NA | -| .... e | [.... Conversion To / From Integers](./section_2/float/rounding.md) | NA | +| .... e | [.... Conversion To / From Integers](./section_2/float/rounding.md) | [Link](./section_2/float/rounding.pdf) | | .... f | [.... Four Basic Operations Not Yet Written](./section_2/float/) | NA | | .... g | [.... Selected Additional Operations Not Yet Written](./section_2/float/) | NA | -| .... z | [.... Half Precision Floats](./section_2/float/half.md) | NA | +| .... z | [.... Half Precision Floats](./section_2/float/half.md) | [Link](./section_2/float/half.pdf) | ## Section 3 - Bit Manipulation diff --git a/macros/README.md b/macros/README.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/macros/README.md @@ -0,0 +1 @@ + diff --git a/macros/apple-linux-convergence.S b/macros/apple-linux-convergence.S new file mode 100644 index 0000000..3661e80 --- /dev/null +++ b/macros/apple-linux-convergence.S @@ -0,0 +1,110 @@ +// Macros to permit the "same" assembly language to build on ARM64 +// Linux systems as well as Apple Silicon systems. +// +// Perry Kivolowitz +// A Gentle Introduction to Assembly Language + +#if defined(__APPLE__) + +// Apple makes a distinction between loading something close by +// versus something global. Note the use of GOTPAGE rather then +// PAGE. +// +// Note: this macro dereferences the label getting what is at +// the label's address. + +.macro GLD_PTR xreg, label // Dereference a global * + adrp \xreg, _\label@GOTPAGE + ldr \xreg, [\xreg, _\label@GOTPAGEOFF] +.endm + +.macro GLD_ADDR xreg, label // Get a global address + adrp \xreg, _\label@GOTPAGE + add \xreg, \xreg, _\label@GOTPAGEOFF +.endm + +// This macro loads the address of a nearby label. + +.macro LLD_ADDR xreg, label // Load a local address + adrp \xreg, \label@PAGE + add \xreg, \xreg, \label@PAGEOFF +.endm + +.macro GLABEL label + .global _\label +.endm + +.macro MAIN +_main: +.endm + +.macro CRT label + bl _\label +.endm + +#else // LINUX + +.macro GLABEL label + .global \label +.endm + +.macro MAIN +main: +.endm + +.macro CRT label + bl \label +.endm + +// This macro treats label as a pointer and dereferences it. +// That is, it puts into the xreg what is found at the address +// of the label. + +.macro GLD_PTR xreg, label // Dereference a global * + ldr \xreg, =\label + ldr \xreg, [\xreg] +.endm + +// This macro loads the address of a nearby label. + +.macro LLD_ADDR xreg, label + ldr \xreg, =\label +.endm + + +.macro LD_ADDR xreg, label +.endm + +#endif + +.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 + +.macro MIN src_a, src_b, dest + csel \dest, \src_a, \src_b, GT +.endm + +.macro MAX src_a, src_b, dest + csel \dest, \src_a, \src_b, LT +.endm diff --git a/more/apple_silicon/macros.S b/more/apple_silicon/macros.S deleted file mode 100644 index d506b73..0000000 --- a/more/apple_silicon/macros.S +++ /dev/null @@ -1,52 +0,0 @@ -// Macros to permit the "same" assembly language to build on ARM64 -// Linux systems as well as Apple Silicon systems. -// -// Perry Kivolowitz -// A Gentle Introduction to Assembly Language - -#if defined(__APPLE__) - -.macro LD_ADDR xreg, label - adrp \xreg, \label@PAGE - add \xreg, \xreg, \label@PAGEOFF -.endm - -.macro GLABEL label - .global _\label -.endm - -.macro MAIN -_main: -.endm - -.macro CRT label - bl _\label -.endm - -#else - -.macro GLABEL label - .global \label -.endm - -.macro MAIN -main: -.endm - -.macro CRT label - bl \label -.endm - -.macro LD_ADDR xreg, label - ldr \xreg, =\label -.endm - -#endif - -.macro START_PROC - .cfi_startproc -.endm - -.macro END_PROC - .cfi_endproc -.endm