-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.asm
133 lines (95 loc) · 2.55 KB
/
main.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
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
#include "syscall.h"
#include "constants.h"
.data
.align 1
goodResponse: .asciz "HTTP/1.1 200 OK\r\n\r\n"
badResponse: .asciz "HTTP/1.1 404 File not found\r\n\r\n"
delimiterString: .asciz "\r\n\r\n"
malformedReq: .asciz "The Request is malformed"
GET: .asciz "GET"
POST: .asciz "POST"
sockfd: .word 0x0
currfd: .word 0x0
addr: .half AF_INET # AF_INET
.half 5000 # sin_port
.byte 127,0,0,1 # sin_addr
.word 0x0 # sin_zero
.word 0x0
goodResponseLen: .word badResponse - goodResponse -1
badResponseLen: .word delimiterString - badResponse -1
malformedReqLen: .word GET - malformedReq -1
addrLen: .word goodResponseLen - addr
.bss
buffer: .space BUFFER_SIZE
currBufferLen: .space 4
reqMethod: .space 4 # 0 means GET,1 means POST
reqURI: .space MAX_FILENAME_LEN+1
reqFD: .space 4
.text
.globl __start
__start:
socket:
li $a0,AF_INET
li $a1,SOCK_STREAM
li $a2,IPPROTO_IP
li $v0,SYS_socket
syscall
la $t5,sockfd
sw $v0,0($t5)
bind:
lw $a0,sockfd
la $a1,addr
lw $a2,addrLen
li $v0, SYS_bind
syscall
listen:
lw $a0,sockfd
li $a1,NULL
li $v0,SYS_listen
syscall
requestLoop:
accept:
lw $a0,sockfd
li $a1,NULL
li $a2,NULL
li $v0,SYS_accept
syscall
la $t5,currfd
sw $v0,0($t5)
move $s0,$v0
la $s1, buffer
li $s4, 0x0 # counter
readLoop:
move $a0,$s0
add $a1,$s1,$s4
li $a2, 1
li $v0, SYS_read
syscall
add $a1,$s1,$s4
lb $t1,0($a1)
li $t3,' '
addiu $s4,$s4,1
bne $t1, $t3, readLoop
slti $t5,$s4,6
beq $t5,$0,methodNotValid
move $a0,$s0
move $a1,$s1
li $a2, 20
li $v0,SYS_write
syscall
close:
lw $a0,currfd
li $v0,SYS_close
syscall
j requestLoop
li $a0,0
li $v0,SYS_exit
syscall
.globl methodNotValid
methodNotValid:
lw $a0,currfd
la $a1,malformedReq
lw $a2,malformedReqLen
li $v0, SYS_write
syscall
jr $ra