Working version
This commit is contained in:
commit
a7fd2b390c
13
Makefile
Normal file
13
Makefile
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
server: main.o func.o
|
||||
gcc main.o func.o -o server -nostdlib
|
||||
|
||||
main.o: main.S
|
||||
gcc -c main.S
|
||||
|
||||
func.o: func.S
|
||||
gcc -c func.S
|
||||
|
||||
clean:
|
||||
rm *.o server
|
||||
|
||||
all: server
|
||||
113
func.S
Normal file
113
func.S
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
.code64
|
||||
#include <asm/unistd_64.h>
|
||||
|
||||
.global read_buf
|
||||
.global write_buf
|
||||
.global passthru
|
||||
|
||||
#define STDIN 0
|
||||
#define STDOUT 1
|
||||
#define STDERR 2
|
||||
|
||||
#define PF_INET 2
|
||||
#define AF_INET PF_INET
|
||||
#define SOCK_STREAM 1
|
||||
|
||||
.data
|
||||
errstr: .ascii "error happened\n"
|
||||
.set errstr_len, .-errstr
|
||||
|
||||
.set buffer_size, 1024
|
||||
buffer: .skip buffer_size, 0
|
||||
|
||||
.text
|
||||
|
||||
.func read_buf
|
||||
read_buf:
|
||||
test %rdx, %rdx
|
||||
jz read_exit
|
||||
mov %rdi, %r13 /* file */
|
||||
mov %rsi, %r14 /* buffer */
|
||||
mov %rdx, %r15 /* length */
|
||||
read_seq:
|
||||
movq $__NR_read, %rax
|
||||
movq %r13, %rdi
|
||||
movq %r14, %rsi
|
||||
movq %r15, %rdx
|
||||
syscall
|
||||
cmp $-1, %rax
|
||||
jz error /* Yes we JUMP, not CALL */
|
||||
test %rax, %rax
|
||||
jz read_empty
|
||||
add %rax, %r14
|
||||
sub %rax, %r15
|
||||
jnz read_seq /* we have more bytes! */
|
||||
read_exit:
|
||||
ret
|
||||
read_empty:
|
||||
jmp error
|
||||
.endfunc
|
||||
|
||||
.func write_buf
|
||||
write_buf:
|
||||
test %rdx, %rdx
|
||||
jz write_exit
|
||||
mov %rdi, %r13 /* file */
|
||||
mov %rsi, %r14 /* buffer */
|
||||
mov %rdx, %r15 /* length */
|
||||
write_seq:
|
||||
movq $__NR_write, %rax
|
||||
movq %r13, %rdi
|
||||
movq %r14, %rsi
|
||||
movq %r15, %rdx
|
||||
syscall
|
||||
cmp $-1, %rax
|
||||
jz error /* Yes we JUMP, not CALL */
|
||||
test %rax, %rax
|
||||
jz write_empty
|
||||
add %rax, %r14
|
||||
sub %rax, %r15
|
||||
jnz write_seq /* we have more bytes! */
|
||||
write_exit:
|
||||
ret
|
||||
write_empty:
|
||||
jmp error
|
||||
.endfunc
|
||||
|
||||
.func passthru
|
||||
passthru:
|
||||
push %rdi
|
||||
push %rsi
|
||||
passthru_step:
|
||||
movq $__NR_read, %rax
|
||||
movq 8(%rsp), %rdi
|
||||
movq $buffer, %rsi
|
||||
movq $buffer_size, %rdx
|
||||
syscall
|
||||
cmp $-1, %rax
|
||||
jz error /* Yes we JUMP, not CALL */
|
||||
test %rax, %rax
|
||||
jz passthru_empty
|
||||
movq 0(%rsp), %rdi
|
||||
movq $buffer, %rsi
|
||||
movq %rax, %rdx
|
||||
call write_buf
|
||||
jmp passthru_step
|
||||
passthru_empty:
|
||||
passthru_exit:
|
||||
addq $16, %rsp
|
||||
ret
|
||||
.endfunc
|
||||
|
||||
error:
|
||||
movq $__NR_write, %rax
|
||||
movq $STDERR, %rdi
|
||||
movq $errstr, %rsi
|
||||
movq $errstr_len, %rdx
|
||||
syscall
|
||||
exit:
|
||||
movq $__NR_exit, %rax
|
||||
movq $0, %rdi
|
||||
movq $0, %rsi
|
||||
syscall
|
||||
movq $0, 0
|
||||
119
main.S
Normal file
119
main.S
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
.code64
|
||||
#include <asm/unistd.h>
|
||||
#include <asm/socket.h>
|
||||
|
||||
#define STDIN 0
|
||||
#define STDOUT 1
|
||||
#define STDERR 2
|
||||
|
||||
#define PF_INET 2
|
||||
#define AF_INET PF_INET
|
||||
#define SOCK_STREAM 1
|
||||
|
||||
#define PORT 20335
|
||||
|
||||
.data
|
||||
greeting: .ascii "Socket test!\n"
|
||||
.set greeting_len, .-greeting
|
||||
|
||||
bye: .ascii "Sockets tested!\n"
|
||||
.set bye_len, .-bye
|
||||
|
||||
errstr: .ascii "Error happened\n"
|
||||
.set errstr_len, .-errstr
|
||||
|
||||
s: .quad 0
|
||||
|
||||
true_val: .quad 1
|
||||
.set true_val_len, .-true_val
|
||||
|
||||
myaddr:
|
||||
.hword AF_INET
|
||||
port: .byte (PORT >> 8) & 0xFF, (PORT & 0xFF)
|
||||
addr: .byte 0, 0, 0, 0
|
||||
.set myaddr_len, 16
|
||||
|
||||
.extern passthru
|
||||
.extern write_buf
|
||||
|
||||
.text
|
||||
.global _start
|
||||
_start:
|
||||
movq $STDERR, %rdi
|
||||
movq $greeting, %rsi
|
||||
movq $greeting_len, %rdx
|
||||
call write_buf
|
||||
|
||||
movq $__NR_socket, %rax
|
||||
movq $PF_INET, %rdi
|
||||
movq $SOCK_STREAM, %rsi
|
||||
movq $0, %rdx
|
||||
syscall
|
||||
cmp $-1, %rax
|
||||
jz error
|
||||
movq %rax, s
|
||||
|
||||
movq $__NR_setsockopt, %rax
|
||||
movq s, %rdi
|
||||
movq $SOL_SOCKET, %rsi
|
||||
movq $SO_REUSEADDR, %rdx
|
||||
movq $true_val, %r10
|
||||
movq $true_val_len, %r8
|
||||
syscall
|
||||
test %rax, %rax
|
||||
jnz error
|
||||
|
||||
movq $__NR_bind, %rax
|
||||
movq s, %rdi
|
||||
movq $myaddr, %rsi
|
||||
movq $myaddr_len, %rdx
|
||||
syscall
|
||||
test %rax, %rax
|
||||
jnz error
|
||||
|
||||
movq $__NR_listen, %rax
|
||||
movq s, %rdi
|
||||
movq $8, %rsi
|
||||
syscall
|
||||
test %rax, %rax
|
||||
jnz error
|
||||
|
||||
movq $__NR_accept, %rax
|
||||
movq s, %rdi
|
||||
movq $0, %rsi
|
||||
movq $0, %rdx
|
||||
movq $0, %r10
|
||||
syscall
|
||||
cmp $-1, %rax
|
||||
jz error
|
||||
|
||||
test %rax, %rax
|
||||
jz error
|
||||
|
||||
movq %rax, %rdi
|
||||
movq $STDOUT, %rsi
|
||||
call passthru
|
||||
|
||||
movq $STDERR, %rdi
|
||||
movq $bye, %rsi
|
||||
movq $bye_len, %rdx
|
||||
call write_buf
|
||||
|
||||
exit:
|
||||
movq $__NR_close, %rax
|
||||
movq s, %rdi
|
||||
syscall
|
||||
|
||||
_exit:
|
||||
movq $__NR_exit, %rax
|
||||
movq $0, %rdi
|
||||
syscall
|
||||
movq $0, 0
|
||||
|
||||
error:
|
||||
movq $__NR_write, %rax
|
||||
movq $STDERR, %rdi
|
||||
movq $errstr, %rsi
|
||||
movq $errstr_len, %rdx
|
||||
syscall
|
||||
jmp _exit
|
||||
Loading…
Reference in New Issue
Block a user