From 23d8f703fdbeb597db4b103c85fb23eb380d50a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Sun, 23 Jun 2019 00:00:03 +0000 Subject: [PATCH] x86 asm: move gnu gas char literals from x86-assembly-cheat --- README.adoc | 14 ++++++++++++++ userland/arch/x86_64/char_literals.S | 25 +++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 userland/arch/x86_64/char_literals.S diff --git a/README.adoc b/README.adoc index 7ba7724..404e9ac 100644 --- a/README.adoc +++ b/README.adoc @@ -12363,6 +12363,20 @@ When reading disassembly, many instructions have either a `.n` or `.w` suffix. Bibliography: https://stackoverflow.com/questions/27147043/n-suffix-to-branch-instruction +==== GNU GAS assembler char literals + +link:userland/arch/x86_64/char_literals.S[] + +http://stackoverflow.com/questions/33246811/how-to-use-character-literals-in-gnu-gas-to-replace-numbers + +This syntax plays horribly with the C preprocessor: + +.... +MACRO($'a) +.... + +fails because cpp treats string and char literals magically. + === NOP instructions * x86: link:userland/arch/x86_64/nop.S[NOP] diff --git a/userland/arch/x86_64/char_literals.S b/userland/arch/x86_64/char_literals.S new file mode 100644 index 0000000..b2b8197 --- /dev/null +++ b/userland/arch/x86_64/char_literals.S @@ -0,0 +1,25 @@ +/* https://github.com/cirosantilli/linux-kernel-module-cheat#gnu-gas-assembler-char-literals */ + +#include + +LKMC_PROLOGUE + mov $0, %r12 + + /* Memory. */ + mov mychar, %r12b + LKMC_ASSERT_EQ(%r12, $0x61) + + /* Immediate. Without the `$`, does a memory access, and segfaults! */ + mov $'b, %r12b + LKMC_ASSERT_EQ(%r12, $0x62) + + /* Space character works. */ + mov $' , %r12b + LKMC_ASSERT_EQ(%r12, $0x20) + + /* Backslash escapes work. */ + mov $'\n , %r12b + LKMC_ASSERT_EQ(%r12, $0x0A) +LKMC_EPILOGUE +mychar: + .byte 'a