summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2024-09-20 00:22:43 +0200
committerfxqnlr <[email protected]>2024-09-20 00:22:43 +0200
commit9c5783f030d7c876ccf6e05e8182d88f84a97fc3 (patch)
tree150f2cccd904d1ed787cc6f413a12960ef26e5d2
parentf0b8e96b51b1a8b19365c9d010639c8d67a42506 (diff)
downloadasmsite-9c5783f030d7c876ccf6e05e8182d88f84a97fc3.tar
asmsite-9c5783f030d7c876ccf6e05e8182d88f84a97fc3.tar.gz
asmsite-9c5783f030d7c876ccf6e05e8182d88f84a97fc3.zip
it works, sometimes
-rwxr-xr-xdebugbin0 -> 15552 bytes
-rw-r--r--main.asm75
-rw-r--r--sys.asm38
3 files changed, 82 insertions, 31 deletions
diff --git a/debug b/debug
new file mode 100755
index 0000000..a0634a1
--- /dev/null
+++ b/debug
Binary files differ
diff --git a/main.asm b/main.asm
index 7644482..8c19651 100644
--- a/main.asm
+++ b/main.asm
@@ -2,26 +2,53 @@ include 'sys.asm'
2 2
3format ELF64 executable 3format ELF64 executable
4 4
5segment writable readable
6struc db [data] 5struc db [data]
7{ 6{
8 common 7 common
9 . db data 8 . db data
10 .size = $ - . 9 .size = $ - .
11} 10}
11
12segment writable readable
13connect_header db 'HTTP/1.1 200 OK', 13, 10
14 db 'Content-Type: text/html', 13, 10
15 db 'Connection: close', 13, 10
16 db 13, 10
17 db '<!DOCTYPE html><html><head><title>asmsite</title></head><body>Hello, World!</body></html>', 10
18connect_header_len = $ - connect_header
19
12stat_msg db 'Get file stats', 10 20stat_msg db 'Get file stats', 10
13open_msg db 'Open file', 10 21open_msg db 'Open file', 10
14read_msg db 'Read file', 10 22read_msg db 'Read file', 10
15close_msg db 'Close file', 10 23close_msg db 'Close file', 10
24socket_msg db 'Open socket', 10
25bind_msg db 'Bind socket', 10
26listen_msg db 'Listen', 10
27accept_msg db 'Wait for connection', 10
16msg_dn db ':', 10 28msg_dn db ':', 10
17 29
18indexname db 'index.html' 30indexname db 'index.html'
19;; indexstat stat 31indexlen dq 0
20indexlen rq 1 32
33indexfd dd -1
34socketfd dd -1
35connfd dd -1
21 36
22indexfd dd 0 37indexbuf dd 0
23indexbuf dd ? 38indexbuf_len dq 0
24indexbuf_len rq 1 39
40struc servaddr_in
41{
42 .sin_family dw 0
43 .sin_port dw 0
44 .sin_addr dd 0
45 .sin_zero dq 0
46 ;; .size = $ - .sin_family
47}
48serveraddr servaddr_in
49serveraddr_len = $ - serveraddr.sin_family
50clientaddr servaddr_in
51clientaddr_len dd serveraddr_len
25 52
26segment readable executable 53segment readable executable
27 54
@@ -50,10 +77,46 @@ _main:
50 cmp qword rax, 0 77 cmp qword rax, 0
51 jl error 78 jl error
52 79
80 ls_write STDOUT, socket_msg, socket_msg.size
81 ls_socket AF_INET, SOCK_STREAM, 0
82 mov qword [socketfd], rax
83 cmp [socketfd], 0
84 jl error
85
86 ls_write STDOUT, bind_msg, bind_msg.size
87 mov [serveraddr.sin_family], AF_INET
88 mov [serveraddr.sin_port], 0xb80b
89 mov [serveraddr.sin_addr], INADDR_ANY
90 ls_bind qword [socketfd], serveraddr, serveraddr_len
91 cmp rax, 0
92 jl error
53 93
94 ls_write STDOUT, listen_msg, listen_msg.size
95 ls_listen qword [socketfd], 5
96 cmp rax, 0
97 jl error
98
99listen:
100 ls_write STDOUT, accept_msg, accept_msg.size
101 ls_accept qword [socketfd], clientaddr, clientaddr_len
102 cmp rax, 0
103 jl error
54 104
105 mov qword [connfd], rax
106
107 ls_write qword [connfd], connect_header, connect_header_len
108
109 ls_close qword [connfd]
110
111 jmp listen
112
113 ls_close qword [indexfd]
114 ls_close qword [socketfd]
115 ls_close qword [connfd]
55 ls_exit 0 116 ls_exit 0
56 117
57error: 118error:
58 ls_close qword [indexfd] 119 ls_close qword [indexfd]
120 ls_close qword [socketfd]
121 ls_close qword [connfd]
59 ls_exit 1 122 ls_exit 1
diff --git a/sys.asm b/sys.asm
index b9ab190..a237890 100644
--- a/sys.asm
+++ b/sys.asm
@@ -6,6 +6,10 @@ O_RDONLY equ 0
6O_WRONLY equ 1 6O_WRONLY equ 1
7O_RDWR equ 2 7O_RDWR equ 2
8 8
9AF_INET equ 2
10SOCK_STREAM equ 1
11INADDR_ANY equ 0
12
9macro scall1 v_rax, v_rdi 13macro scall1 v_rax, v_rdi
10{ 14{
11 mov rax, v_rax 15 mov rax, v_rax
@@ -38,31 +42,15 @@ macro ls_open filename, flags, mode { scall3 2, filename, flags, mode }
38 42
39macro ls_close fd { scall1 3, fd } 43macro ls_close fd { scall1 3, fd }
40 44
41;; kernel: arch/x86/include/uapi/asm/stat.h 45;; macro ls_stat filename, statbuf { scall2 4, filename, statbuf }
42;; struc stat 46
43;; { 47macro ls_socket family, type, protocol { scall3 41, family, type, protocol }
44;; .st_dev rd 1 48
45;; .st_ino rd 1 49macro ls_accept fd, upeer_sockaddr, upeer_addrlen { scall3 43, fd, upeer_sockaddr, upeer_addrlen }
46;; .st_mode rw 1 50
47;; .st_nlink rw 1 51macro ls_bind fd, umyaddr, addrlen { scall3 49, fd, umyaddr, addrlen }
48;; .st_uid rw 1 52
49;; .st_gid rw 1 53macro ls_listen fd, backlog { scall2 50, fd, backlog }
50;; .st_rdev rd 1
51;; .st_size rd 1
52;; .st_blksize rd 1
53;; .st_blocks rd 1
54;;
55;; .st_atime rd 1
56;; .st_atime_nsec rd 1
57;; .st_mtime rd 1
58;; .st_mtime_nsec rd 1
59;; .st_ctime rd 1
60;; .st_ctime_nsec rd 1
61;;
62;; .__unused4 rd 1
63;; .__unused5 rd 1
64;; }
65macro ls_stat filename, statbuf { scall2 4, filename, statbuf }
66 54
67macro ls_exit status { scall1 60, status } 55macro ls_exit status { scall1 60, status }
68 56