-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.asm
85 lines (70 loc) · 1.22 KB
/
utils.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
macro print_char fd, char
{
push char
write fd, rsp, 1
add rsp, 8
}
;; rdi - buf
;; retval - rax
strlen:
xor rax, rax
.strlenloop:
cmp byte [rdi], 0
je .strlenret
inc rdi
inc rax
jmp .strlenloop
.strlenret:
ret
;; Replace the first carriage return with a terminating zero
;; rdi - buf
trim_first_line:
.loop:
cmp byte [rdi], 13
je .found
inc rdi
jmp .loop
.found:
mov byte [rdi], 0
ret
macro print fd, string
{
mov rdi, string
call strlen
mov rdx, rax
write fd, string, rdx
}
;; Checks if the route string corresponds to the request string
;; rdi - request string
;; rsi - route string
;; retval - rax
check_route:
add rdi, 5
.routecheckloop:
cmp byte [rdi], 32
je .routecheckintermediate
mov al, byte [rdi]
mov bl, byte [rsi]
cmp al, bl
jne .routecheckfailure
inc rdi
inc rsi
jmp .routecheckloop
.routecheckintermediate:
cmp byte [rsi], 0
je .routechecksuccess
jne .routecheckfailure
.routechecksuccess:
mov rax, 1
ret
.routecheckfailure:
mov rax, 0
ret
macro serve_route req, route
{
mov rdi, req
mov rsi, route
call check_route
cmp rax, 0
jne .serve_#route
}