...
This commit is contained in:
9
src/runtime/core/syscall.asm
Normal file
9
src/runtime/core/syscall.asm
Normal file
@@ -0,0 +1,9 @@
|
||||
section .text
|
||||
global core_syscall
|
||||
core_syscall:
|
||||
mov rax, rdi
|
||||
mov rdi, rsi
|
||||
mov rsi, rdx
|
||||
mov r10, rcx
|
||||
syscall
|
||||
ret
|
||||
@@ -9,6 +9,9 @@ _start:
|
||||
mov rax, 60
|
||||
syscall
|
||||
|
||||
; String comparison function null-terminated strings
|
||||
; Arguments: rdi = lhs (*char), rsi = rhs (*char)
|
||||
; Returns: 1 if equal, else 0
|
||||
global nub_strcmp
|
||||
nub_strcmp:
|
||||
xor rdx, rdx
|
||||
@@ -28,6 +31,9 @@ nub_strcmp:
|
||||
mov rax, 1
|
||||
ret
|
||||
|
||||
; Panic function with message
|
||||
; Arguments: rdi = message (*char), rsi = message length (long)
|
||||
; Remarks: exits the program
|
||||
global nub_panic
|
||||
nub_panic:
|
||||
mov rdx, rsi
|
||||
@@ -39,73 +45,50 @@ nub_panic:
|
||||
mov rdi, 101
|
||||
syscall
|
||||
|
||||
; TODO: This is ai-generated. Should be re-implemented in the future
|
||||
; Memory set function
|
||||
; Arguments: rdi = destination pointer, rsi = value (byte), rdx = count
|
||||
; Returns: rdi (original destination pointer)
|
||||
global nub_memset
|
||||
nub_memset:
|
||||
; Save original destination for return value
|
||||
mov rax, rdi
|
||||
push rdi ; Save original destination for return value
|
||||
mov rcx, rdx ; Load count into counter register
|
||||
mov al, sil ; Move byte value to al (lower 8 bits of rsi)
|
||||
|
||||
; Handle zero length
|
||||
test rdx, rdx
|
||||
; Handle zero count case
|
||||
test rcx, rcx
|
||||
jz .done
|
||||
|
||||
; For small sizes, use simple byte-by-byte loop
|
||||
cmp rdx, 16
|
||||
jb .byte_loop
|
||||
|
||||
; Prepare value for bulk setting
|
||||
; Replicate the byte across all 8 bytes of rsi
|
||||
and rsi, 0xFF ; Ensure only low byte is used
|
||||
mov rcx, rsi ; rcx = byte value
|
||||
shl rsi, 8
|
||||
or rsi, rcx ; rsi = byte | (byte << 8)
|
||||
mov rcx, rsi
|
||||
shl rsi, 16
|
||||
or rsi, rcx ; rsi = 4 copies of byte
|
||||
mov rcx, rsi
|
||||
shl rsi, 32
|
||||
or rsi, rcx ; rsi = 8 copies of byte
|
||||
|
||||
; Align to 8-byte boundary if needed
|
||||
mov rcx, rdi
|
||||
and rcx, 7 ; rcx = bytes until 8-byte aligned
|
||||
jz .aligned
|
||||
|
||||
; Fill bytes until aligned
|
||||
neg rcx
|
||||
add rcx, 8 ; rcx = bytes to fill for alignment
|
||||
cmp rcx, rdx
|
||||
jbe .align_loop
|
||||
mov rcx, rdx ; Don't go past end
|
||||
.align_loop:
|
||||
mov [rdi], sil
|
||||
inc rdi
|
||||
dec rdx
|
||||
dec rcx
|
||||
jnz .align_loop
|
||||
|
||||
.aligned:
|
||||
; Fill 8 bytes at a time
|
||||
mov rcx, rdx
|
||||
shr rcx, 3 ; rcx = number of 8-byte chunks
|
||||
jz .remainder
|
||||
.quad_loop:
|
||||
mov [rdi], rsi
|
||||
add rdi, 8
|
||||
dec rcx
|
||||
jnz .quad_loop
|
||||
|
||||
; Handle remainder bytes
|
||||
and rdx, 7 ; rdx = remaining bytes
|
||||
.remainder:
|
||||
test rdx, rdx
|
||||
jz .done
|
||||
|
||||
.byte_loop:
|
||||
mov [rdi], sil
|
||||
inc rdi
|
||||
dec rdx
|
||||
jnz .byte_loop
|
||||
.loop:
|
||||
mov [rdi], al ; Store byte at current position
|
||||
inc rdi ; Move to next byte
|
||||
dec rcx ; Decrement counter
|
||||
jnz .loop ; Continue if counter not zero
|
||||
|
||||
.done:
|
||||
ret
|
||||
pop rax ; Return original destination pointer
|
||||
ret
|
||||
|
||||
; Memory copy function
|
||||
; Arguments: rdi = destination, rsi = source, rdx = count
|
||||
; Returns: rdi (original destination pointer)
|
||||
global nub_memcpy
|
||||
nub_memcpy:
|
||||
push rdi ; Save original destination for return value
|
||||
mov rcx, rdx ; Load count into counter register
|
||||
|
||||
; Handle zero count case
|
||||
test rcx, rcx
|
||||
jz .done
|
||||
|
||||
; Simple byte-by-byte copy (no overlap handling)
|
||||
.loop:
|
||||
mov al, [rsi] ; Load byte from source
|
||||
mov [rdi], al ; Store byte to destination
|
||||
inc rsi ; Move to next source byte
|
||||
inc rdi ; Move to next destination byte
|
||||
dec rcx ; Decrement counter
|
||||
jnz .loop ; Continue if counter not zero
|
||||
|
||||
.done:
|
||||
pop rax ; Return original destination pointer
|
||||
ret
|
||||
|
||||
Reference in New Issue
Block a user