summaryrefslogtreecommitdiff
path: root/main.asm
diff options
context:
space:
mode:
Diffstat (limited to 'main.asm')
-rw-r--r--main.asm75
1 files changed, 69 insertions, 6 deletions
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