From eb5398da8ffbbafff4a08974bb0c9905fa3d0414 Mon Sep 17 00:00:00 2001 From: Kwarde Date: Mon, 28 Jul 2025 16:54:55 +0200 Subject: [PATCH] Adds strfind --- src/string.asm | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/string.asm b/src/string.asm index 920b594..50266b5 100644 --- a/src/string.asm +++ b/src/string.asm @@ -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