In this project, your task is to implement the strstr
function, which searches
one string for the first occurrence of a pattern string:
char* strstr(char* text, char* ptn);
strstr
returns a pointer to the first occurrence of ptn
within text
, or
to the nul character at the end of text
if no occurrences can be found.
section .data
search_text1: db "The cat in the hat.", 0
search_text2: db "The dog in the hat.", 0
search_text3: db "potato ca", 0
ptn_text: db "cat", 0
not_found: db "Not found", 10, 0
section .text
extern printf
global main
main:
push rbp
mov rbp, rsp
mov rdi, search_text1
mov rsi, ptn_text
call asm_strstr
cmp byte [rax], 0
je .not_found1
; Found
mov rdi, rax
mov al, 0
call printf
jmp .search2
.not_found1:
mov rdi, not_found
call printf
.search2:
mov rdi, search_text2
mov rsi, ptn_text
call asm_strstr
cmp byte [rax], 0
je .not_found2
; Found
mov rdi, rax
call printf
jmp .search3
.not_found2:
mov rdi, not_found
call printf
.search3:
mov rdi, search_text3
mov rsi, ptn_text
call asm_strstr
cmp byte [rax], 0
je .return
; Found
mov rdi, rax
call printf
jmp .return
.return:
pop rbp
mov rax, 0
ret
asm_strstr:
push rbp
mov rbp, rsp
; rdi = char* text
; rsi = char* ptn
; rax = returned char*
; Your code here.
pop rbp
ret
If your asm_strstr
works correctly, the program should print
cat in the hat.
followed by Not found
twice. The provided main
checks
your code in three situations:
Where the ptn string is found
Where the ptn string is not found
Where the ptn string is partially found, but the search text ends before the match is complete. (I.e., it tries to find “cat” in “potato ca”.)