mirror of
https://github.com/pkivolowitz/asm_book.git
synced 2026-06-23 23:46:48 +08:00
renaming and changes to apple macros
This commit is contained in:
parent
d09b35ed28
commit
f584354075
4 changed files with 134 additions and 64 deletions
35
README.md
35
README.md
|
|
@ -47,20 +47,27 @@ and how parameters are passed.
|
||||||
In this book we will use the ARM LINUX conventions. This means:
|
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
|
* 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
|
The convention used in this book should work on all ARM Linux
|
||||||
machines while the Apple calling convention is specific to Apple
|
machines while the Apple calling convention is specific to Apple
|
||||||
Silicon-based machines.
|
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
|
We now have a chapter devoted to bringing Linux and Apple code
|
||||||
together to the degree possible. [This chapter](./more/apple_silicon/)
|
together to the degree possible.
|
||||||
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
|
[This chapter](./more/apple_silicon/) provides a suite of macros that
|
||||||
successfully write assembly language once and build it on both Linux
|
provide this help. If you're willing to adjust how you code (and use
|
||||||
and Mac OS.
|
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
|
* You will need to run WSL (Windows Subsystem for Linux) on ARM-based
|
||||||
Windows machines. These do exist!
|
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
|
`#include`. These commands are not part of C or C++. Rather they
|
||||||
are commands to the preprocessor.
|
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
|
* The *actual* compiler, whose job it is turn high level languages
|
||||||
such as C and C++ into assembly language.
|
such as C and C++ into assembly language.
|
||||||
|
|
||||||
|
|
@ -265,14 +276,14 @@ own section:
|
||||||
| Chapter | Markdown | PDF |
|
| Chapter | Markdown | PDF |
|
||||||
| ------- | -------- | --- |
|
| ------- | -------- | --- |
|
||||||
| 1 | Floating Point | |
|
| 1 | Floating Point | |
|
||||||
| .... a | [.... What Are Floating Point Numbers?](./section_2/float/what.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) | NA |
|
| .... b | [.... Registers (simplified)](./section_2/float/working.md) | [Link](./section_2/float/working.pdf) |
|
||||||
| .... c | [.... Literals](./section_2/float/literals.md) | NA |
|
| .... c | [.... Literals](./section_2/float/literals.md) | [Link](./section_2/float/literals.pdf) |
|
||||||
| .... d | [.... `fmov` Not Yet Written](./section_2/float/) | NA |
|
| .... 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 |
|
| .... f | [.... Four Basic Operations Not Yet Written](./section_2/float/) | NA |
|
||||||
| .... g | [.... Selected Additional 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
|
## Section 3 - Bit Manipulation
|
||||||
|
|
||||||
|
|
|
||||||
1
macros/README.md
Normal file
1
macros/README.md
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
|
||||||
110
macros/apple-linux-convergence.S
Normal file
110
macros/apple-linux-convergence.S
Normal file
|
|
@ -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
|
||||||
|
|
@ -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
|
|
||||||
Loading…
Reference in a new issue