Compare commits
3 Commits
b7ac106623
...
e3705a5d5c
| Author | SHA1 | Date | |
|---|---|---|---|
| e3705a5d5c | |||
| ae54bb7b92 | |||
| 8be7bb8718 |
4
Makefile
4
Makefile
|
|
@ -1,5 +1,5 @@
|
|||
server: main.o func.o
|
||||
gcc main.o func.o -o server -nostdlib -static
|
||||
server: link.ld main.o func.o
|
||||
gcc -T link.ld main.o func.o -o server -nostdlib -static
|
||||
|
||||
main.o: main.S
|
||||
gcc -c main.S
|
||||
|
|
|
|||
2
func.S
2
func.S
|
|
@ -13,7 +13,7 @@
|
|||
#define AF_INET PF_INET
|
||||
#define SOCK_STREAM 1
|
||||
|
||||
.data
|
||||
.section .rodata
|
||||
errstr: .ascii "error happened\n"
|
||||
.set errstr_len, .-errstr
|
||||
|
||||
|
|
|
|||
18
link.ld
Normal file
18
link.ld
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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;
|
||||
}
|
||||
30
main.S
30
main.S
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
#define PORT 20335
|
||||
|
||||
.data
|
||||
.section .rodata
|
||||
greeting:
|
||||
.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"
|
||||
|
|
@ -29,9 +29,6 @@ message: .ascii "Client connected\n"
|
|||
errstr: .ascii "Error happened\n"
|
||||
.set errstr_len, .-errstr
|
||||
|
||||
s: .quad 0
|
||||
s2: .quad 0
|
||||
|
||||
true_val: .quad 1
|
||||
.set true_val_len, .-true_val
|
||||
|
||||
|
|
@ -44,6 +41,9 @@ myaddr:
|
|||
.extern passthru
|
||||
.extern write_buf
|
||||
|
||||
#define server_socket %rbx
|
||||
#define connection_socket %r12
|
||||
|
||||
.text
|
||||
.global _start
|
||||
_start:
|
||||
|
|
@ -59,10 +59,10 @@ _start:
|
|||
syscall
|
||||
cmp $-1, %rax
|
||||
jz error
|
||||
movq %rax, s
|
||||
movq %rax, server_socket
|
||||
|
||||
movq $__NR_setsockopt, %rax
|
||||
movq s, %rdi
|
||||
movq server_socket, %rdi
|
||||
movq $SOL_SOCKET, %rsi
|
||||
movq $SO_REUSEADDR, %rdx
|
||||
movq $true_val, %r10
|
||||
|
|
@ -72,7 +72,7 @@ _start:
|
|||
jnz error
|
||||
|
||||
movq $__NR_bind, %rax
|
||||
movq s, %rdi
|
||||
movq server_socket, %rdi
|
||||
movq $myaddr, %rsi
|
||||
movq $myaddr_len, %rdx
|
||||
syscall
|
||||
|
|
@ -80,7 +80,7 @@ _start:
|
|||
jnz error
|
||||
|
||||
movq $__NR_listen, %rax
|
||||
movq s, %rdi
|
||||
movq server_socket, %rdi
|
||||
movq $8, %rsi
|
||||
syscall
|
||||
test %rax, %rax
|
||||
|
|
@ -88,14 +88,14 @@ _start:
|
|||
|
||||
loop:
|
||||
movq $__NR_accept, %rax
|
||||
movq s, %rdi
|
||||
movq server_socket, %rdi
|
||||
movq $0, %rsi
|
||||
movq $0, %rdx
|
||||
movq $0, %r10
|
||||
syscall
|
||||
cmp $-1, %rax
|
||||
jz error
|
||||
movq %rax, s2
|
||||
movq %rax, connection_socket
|
||||
|
||||
movq $STDOUT, %rdi
|
||||
movq $message, %rsi
|
||||
|
|
@ -116,7 +116,7 @@ loop:
|
|||
/* we're in the parent if we reach this line */
|
||||
|
||||
movq $__NR_close, %rax
|
||||
movq s2, %rdi
|
||||
movq connection_socket, %rdi
|
||||
syscall
|
||||
test %rax, %rax
|
||||
jnz error
|
||||
|
|
@ -124,7 +124,7 @@ loop:
|
|||
|
||||
exit:
|
||||
movq $__NR_close, %rax
|
||||
movq s, %rdi
|
||||
movq server_socket, %rdi
|
||||
syscall
|
||||
test %rax, %rax
|
||||
jnz error
|
||||
|
|
@ -148,12 +148,12 @@ error:
|
|||
|
||||
child:
|
||||
/* we're in the child if we reach this line */
|
||||
movq s2, %rdi
|
||||
movq s2, %rsi
|
||||
movq connection_socket, %rdi
|
||||
movq connection_socket, %rsi
|
||||
call passthru
|
||||
|
||||
movq $__NR_close, %rax
|
||||
movq s2, %rdi
|
||||
movq connection_socket, %rdi
|
||||
syscall
|
||||
|
||||
movq $__NR_exit, %rax
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user