include 'sys.asm'
format ELF64 executable
struc db [data]
{
common
. db data
.size = $ - .
}
segment writable readable
connect_header db 'HTTP/1.1 200 OK', 13, 10
db 'Content-Type: text/html', 13, 10
db 'Connection: close', 13, 10
db 13, 10
db '<!DOCTYPE html><html><head><title>asmsite</title></head><body>Hello, World!</body></html>', 10
connect_header_len = $ - connect_header
stat_msg db 'Get file stats', 10
open_msg db 'Open file', 10
read_msg db 'Read file', 10
close_msg db 'Close file', 10
socket_msg db 'Open socket', 10
bind_msg db 'Bind socket', 10
listen_msg db 'Listen', 10
accept_msg db 'Wait for connection', 10
msg_dn db ':', 10
indexname db 'index.html'
indexlen dq 0
indexfd dd -1
socketfd dd -1
connfd dd -1
indexbuf dd 0
indexbuf_len dq 0
struc servaddr_in
{
.sin_family dw 0
.sin_port dw 0
.sin_addr dd 0
.sin_zero dq 0
;; .size = $ - .sin_family
}
serveraddr servaddr_in
serveraddr_len = $ - serveraddr.sin_family
clientaddr servaddr_in
clientaddr_len dd serveraddr_len
segment readable executable
entry _main
_main:
ls_write STDOUT, open_msg, open_msg.size
ls_open indexname, O_RDONLY, 0
mov qword [indexfd], rax
cmp [indexfd], 0
jl error
ls_write STDOUT, read_msg, read_msg.size
ls_read qword [indexfd], indexbuf, indexbuf_len
cmp qword rax, 0
jl error
mov [indexlen], rax
ls_write STDOUT, close_msg, close_msg.size
ls_close qword [indexfd]
cmp qword rax, 0
jl error
ls_write STDOUT, indexname, indexname.size
ls_write STDOUT, msg_dn, msg_dn.size
ls_write STDOUT, indexbuf, [indexlen]
cmp qword rax, 0
jl error
ls_write STDOUT, socket_msg, socket_msg.size
ls_socket AF_INET, SOCK_STREAM, 0
mov qword [socketfd], rax
cmp [socketfd], 0
jl error
ls_write STDOUT, bind_msg, bind_msg.size
mov [serveraddr.sin_family], AF_INET
mov [serveraddr.sin_port], 0xb80b
mov [serveraddr.sin_addr], INADDR_ANY
ls_bind qword [socketfd], serveraddr, serveraddr_len
cmp rax, 0
jl error
ls_write STDOUT, listen_msg, listen_msg.size
ls_listen qword [socketfd], 5
cmp rax, 0
jl error
listen:
ls_write STDOUT, accept_msg, accept_msg.size
ls_accept qword [socketfd], clientaddr, clientaddr_len
cmp rax, 0
jl error
mov qword [connfd], rax
ls_write qword [connfd], connect_header, connect_header_len
ls_close qword [connfd]
jmp listen
ls_close qword [indexfd]
ls_close qword [socketfd]
ls_close qword [connfd]
ls_exit 0
error:
ls_close qword [indexfd]
ls_close qword [socketfd]
ls_close qword [connfd]
ls_exit 1