From 0b8e618c7e32bd22158ee74bc3cf095b1b262032 Mon Sep 17 00:00:00 2001 From: Clemens Fries Date: Wed, 5 Dec 2018 18:19:52 +0100 Subject: Add some 64 bit assembly program from around 2010 --- README.adoc | 7 +++++++ linux64/Makefile | 3 +++ linux64/foo.asm | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 linux64/Makefile create mode 100644 linux64/foo.asm diff --git a/README.adoc b/README.adoc index 9ee985a..f818e56 100644 --- a/README.adoc +++ b/README.adoc @@ -1,4 +1,11 @@ == Assembly Stuff +=== `boot/` + Trying to do some low-level i386 assembly. I apparently succeeded loading some data from a virtual floppy disk. Maybe, one day, I'll try to do more. + +=== `linux64/` + +Wrote a small program for amd64 Linux that uses a syscall to print its first +parameter to stdout. diff --git a/linux64/Makefile b/linux64/Makefile new file mode 100644 index 0000000..5254a2f --- /dev/null +++ b/linux64/Makefile @@ -0,0 +1,3 @@ +default: + nasm -f elf64 foo.asm + ld -o foo foo.o diff --git a/linux64/foo.asm b/linux64/foo.asm new file mode 100644 index 0000000..38dcca0 --- /dev/null +++ b/linux64/foo.asm @@ -0,0 +1,55 @@ +bits 64 + +section .data + usage: db 'usage: ', 0x00 + params: db ' [string]', 0x00 + newline: db 0x0a, 0x00 + +section .text + global _start + +_start: + pop rax ; argc is on stack + cmp rax,1 ; see if is 1 (no parameters) + je help + pop rsi ; program name + pop rsi ; first parameter + call print + mov rsi, newline + call print + jmp exit + +help: + mov rsi, usage + call print + pop rsi + call print + mov rsi, params + call print + mov rsi, newline + call print + + jmp exit + +print: + mov rdi, rsi ; copy, scasb uses edi + xor al, al ; al = 0 + mov rcx, 4096 ; maximum length 4096 + ; this is used by REPNE + ; and not specific to SCASB + cld + repne scasb ; scan for '\0' + ; edi is now address of '\0' + sub rdi, rsi ; rdi is now length of string + mov rdx, rdi ; rdx : write(..., strlen) + sub rdx, 1 ; skip '\0' + + mov rax, 1 ; syscall write() + mov rdi, 1 ; rdi : write(fd, ...) + syscall + ret + +exit: + mov rax, 60 ; exit() + mov rdi, 0 + syscall -- cgit