separate into files

This commit is contained in:
root 2025-05-07 11:38:19 +00:00
parent 0038b22aca
commit 9f3d59cf15
3 changed files with 68 additions and 75 deletions

123
main.s Normal file
View file

@ -0,0 +1,123 @@
global _start
; constants
SYS_READ equ 0
SYS_WRITE equ 1
SYS_POLL equ 7
SYS_SENDFILE equ 40
SYS_SOCKET equ 41
SYS_ACCEPT equ 43
SYS_BIND equ 49
SYS_LISTEN equ 50
SYS_EXIT equ 60
SYS_FCNTL equ 72
SYS_UNLINK equ 87
STDOUT equ 1
AF_UNIX equ 1
SOCK_STREAM equ 1
F_GETFL equ 3
F_SETFL equ 4
O_NONBLOCK equ 2048
POLLIN equ 1
POLLOUT equ 4
pollfd_size equ 4 + 2 + 2 ; $ man 2 poll
pollfds_capacity equ 100
SECTION .data
server_path db "server.sock", 0x00
server_path_len equ $ - server_path
sockaddr_size equ 2 + 108 ; $ man 7 unix
SECTION .bss
sockaddr resb sockaddr_size
pollfds resb pollfd_size * pollfds_capacity
SECTION .text
%include "server.s"
_start:
call make_server
mov rbx, 0 ; pollfd array length
mov ebp, eax ; server fd
mov rax, rbx
mov edi, ebp
mov si, POLLIN
call pollfds__append
mov rbx, rax
poll:
mov rax, SYS_POLL
mov rdi, pollfds
mov rsi, rbx
mov rdx, 5000
syscall
cmp rax, 0
je poll
jl exit
exit:
mov rax, SYS_EXIT
mov rdi, 0
syscall
accept:
mov rdi, rax
mov rax, SYS_ACCEPT
mov rsi, 0
mov rdx, 0
syscall
ret
; rax - array length
; edi - fd
; si - events
; Returns new array length in rax
pollfds__append:
mov [pollfds + rax], edi
mov [pollfds + rax + 4], si
add rax, 1
ret
; Returns new array length in rax
; rax - array length
; edi - fd
pollfds__remove:
mov r10d, edi
mov rdi, 0
; Variables:
; rax - array length
; rdi - array index
; r10d - fd
; r11d - pollfds[rdi].fd
pollfds__remove__loop:
cmp rdi, rax
jge return ; fd was not found
mov r11d, [pollfds + rdi * pollfd_size]
add rdi, 1
cmp r11d, r10d
jne pollfds__remove__loop
sub rdi, 1
; Returns new array length in rax
; rax - array length
; rdi - array index to remove
pollfds__remove_index:
cmp rax, 1
jle pollfds__clear
mov r10d, [pollfds + (rax - 1) * pollfd_size]
mov [pollfds + rdi * pollfd_size], r10d
sub rax, 1
ret
; Returns new array length in rax
; rax - array length
pollfds__clear:
mov rax, 0
ret
return:
ret