Adding everything
This commit is contained in:
131
helper.s
Normal file
131
helper.s
Normal file
@@ -0,0 +1,131 @@
|
||||
.intel_syntax noprefix
|
||||
.data
|
||||
error_msg_closefd: .ascii "Cannot close STD IO!\n\0"
|
||||
error_msg_openfd_noexist: .ascii "The file does not exist or can't be opened!\n\0"
|
||||
code_max_size: .quad 65536
|
||||
max_size: .quad 16384
|
||||
|
||||
.text
|
||||
|
||||
# ===================
|
||||
# Memory Allocs
|
||||
# ===================
|
||||
# rdi = address
|
||||
# rsi = size
|
||||
clear_heap_block:
|
||||
xor rcx, rcx
|
||||
clear_heap_block_loop:
|
||||
mov byte ptr [rdi+rcx], 0
|
||||
inc rcx
|
||||
cmp rcx, rsi
|
||||
jle clear_heap_block_loop
|
||||
ret
|
||||
|
||||
# rdi = size in octets
|
||||
brk_alloc:
|
||||
push rbx
|
||||
push r12
|
||||
inc rdi # so you get all of the requested space
|
||||
mov rbx, rdi
|
||||
|
||||
mov rax, 12
|
||||
xor rdi, rdi
|
||||
syscall
|
||||
|
||||
mov rdi, rax # To push brk
|
||||
add rdi, rbx
|
||||
mov r12, rax # To return
|
||||
|
||||
# Pushed
|
||||
mov rax, 12
|
||||
syscall
|
||||
|
||||
mov rax, r12
|
||||
|
||||
# Gotta return
|
||||
pop r12
|
||||
pop rbx
|
||||
ret
|
||||
|
||||
|
||||
# ===================
|
||||
# File I/O
|
||||
# ===================
|
||||
# rdi = FD
|
||||
# rsi = char*
|
||||
# rdx = size
|
||||
read:
|
||||
push r11 # read uses r11
|
||||
xor rax, rax
|
||||
syscall
|
||||
pop r11
|
||||
ret
|
||||
|
||||
# rdi = FD
|
||||
# rsi = char*
|
||||
write:
|
||||
push r15
|
||||
mov r15, rdi
|
||||
mov rdi, rsi # no need to touch rsi
|
||||
call count
|
||||
|
||||
mov rdx, rax # Lenght
|
||||
mov rax, 1
|
||||
mov rdi, r15
|
||||
|
||||
syscall
|
||||
pop r15
|
||||
ret
|
||||
|
||||
# rdi = String to count
|
||||
count:
|
||||
mov rax, rdi
|
||||
count_loop:
|
||||
mov r8b, [rax]
|
||||
inc rax
|
||||
cmp r8b, 0
|
||||
jnz count_loop
|
||||
dec rax
|
||||
sub rax, rdi
|
||||
ret
|
||||
|
||||
# ===================
|
||||
# File Handling
|
||||
# ===================
|
||||
# rdi = Path
|
||||
open_fd:
|
||||
mov rax, 2
|
||||
xor rsi, rsi # RO
|
||||
mov rdx, 644
|
||||
syscall
|
||||
cmp rax, 0
|
||||
jl error_out_open
|
||||
ret
|
||||
|
||||
# rdi = FD
|
||||
close_fd:
|
||||
cmp rdi, 0
|
||||
je error_out_close
|
||||
mov rax, 3
|
||||
syscall
|
||||
ret
|
||||
|
||||
# ===================
|
||||
# Exit & Errors
|
||||
# ===================
|
||||
# rdi = Exit code
|
||||
exit:
|
||||
mov rax, 60
|
||||
syscall
|
||||
|
||||
error_out_close:
|
||||
mov rdi, 1
|
||||
mov rsi, offset error_msg_closefd
|
||||
call write
|
||||
call exit
|
||||
|
||||
error_out_open:
|
||||
mov rdi, 1
|
||||
mov rsi, offset error_msg_openfd_noexist
|
||||
call write
|
||||
call exit
|
Reference in New Issue
Block a user