Compare commits
No commits in common. "e3705a5d5cf88731371da76194bdf59706eda170" and "b7ac106623ebcfc82aa5b067c0cf7a318803aae3" have entirely different histories.
e3705a5d5c
...
b7ac106623
4
Makefile
4
Makefile
|
|
@ -1,5 +1,5 @@
|
||||||
server: link.ld main.o func.o
|
server: main.o func.o
|
||||||
gcc -T link.ld main.o func.o -o server -nostdlib -static
|
gcc main.o func.o -o server -nostdlib -static
|
||||||
|
|
||||||
main.o: main.S
|
main.o: main.S
|
||||||
gcc -c main.S
|
gcc -c main.S
|
||||||
|
|
|
||||||
2
func.S
2
func.S
|
|
@ -13,7 +13,7 @@
|
||||||
#define AF_INET PF_INET
|
#define AF_INET PF_INET
|
||||||
#define SOCK_STREAM 1
|
#define SOCK_STREAM 1
|
||||||
|
|
||||||
.section .rodata
|
.data
|
||||||
errstr: .ascii "error happened\n"
|
errstr: .ascii "error happened\n"
|
||||||
.set errstr_len, .-errstr
|
.set errstr_len, .-errstr
|
||||||
|
|
||||||
|
|
|
||||||
18
link.ld
18
link.ld
|
|
@ -1,18 +0,0 @@
|
||||||
ENTRY(_start)
|
|
||||||
|
|
||||||
SECTIONS {
|
|
||||||
. = 4M;
|
|
||||||
. = . + SIZEOF_HEADERS;
|
|
||||||
.text : { *(.text) } :text
|
|
||||||
.rodata : { *(.rodata) } :text
|
|
||||||
. += 4K;
|
|
||||||
.data : { *(.data) } :data
|
|
||||||
. = ALIGN(4K);
|
|
||||||
.bss : { *(COMMON) *(.bss) } :bss
|
|
||||||
}
|
|
||||||
|
|
||||||
PHDRS {
|
|
||||||
text PT_LOAD FILEHDR PHDRS FLAGS(5);
|
|
||||||
data PT_LOAD FLAGS(6);
|
|
||||||
bss PT_LOAD;
|
|
||||||
}
|
|
||||||
32
main.S
32
main.S
|
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
#define PORT 20335
|
#define PORT 20335
|
||||||
|
|
||||||
.section .rodata
|
.data
|
||||||
greeting:
|
greeting:
|
||||||
.ascii "Welcome to the minimalistic echo server!\n"
|
.ascii "Welcome to the minimalistic echo server!\n"
|
||||||
.ascii "It is written in pure assembly, and it don't use the standard library, only raw system calls.\n"
|
.ascii "It is written in pure assembly, and it don't use the standard library, only raw system calls.\n"
|
||||||
|
|
@ -29,11 +29,14 @@ message: .ascii "Client connected\n"
|
||||||
errstr: .ascii "Error happened\n"
|
errstr: .ascii "Error happened\n"
|
||||||
.set errstr_len, .-errstr
|
.set errstr_len, .-errstr
|
||||||
|
|
||||||
|
s: .quad 0
|
||||||
|
s2: .quad 0
|
||||||
|
|
||||||
true_val: .quad 1
|
true_val: .quad 1
|
||||||
.set true_val_len, .-true_val
|
.set true_val_len, .-true_val
|
||||||
|
|
||||||
myaddr:
|
myaddr:
|
||||||
.hword AF_INET
|
.hword AF_INET
|
||||||
port: .byte (PORT >> 8) & 0xFF, (PORT & 0xFF)
|
port: .byte (PORT >> 8) & 0xFF, (PORT & 0xFF)
|
||||||
addr: .byte 0, 0, 0, 0
|
addr: .byte 0, 0, 0, 0
|
||||||
.set myaddr_len, 16
|
.set myaddr_len, 16
|
||||||
|
|
@ -41,9 +44,6 @@ myaddr:
|
||||||
.extern passthru
|
.extern passthru
|
||||||
.extern write_buf
|
.extern write_buf
|
||||||
|
|
||||||
#define server_socket %rbx
|
|
||||||
#define connection_socket %r12
|
|
||||||
|
|
||||||
.text
|
.text
|
||||||
.global _start
|
.global _start
|
||||||
_start:
|
_start:
|
||||||
|
|
@ -59,10 +59,10 @@ _start:
|
||||||
syscall
|
syscall
|
||||||
cmp $-1, %rax
|
cmp $-1, %rax
|
||||||
jz error
|
jz error
|
||||||
movq %rax, server_socket
|
movq %rax, s
|
||||||
|
|
||||||
movq $__NR_setsockopt, %rax
|
movq $__NR_setsockopt, %rax
|
||||||
movq server_socket, %rdi
|
movq s, %rdi
|
||||||
movq $SOL_SOCKET, %rsi
|
movq $SOL_SOCKET, %rsi
|
||||||
movq $SO_REUSEADDR, %rdx
|
movq $SO_REUSEADDR, %rdx
|
||||||
movq $true_val, %r10
|
movq $true_val, %r10
|
||||||
|
|
@ -72,7 +72,7 @@ _start:
|
||||||
jnz error
|
jnz error
|
||||||
|
|
||||||
movq $__NR_bind, %rax
|
movq $__NR_bind, %rax
|
||||||
movq server_socket, %rdi
|
movq s, %rdi
|
||||||
movq $myaddr, %rsi
|
movq $myaddr, %rsi
|
||||||
movq $myaddr_len, %rdx
|
movq $myaddr_len, %rdx
|
||||||
syscall
|
syscall
|
||||||
|
|
@ -80,7 +80,7 @@ _start:
|
||||||
jnz error
|
jnz error
|
||||||
|
|
||||||
movq $__NR_listen, %rax
|
movq $__NR_listen, %rax
|
||||||
movq server_socket, %rdi
|
movq s, %rdi
|
||||||
movq $8, %rsi
|
movq $8, %rsi
|
||||||
syscall
|
syscall
|
||||||
test %rax, %rax
|
test %rax, %rax
|
||||||
|
|
@ -88,14 +88,14 @@ _start:
|
||||||
|
|
||||||
loop:
|
loop:
|
||||||
movq $__NR_accept, %rax
|
movq $__NR_accept, %rax
|
||||||
movq server_socket, %rdi
|
movq s, %rdi
|
||||||
movq $0, %rsi
|
movq $0, %rsi
|
||||||
movq $0, %rdx
|
movq $0, %rdx
|
||||||
movq $0, %r10
|
movq $0, %r10
|
||||||
syscall
|
syscall
|
||||||
cmp $-1, %rax
|
cmp $-1, %rax
|
||||||
jz error
|
jz error
|
||||||
movq %rax, connection_socket
|
movq %rax, s2
|
||||||
|
|
||||||
movq $STDOUT, %rdi
|
movq $STDOUT, %rdi
|
||||||
movq $message, %rsi
|
movq $message, %rsi
|
||||||
|
|
@ -116,7 +116,7 @@ loop:
|
||||||
/* we're in the parent if we reach this line */
|
/* we're in the parent if we reach this line */
|
||||||
|
|
||||||
movq $__NR_close, %rax
|
movq $__NR_close, %rax
|
||||||
movq connection_socket, %rdi
|
movq s2, %rdi
|
||||||
syscall
|
syscall
|
||||||
test %rax, %rax
|
test %rax, %rax
|
||||||
jnz error
|
jnz error
|
||||||
|
|
@ -124,7 +124,7 @@ loop:
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
movq $__NR_close, %rax
|
movq $__NR_close, %rax
|
||||||
movq server_socket, %rdi
|
movq s, %rdi
|
||||||
syscall
|
syscall
|
||||||
test %rax, %rax
|
test %rax, %rax
|
||||||
jnz error
|
jnz error
|
||||||
|
|
@ -148,12 +148,12 @@ error:
|
||||||
|
|
||||||
child:
|
child:
|
||||||
/* we're in the child if we reach this line */
|
/* we're in the child if we reach this line */
|
||||||
movq connection_socket, %rdi
|
movq s2, %rdi
|
||||||
movq connection_socket, %rsi
|
movq s2, %rsi
|
||||||
call passthru
|
call passthru
|
||||||
|
|
||||||
movq $__NR_close, %rax
|
movq $__NR_close, %rax
|
||||||
movq connection_socket, %rdi
|
movq s2, %rdi
|
||||||
syscall
|
syscall
|
||||||
|
|
||||||
movq $__NR_exit, %rax
|
movq $__NR_exit, %rax
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user