diff --git a/main.s b/main.s index e42255e..e87bcb7 100644 --- a/main.s +++ b/main.s @@ -30,94 +30,85 @@ 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 +sockaddr resb sockaddr_size +pollfds resb pollfd_size * pollfds_capacity +pollfds_len resb 8 SECTION .text %include "server.s" +;%include "pollfds_remove.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 + mov rbx, rax ; server fd + mov rdi, rax + mov rsi, POLLIN call pollfds__append - mov rbx, rax poll: mov rax, SYS_POLL mov rdi, pollfds - mov rsi, rbx + mov rsi, [pollfds_len] mov rdx, 5000 syscall cmp rax, 0 je poll jl exit + mov rdi, rbx + +; rdi - server fd +pollfds__scan: + mov r15, 0 + +pollfds__scan__loop: + cmp r15, [pollfds_len] + jge return + mov r10w, [pollfds + r15 * pollfd_size + 6] + cmp r10w, 0 + jne pollfds__scan__found + add r15, 1 + jmp pollfds__scan__loop + +pollfds__scan__found: + mov r14d, [pollfds + r15 * pollfd_size] + cmp r14d, edi + je pollfds__scan__found__server + +pollfds__scan__found__client: + jmp exit + jmp pollfds__scan__loop + +pollfds__scan__found__server: + mov rax, SYS_ACCEPT + push rdi + mov rsi, 0 + mov rdx, 0 + syscall + cmp rax, 0 + jl exit + + mov rdi, rax + mov si, POLLIN | POLLOUT + call pollfds__append + + pop rdi + jmp pollfds__scan__loop + +; edi - fd +; si - events +pollfds__append: + mov r10, [pollfds_len] + mov [pollfds + r10 * pollfd_size], edi + mov [pollfds + r10 * pollfd_size + 4], si + add r10, 1 + mov [pollfds_len], r10 + ret + +return: + ret 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 diff --git a/poll_remove.s b/poll_remove.s new file mode 100644 index 0000000..477dd81 --- /dev/null +++ b/poll_remove.s @@ -0,0 +1,35 @@ +; Returns new array length in rax +; edi - fd +pollfds__remove: + mov r10d, edi + mov rdi, [pollfds_len] + +; Variables: +; rdi - array length +; rsi - array index +; r10d - fd +; r11d - pollfds[rdi].fd +pollfds__remove__loop: + cmp rsi, rdi + jge return ; fd was not found + mov r11d, [pollfds + rsi * pollfd_size] + add rsi, 1 + cmp r11d, r10d + jne pollfds__remove__loop + sub rsi, 1 + +; Returns new array length in rax +; rdi - array length +; rsi - array index to remove +pollfds__remove_index: + cmp rdi, 1 + jle pollfds__clear + mov r10d, [pollfds + (rdi - 1) * pollfd_size] + mov [pollfds + rsi * pollfd_size], r10d + sub rdi, 1 + ret + +; Returns new array length in rax +pollfds__clear: + mov rax, 0 + ret diff --git a/readme.md b/readme.md index 64a06fc..cdee2fb 100644 --- a/readme.md +++ b/readme.md @@ -1,10 +1,9 @@ # x86_64 assembly -https://userpages.cs.umbc.edu/tsimo1/CMSC313/nasmdoc/html/nasmdoc3.html -https://x64.syscall.sh/ - -https://web.stanford.edu/class/cs107/resources/x86-64-reference.pdf -https://ggbaker.ca/295/x86.html https://josemariasola.github.io/reference/assembler/Stanford%20CS107%20Guide%20to%20x86-64.pdf - https://www.bencode.net/blob/nasmcheatsheet.pdf +https://x64.syscall.sh/ +https://userpages.cs.umbc.edu/tsimo1/CMSC313/nasmdoc/html/nasmdoc3.html +https://web.stanford.edu/class/cs107/resources/x86-64-reference.pdf + +https://engineering.purdue.edu/ece264/22su/hw/ec02/resources/x64-intro.pdf