Skip to content

Commit

Permalink
Split code into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Jul 25, 2024
1 parent e778cc1 commit 112beed
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 231 deletions.
9 changes: 9 additions & 0 deletions src/data/syscalls.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
SYS_READ equ 0
SYS_WRITE equ 1
SYS_OPEN equ 2
SYS_CLOSE equ 3
SYS_SOCKET equ 41
SYS_SENDTO equ 44
SYS_RECVFROM equ 45
SYS_EXIT equ 60

5 changes: 5 additions & 0 deletions src/data/teeworlds.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
; tw protocol
MSG_CTRL_TOKEN db 0x04, 0x00, 0x00, 0x0FF, 0xFF, 0xFF, 0xFF, 0x05, 0x51, 0x3B, 0x59, 0x46, 512 dup (0x00)
MSG_CTRL_TOKEN_LEN equ $ - MSG_CTRL_TOKEN
NET_MAX_PACKETSIZE equ 1400

50 changes: 50 additions & 0 deletions src/hex.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
hex_to_char:
; hex_to_char [rax]
;
; moves byte in `rax` as hex string into
; the `hex_str` variable
; https://stackoverflow.com/a/18879886/6287070
push rbx
push rax
mov rbx, HEX_TABLE

mov ah, al
shr al, 4
and ah, 0x0f
xlat
xchg ah, al
xlat

mov rbx, hex_str
xchg ah, al
mov [rbx], rax

pop rax
pop rbx
ret

print_hex_byte:
; print_hex [rax]
;
; prints given arg as hex string
; to stdout
push rax
push rdi
push rsi
push rdx
push rcx

call hex_to_char

mov eax, SYS_WRITE
mov edi, STDOUT
mov rsi, hex_str ; movabs
mov edx, 0x2
syscall

pop rcx
pop rdx
pop rsi
pop rdi
pop rax
ret
74 changes: 74 additions & 0 deletions src/logger.asm
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,77 @@ print_newline:
pop rax
ret

dbg_print_uint32:
; dbg_print_num [rax]
;
; prints given arg as uint32 turned into a string
; to stdout
; prefixed with a debug string message

push rax
push rdi
push rsi
push rdx

mov rsi, s_dbg_digit
mov eax, SYS_WRITE
mov edi, STDOUT
mov edx, l_dbg_digit
syscall

pop rdx
pop rsi
pop rdi
pop rax

call print_uint32
call print_newline

ret

print_uint32:
; print_uint32 [rax]
;
; has a sub label toascii_digit
; and prints the given value in rax
; as a digit to stdout
; https://stackoverflow.com/a/46301894/6287070
push rax
push rsi
push rcx
push rdx

mov ecx, 0xa ; base 10
push rcx ; ASCII newline '\n' = 0xa = base
mov rsi, rsp
sub rsp, 16 ; not needed on 64-bit Linux, the red-zone is big enough. Change the LEA below if you remove this.

;;; rsi is pointing at '\n' on the stack, with 16B of "allocated" space below that.
.print_uint32_toascii_digit: ; do {
xor edx, edx
div ecx ; edx=remainder = low digit = 0..9. eax/=10
;; DIV IS SLOW. use a multiplicative inverse if performance is relevant.
add edx, '0'
dec rsi ; store digits in MSD-first printing order, working backwards from the end of the string
mov [rsi], dl

test eax,eax ; } while(x);
jnz .print_uint32_toascii_digit
;;; rsi points to the first digit


mov eax, SYS_WRITE
mov edi, STDOUT
; pointer already in RSI ; buf = last digit stored = most significant
lea edx, [rsp+16 + 1] ; yes, it's safe to truncate pointers before subtracting to find length.
sub edx, esi ; RDX = length = end-start, including the \n
syscall ; write(1, string /*RSI*/, digits + 1)

add rsp, 24 ; (in 32-bit: add esp,20) undo the push and the buffer reservation

pop rdx
pop rcx
pop rsi
pop rax
ret

Loading

0 comments on commit 112beed

Please sign in to comment.