commit 9b565d7be83b5ec5c9c2ce28a9ce8bb28454307e
parent f52c82446e6fd25cd76d814920b39caf14e5460a
Author: afify <hassan@afify.dev>
Date: Wed, 23 Mar 2022 16:27:01 +0300
[feat] add getchar
Diffstat:
13 files changed, 145 insertions(+), 52 deletions(-)
diff --git a/Makefile b/Makefile
@@ -2,7 +2,7 @@
.PHONY: all options clean
.SUFFIXES: .hex .o
-BIN = execve hi abc
+BIN = execve hi abc getchar
SRC = ${BIN:=.s}
OBJ = ${BIN:=.o}
HEX = ${BIN:=.hex}
diff --git a/abc.s b/abc.s
@@ -1,7 +1,7 @@
; See LICENSE file for copyright and license details.
BITS 64
-%include "syscalls.s"
-%include "portable.s"
+%include "syscalls.inc"
+%include "portable.inc"
section .text
global _start
diff --git a/execve.s b/execve.s
@@ -1,7 +1,7 @@
; See LICENSE file for copyright and license details.
BITS 64
-%include "syscalls.s"
-%include "portable.s"
+%include "syscalls.inc"
+%include "portable.inc"
section .text
global _start
diff --git a/getchar.s b/getchar.s
@@ -0,0 +1,54 @@
+BITS 64
+%include "syscalls.inc"
+%include "macros.inc"
+%include "signals.inc"
+%include "portable.inc"
+
+%define SIGUSR1 10 ; User-defined signal r1
+
+section .rodata
+ usage_msg: db "usage: azan [-AaNnUuv]", 10, 0
+ usage_len: equ $ - usage_msg
+
+section .data
+ dchar: db 1
+ SIGACTION sa
+
+section .bss
+
+section .text
+ global _start
+
+_start:
+
+signal:
+ mov rax, SYS_sigaction
+ mov rdi, SIGUSR1 ;int sig
+ mov rsi, sa ;const struct sigaction * act
+ mov rdx, 0 ;struct sigaction * oact
+ mov r10, 0 ;size_t sigsetsize
+ syscall
+
+read:
+ mov rax, SYS_read
+ mov rdi, STDIN ;unsigned int fd
+ mov rsi, dchar ;char *buf
+ mov rdx, 1 ;size_t count
+ syscall
+
+cmpkey:
+ cmp [dchar], byte 0x71 ; if q => write
+ je exit
+ cmp [dchar], byte 0x73 ; if s => write
+ je writes
+ jne read
+
+writes:
+ mov rax, SYS_write
+ mov rdi, STDOUT ;unsigned int fd
+ mov rsi, usage_msg, ;char *buf
+ mov rdx, usage_len ;size_t count
+ syscall
+
+exit:
+ EEXIT EXIT_SUCCESS
diff --git a/hi.s b/hi.s
@@ -1,7 +1,7 @@
; See LICENSE file for copyright and license details.
BITS 64
-%include "syscalls.s"
-%include "portable.s"
+%include "syscalls.inc"
+%include "portable.inc"
section .text
global _start
diff --git a/linux_x86_64.csv b/linux_x86_64.csv
@@ -1,4 +1,6 @@
%rax,System call,%rdi,%rsi,%rdx,%r10,%r8,%r9
+0,sys_read,unsigned int fd,char * buf,size_t count,,,
1,sys_write,unsigned int fd,const char *buf,size_t count,,,
+13,sys_rt_sigaction,int sig,const struct sigaction * act,struct sigaction * oact,size_t sigsetsize,,
59,sys_execve,const char *filename,const char *const argv[],const char *const envp[],,,
60,sys_exit,int error_code,,,,,
diff --git a/macros.inc b/macros.inc
@@ -0,0 +1,27 @@
+; See LICENSE file for copyright and license details.
+
+%ifndef MACROS_S
+%define MACROS_S
+
+%define EXIT_SUCCESS 0
+%define EXIT_FAILURE 1
+%define STDIN 0
+%define STDOUT 1
+%define STDERR 2
+
+%macro EEXIT 1
+ mov rax, SYS_exit
+ mov rdi, %1
+ syscall
+%endmacro
+
+%macro DIE 2
+ mov rax, SYS_write
+ mov rdi, STDERR
+ mov rsi, %1
+ mov rdx, %2
+ syscall
+ EEXIT EXIT_FAILURE
+%endmacro
+
+%endif ;MACROS_S
diff --git a/macros.s b/macros.s
@@ -1,26 +0,0 @@
-; See LICENSE file for copyright and license details.
-
-%ifndef MACROS_S
-%define MACROS_S
-
-%define EXIT_SUCCESS 0
-%define EXIT_FAILURE 1
-%define STDOUT 1
-%define STDERR 2
-
-%macro EEXIT 1
- mov rax, SYS_exit
- mov rdi, %1
- syscall
-%endmacro
-
-%macro DIE 2
- mov rax, SYS_write
- mov rdi, STDERR
- mov rsi, %1
- mov rdx, %2
- syscall
- EEXIT EXIT_FAILURE
-%endmacro
-
-%endif ;MACROS_S
diff --git a/openbsd_x86_64.csv b/openbsd_x86_64.csv
@@ -1,2 +1,4 @@
%rax,System call,ret,%rdi,%rsi,%rdx,%r10,%r8,%r9
+3,read,ssize_t,int,void *,size_t,,,
+46,sigaction,int,int,const struct sigaction *,struct sigaction *,,,
59,execve,int,const char *,char *const *,char *const *,,,
diff --git a/portable.s b/portable.inc
diff --git a/signals.inc b/signals.inc
@@ -0,0 +1,30 @@
+; See LICENSE file for copyright and license details.
+
+%ifndef SIGNALS_S
+%define SIGNALS_S
+
+%define NSIG 64
+%define NSIG_BPW 8
+%define NSIG_WORDS NSIG / NSIG_BPW
+
+STRUC SIGACTION_STRUC
+ .sa_handler: resq 1
+ .sa_flags: resq 1
+ .sa_restorer: resq 1
+ .sa_mask: resb 128
+ENDSTRUC
+
+%macro SIGACTION 1
+ %1: ISTRUC SIGACTION_STRUC
+ at SIGACTION_STRUC.sa_handler, dq 0
+ at SIGACTION_STRUC.sa_flags, dq 0
+ at SIGACTION_STRUC.sa_restorer, dq 0
+ at SIGACTION_STRUC.sa_mask, times 128 db 0
+ IEND
+ %define %1.sa_handler %1+SIGACTION_STRUC.sa_handler
+ %define %1.sa_flags %1+SIGACTION_STRUC.sa_flags
+ %define %1.sa_restorer %1+SIGACTION_STRUC.sa_restorer
+ %define %1.sa_mask %1+SIGACTION_STRUC.sa_mask
+%endmacro
+
+%endif ;SIGNALS_S
diff --git a/syscalls.inc b/syscalls.inc
@@ -0,0 +1,23 @@
+; See LICENSE file for copyright and license details.
+
+%ifndef SYSCALLS_S
+%define SYSCALLS_S
+
+%ifdef Linux
+ %define SYS_read 0
+ %define SYS_write 1
+ %define SYS_execve 59
+ %define SYS_sigaction 13
+ %define SYS_exit 60
+%elifdef OpenBSD
+ %define SYS_read 3
+ %define SYS_write 4
+ %define SYS_execve 59
+ %define SYS_sigaction 49
+ %define SYS_exit 1
+ %define SYS_pledge 108
+%else
+ %fatal "OS not supported"
+%endif
+
+%endif ;SYSCALLS_S
diff --git a/syscalls.s b/syscalls.s
@@ -1,19 +0,0 @@
-; See LICENSE file for copyright and license details.
-
-%ifndef SYSCALLS_S
-%define SYSCALLS_S
-
-%ifdef Linux
- %define SYS_exit 60
- %define SYS_write 1
- %define SYS_execve 59
-%elifdef OpenBSD
- %define SYS_exit 1
- %define SYS_write 4
- %define SYS_execve 59
- %define SYS_pledge 108
-%else
- %fatal "OS not supported"
-%endif
-
-%endif ;SYSCALLS_S