From 2c894f0c6803cf893fe1c8b10e835831499bb10b Mon Sep 17 00:00:00 2001 From: johannst Date: Sat, 19 Sep 2020 13:14:59 +0200 Subject: x86_64 added asm snippt (Linux) --- src/arch/x86_64.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src') diff --git a/src/arch/x86_64.md b/src/arch/x86_64.md index d55ccd6..964bf00 100644 --- a/src/arch/x86_64.md +++ b/src/arch/x86_64.md @@ -148,6 +148,44 @@ must must save these registers in case they are used. LO ADDR ``` +## ASM skeleton +Small assembler skeleton, ready to use with following properties: +- use raw Linux syscalls (`man 2 syscall` for ABI) +- no `C runtime (crt)` +- gnu assembler [`gas`][gas_doc] +- intel syntax +```x86asm +# file: greet.s + + .intel_syntax noprefix + + .section .text, "ax", @progbits + .global _start +_start: + mov rdi, 1 # fd + lea rsi, [rip + greeting] # buf + mov rdx, [rip + greeting_len] # count + mov rax, 1 # write(2) syscall nr + syscall + + mov rdi, 0 # exit code + mov rax, 1 # exit(2) syscall nr + syscall + + .section .rdonly, "a", @progbits +greeting: + .asciz "Hi ASM-World!\n" +greeting_len: + .int .-greeting +``` +> Syscall numbers are defined in `/usr/include/asm/unistd.h`. + +To compile and run: +```bash +> gcc -o greet greet.s -nostartfiles -nostdlib && ./greet +Hi ASM-World! +``` + ## References - [SystemV AMD64 ABI][sysvabi] - [AMD64 Vol1: Application Programming][amd64_vol1] @@ -167,4 +205,5 @@ must must save these registers in case they are used. [intel64_vol1]: https://software.intel.com/content/www/us/en/develop/download/intel-64-and-ia-32-architectures-software-developers-manual-volume-1-basic-architecture.html [intel64_vol2]: https://software.intel.com/content/www/us/en/develop/download/intel-64-and-ia-32-architectures-sdm-combined-volumes-2a-2b-2c-and-2d-instruction-set-reference-a-z.html [intel64_vol3]: https://software.intel.com/content/www/us/en/develop/download/intel-64-and-ia-32-architectures-sdm-combined-volumes-3a-3b-3c-and-3d-system-programming-guide.html +[gas_doc]: https://sourceware.org/binutils/docs/as [gas_directives]: https://sourceware.org/binutils/docs/as/Pseudo-Ops.html#Pseudo-Ops -- cgit v1.2.3