This repository has been archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.s
234 lines (163 loc) · 4.04 KB
/
server.s
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
.section .rodata
port:
.int 1234
startMsg:
.asciz "Starting Local Server on localhost:%u...\n"
httpBase:
.asciz "HTTP/1.0 200\r\nContent-type:text/html\r\n\r\n%s"
htmlBase:
.asciz "<html><head><title>%s</title></head><body>%s</body></html>"
htmlTitle:
.asciz "Assembly Rules."
htmlBody:
.asciz "<h1>Hello, world!</h1><h2>You are visitor #%u</h2><p>%s</p>"
paragraph:
.asciz "Web rendered dynamically by x86 assembly."
httpResponse404:
.asciz "HTTP/1.0 404 Not Found\r\n\r\n"
httpRequestHeader:
.asciz "GET / HTTP/"
.section .data
count:
.int 0
buf:
.asciz "GET / HTTP/"
.text
.global main
main:
push %ebp
movl %esp, %ebp
sub $28, %esp
movl port, %eax
movl %eax, 4(%esp)
movl $startMsg, (%esp)
call printf
call m_listen
movl %eax, -4(%ebp)
repeat:
movl -4(%ebp), %eax
movl $0, 8(%esp)
movl $0, 4(%esp)
movl %eax, (%esp)
call accept
movl %eax, -8(%ebp)
movl $11, 8(%esp)
movl $buf, 4(%esp)
movl %eax, (%esp)
call read
movl $11, 8(%esp)
movl $httpRequestHeader, 4(%esp)
movl $buf, (%esp)
call strncmp
cmp $0, %eax
jne http404
call construct_response
movl %eax, -12(%ebp)
movl %eax, (%esp)
call strlen
movl %eax, -16(%ebp)
movl -8(%ebp), %eax
movl -12(%ebp), %ebx
movl -16(%ebp), %ecx
movl %ecx, 8(%esp)
movl %ebx, 4(%esp)
movl %eax, (%esp)
call write
movl -12(%ebp), %eax
movl %eax, (%esp)
call destroy_response
jmp cleanup
http404:
movl -8(%ebp), %eax
movl $26, 8(%esp)
movl $httpResponse404, 4(%esp)
movl %eax, (%esp)
call write
cleanup:
movl -8(%ebp), %eax
movl $2, 4(%esp)
movl %eax, (%esp)
call shutdown
movl -8(%ebp), %eax
movl %eax, (%esp)
call close
jmp repeat
movl $1, %eax
leave
ret
m_listen:
push %ebp
movl %esp, %ebp
subl $56, %esp
movl $0, 8(%esp)
movl $1, 4(%esp)
movl $2, (%esp)
call socket
movl %eax, -12(%ebp)
movl $16, 8(%esp)
movl $0, 4(%esp)
leal -28(%ebp), %eax
movl %eax, (%esp)
call memset
movw $2, -28(%ebp)
movl $0, -24(%ebp)
movl port, %eax
movzwl %ax, %eax
movl %eax, (%esp)
call htons
movw %ax, -26(%ebp)
movl $16, 8(%esp)
leal -28(%ebp), %eax
movl %eax, 4(%esp)
movl -12(%ebp), %eax
movl %eax, (%esp)
call bind
movl $16, 4(%esp)
movl -12(%ebp), %eax
movl %eax, (%esp)
call listen
movl -12(%ebp), %eax
leave
ret
construct_response:
push %ebp
movl %esp, %ebp
sub $28, %esp
add $1, count
leal -4(%ebp), %eax
movl count, %ebx
movl $paragraph, 12(%esp)
movl %ebx, 8(%esp)
movl $htmlBody, 4(%esp)
movl %eax, 0(%esp)
call asprintf
leal -8(%ebp), %eax
movl -4(%ebp), %ebx
movl %ebx, 12(%esp)
movl $htmlTitle, 8(%esp)
movl $htmlBase, 4(%esp)
movl %eax, (%esp)
call asprintf
leal -12(%ebp), %eax
movl -8(%ebp), %ebx
movl %ebx, 8(%esp)
movl $httpBase, 4(%esp)
movl %eax, (%esp)
call asprintf
movl -4(%ebp), %eax
movl %eax, (%esp)
call free
movl -8(%ebp), %eax
movl %eax, (%esp)
call free
movl -12(%ebp), %eax
leave
ret
destroy_response:
push %ebp
movl %esp, %ebp
movl 8(%ebp), %eax
push %eax
call free
leave
ret