From 838285304337c7b6957fab08df46fa200875177e Mon Sep 17 00:00:00 2001 From: Perry Kivolowitz Date: Thu, 23 Feb 2023 13:42:45 -0600 Subject: [PATCH] updated some samples --- .vscode/sftp.json | 12 ++ section_1/regs/align.s | 42 +++++-- section_1/regs/apple-linux-convergence.S | 151 +++++++++++++++++++++++ section_1/regs/badstack.s | 3 + section_1/regs/quinn.S | 2 +- 5 files changed, 199 insertions(+), 11 deletions(-) create mode 100644 .vscode/sftp.json create mode 100644 section_1/regs/apple-linux-convergence.S diff --git a/.vscode/sftp.json b/.vscode/sftp.json new file mode 100644 index 0000000..09d80c7 --- /dev/null +++ b/.vscode/sftp.json @@ -0,0 +1,12 @@ +{ + "name": "My Server", + "host": "localhost", + "protocol": "sftp", + "port": 2222, + "username": "user", + "remotePath": "./asm_book", + "uploadOnSave": true, + "useTempFile": false, + "openSsh": true, + "password": "a" +} diff --git a/section_1/regs/align.s b/section_1/regs/align.s index 675222b..a4a3356 100644 --- a/section_1/regs/align.s +++ b/section_1/regs/align.s @@ -1,15 +1,37 @@ - .global main - .text - .align 2 +#include "apple-linux-convergence.S" -main: mov x0, xzr - ldr x1, =ram - strb w0, [x1] - strh w0, [x1] - str w0, [x1] - str x0, [x1] +/* The purpose of this program is to use gdb or lldb to watch a + region of memory get overwritten first with a byte, then a short, + then an int and finally with a long. + + As can be seen, the program produces no output of its own. Rather, + use of a debugger is needed. + + The gdb command to examine the memory located at "ram" is: + x/xg $x1 +*/ + + GLABEL main + .text + .p2align 2 + + +MAIN + START_PROC + PUSH_P x29, x30 + mov x29, sp + + mov x0, xzr + ldr x1, =ram + strb w0, [x1] + strh w0, [x1] + str w0, [x1] + str x0, [x1] + + POP_P x29, x30 ret - + END_PROC + .data ram: .quad 0xFFFFFFFFFFFFFFFF diff --git a/section_1/regs/apple-linux-convergence.S b/section_1/regs/apple-linux-convergence.S new file mode 100644 index 0000000..c60d2da --- /dev/null +++ b/section_1/regs/apple-linux-convergence.S @@ -0,0 +1,151 @@ +/* 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 diff --git a/section_1/regs/badstack.s b/section_1/regs/badstack.s index a2ef4b0..e1e3b09 100644 --- a/section_1/regs/badstack.s +++ b/section_1/regs/badstack.s @@ -1,3 +1,6 @@ +/* The purpose of this program is to crash due to not manipulating the + stack in multiples of 16. +*/ .global main .text .align 2 diff --git a/section_1/regs/quinn.S b/section_1/regs/quinn.S index 8e5c2a4..5cdd3e4 100644 --- a/section_1/regs/quinn.S +++ b/section_1/regs/quinn.S @@ -22,6 +22,6 @@ main: stp x29, x30, [sp, -16]! .data -fmt: .asciz "Number: %d\n" +fmt: .asciz "Number: %d\n" .end