From f0b8e96b51b1a8b19365c9d010639c8d67a42506 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Thu, 19 Sep 2024 20:06:53 +0200 Subject: initial commit --- .gitignore | 4 ++++ Makefile | 4 ++++ index.html | 9 ++++++++ main.asm | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ recources.md | 3 +++ sys.asm | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 147 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 index.html create mode 100644 main.asm create mode 100644 recources.md create mode 100644 sys.asm diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e36c9ec --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +gf/ +fasm/ +main +*.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e01d065 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +make: + ./fasm/fasm main.asm + chmod +x main + ./main diff --git a/index.html b/index.html new file mode 100644 index 0000000..9175f0f --- /dev/null +++ b/index.html @@ -0,0 +1,9 @@ + + + asmsite + + + +

Hello World

+ + diff --git a/main.asm b/main.asm new file mode 100644 index 0000000..7644482 --- /dev/null +++ b/main.asm @@ -0,0 +1,59 @@ +include 'sys.asm' + +format ELF64 executable + +segment writable readable +struc db [data] +{ + common + . db data + .size = $ - . +} +stat_msg db 'Get file stats', 10 +open_msg db 'Open file', 10 +read_msg db 'Read file', 10 +close_msg db 'Close file', 10 +msg_dn db ':', 10 + +indexname db 'index.html' +;; indexstat stat +indexlen rq 1 + +indexfd dd 0 +indexbuf dd ? +indexbuf_len rq 1 + +segment readable executable + +entry _main +_main: + ls_write STDOUT, open_msg, open_msg.size + ls_open indexname, O_RDONLY, 0 + mov qword [indexfd], rax + cmp [indexfd], 0 + jl error + + ls_write STDOUT, read_msg, read_msg.size + ls_read qword [indexfd], indexbuf, indexbuf_len + cmp qword rax, 0 + jl error + mov [indexlen], rax + + ls_write STDOUT, close_msg, close_msg.size + ls_close qword [indexfd] + cmp qword rax, 0 + jl error + + ls_write STDOUT, indexname, indexname.size + ls_write STDOUT, msg_dn, msg_dn.size + ls_write STDOUT, indexbuf, [indexlen] + cmp qword rax, 0 + jl error + + + + ls_exit 0 + +error: + ls_close qword [indexfd] + ls_exit 1 diff --git a/recources.md b/recources.md new file mode 100644 index 0000000..d1199b6 --- /dev/null +++ b/recources.md @@ -0,0 +1,3 @@ +# Links +* [syscall table](https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/) +* [manual](https://flatassembler.net/docs.php?article=manual#1.2.6) diff --git a/sys.asm b/sys.asm new file mode 100644 index 0000000..b9ab190 --- /dev/null +++ b/sys.asm @@ -0,0 +1,68 @@ +STDIN equ 0 +STDOUT equ 1 +STDERR equ 2 + +O_RDONLY equ 0 +O_WRONLY equ 1 +O_RDWR equ 2 + +macro scall1 v_rax, v_rdi +{ + mov rax, v_rax + mov rdi, v_rdi + syscall +} + +macro scall2 v_rax, v_rdi, v_rsi +{ + mov rax, v_rax + mov rdi, v_rdi + mov rsi, v_rsi + syscall +} + +macro scall3 v_rax, v_rdi, v_rsi, v_rdx +{ + mov rax, v_rax + mov rdi, v_rdi + mov rsi, v_rsi + mov rdx, v_rdx + syscall +} + +macro ls_read fd, buf, count { scall3 0, fd, buf, count } + +macro ls_write fd, buf, count { scall3 1, fd, buf, count } + +macro ls_open filename, flags, mode { scall3 2, filename, flags, mode } + +macro ls_close fd { scall1 3, fd } + +;; kernel: arch/x86/include/uapi/asm/stat.h +;; struc stat +;; { +;; .st_dev rd 1 +;; .st_ino rd 1 +;; .st_mode rw 1 +;; .st_nlink rw 1 +;; .st_uid rw 1 +;; .st_gid rw 1 +;; .st_rdev rd 1 +;; .st_size rd 1 +;; .st_blksize rd 1 +;; .st_blocks rd 1 +;; +;; .st_atime rd 1 +;; .st_atime_nsec rd 1 +;; .st_mtime rd 1 +;; .st_mtime_nsec rd 1 +;; .st_ctime rd 1 +;; .st_ctime_nsec rd 1 +;; +;; .__unused4 rd 1 +;; .__unused5 rd 1 +;; } +macro ls_stat filename, statbuf { scall2 4, filename, statbuf } + +macro ls_exit status { scall1 60, status } + -- cgit v1.2.3