This commit is contained in:
nub31
2025-06-10 18:40:35 +02:00
parent 7f34719205
commit fb391c9fb2
12 changed files with 115 additions and 674 deletions

View File

@@ -0,0 +1,11 @@
.intel_syntax noprefix
.extern main
.section .text
.globl _start
_start:
mov rdi, rsp
call main
mov rdi, rax
mov rax, 60
syscall

View File

@@ -0,0 +1,24 @@
.intel_syntax noprefix
.section .text
# Memory copy function
# Arguments: rdi = destination, rsi = source, rdx = count
# Returns: rdi (original destination pointer)
.globl 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, BYTE PTR [rsi] # Load byte from source
mov BYTE PTR [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

View File

@@ -0,0 +1,22 @@
.intel_syntax noprefix
.section .text
# Memory set function
# Arguments: rdi = destination pointer, rsi = value (byte), rdx = count
# Returns: rdi (original destination pointer)
.globl nub_memset
nub_memset:
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 count case
test rcx, rcx
jz .done
.loop:
mov BYTE PTR [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:
pop rax # Return original destination pointer
ret

View File

@@ -0,0 +1,16 @@
.intel_syntax noprefix
.section .text
# Panic function with message
# Arguments: rdi = message (*char), rsi = message length (long)
# Remarks: exits the program
.globl nub_panic
nub_panic:
mov rdx, rsi
mov rsi, rdi
mov rax, 1
mov rdi, 2
syscall
mov rax, 60
mov rdi, 101
syscall

View File

@@ -0,0 +1,24 @@
.intel_syntax noprefix
.section .text
# String comparison function null-terminated strings
# Arguments: rdi = lhs (*char), rsi = rhs (*char)
# Returns: 1 if equal, else 0
.globl nub_strcmp
nub_strcmp:
xor rdx, rdx
.loop:
mov al, BYTE PTR [rsi + rdx]
mov bl, BYTE PTR [rdi + rdx]
inc rdx
cmp al, bl
jne .not_equal
cmp al, 0
je .equal
jmp .loop
.not_equal:
mov rax, 0
ret
.equal:
mov rax, 1
ret

View File

@@ -1,9 +1,11 @@
section .text
global core_syscall
.intel_syntax noprefix
.globl core_syscall
.section .text
core_syscall:
mov rax, rdi
mov rdi, rsi
mov rsi, rdx
mov r10, rcx
movq rax, rdi
movq rdi, rsi
movq rsi, rdx
movq r10, rcx
syscall
ret
ret

View File

@@ -1,94 +0,0 @@
global _start
extern main
section .text
_start:
mov rdi, rsp
call main
mov rdi, rax
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
.loop:
mov al, [rsi + rdx]
mov bl, [rdi + rdx]
inc rdx
cmp al, bl
jne .not_equal
cmp al, 0
je .equal
jmp .loop
.not_equal:
mov rax, 0
ret
.equal:
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
mov rsi, rdi
mov rax, 1
mov rdi, 2
syscall
mov rax, 60
mov rdi, 101
syscall
; Memory set function
; Arguments: rdi = destination pointer, rsi = value (byte), rdx = count
; Returns: rdi (original destination pointer)
global nub_memset
nub_memset:
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 count case
test rcx, rcx
jz .done
.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:
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