95 lines
2.2 KiB
NASM
95 lines
2.2 KiB
NASM
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
|