Adds strfind

This commit is contained in:
2025-07-28 16:54:55 +02:00
parent 1848e815fb
commit eb5398da8f

View File

@@ -6,6 +6,7 @@ section .text
global strcat
global strclr
global strcmp
global strfind
;----- strlen(*str[]) -----;
; Gets the length of given string
@@ -149,3 +150,58 @@ strcmp:
.quit:
ret
;----- strfind(*str[], *what[], start_at) -----;
; Finds string inside another string.
; Return values: 0 if what[] wasn't found within str[]. Position (index+1) where first character was found in str[] otherwise
; Used registers:
; rax* (ret)
; rdi (arg) Pointer to str[]
; rsi (arg) Pointer to what[]
; rdx (arg) Where to start looking in str[] (index)
; rcx* Keeps track of current position in str[]
; r8* Stores index where first character match of what[] was found
; r9* Used to move byte for cmp instruction
; r10* Keeps track if there is currently a match between str[] and what[]
; r11* Backup for original pointer to what[]
strfind:
xor rax, rax
mov rcx, 1
add rcx, rdx
add rdi, rdx
xor r10b, r10b
mov r11, rsi
.findStr:
cmp byte [rsi], EOS
je .testFullMatch
cmp byte [rdi], EOS
je .quit
mov r9b, byte [rsi]
cmp byte [rdi], r9b
jne .noMatch
;.hasMatch:
test r10b, r10b
jnz .notFirstChar
mov r10b, 1
mov r8, rcx
.notFirstChar:
inc rsi
jmp .continueLoop
.noMatch:
; not gonna figure out here if there was an existing match already. just XOR r10b,r10b and load original pointer to what[] to rsi
xor r10b, r10b
mov rsi, r11
.continueLoop:
inc rdi
inc rcx
jmp .findStr
.testFullMatch:
test r10b, r10b
jz .quit
mov rax, r8
.quit:
ret