-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_strchr.asm
31 lines (26 loc) · 1.33 KB
/
my_strchr.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
BITS 64 ; 64 bits mode
SECTION .text ; Code section
GLOBAL strchr ; export strchr
strchr:
PUSH RCX ; push the counter in the stack
XOR RCX, RCX ; clear the counter RCX
XOR RAX, RAX ; set RAX to null (return value)
_strchr_loop:
CMP BYTE [RDI + RCX], SIL ; compare RDI char 1 by 1 with the 2nd parameter SIL(1 byte)
JE _strchr_found ; if cmp is ok -> jump to _strchr_found
CMP BYTE [RDI + RCX], 0 ; compare RDI char 1 by 1
JZ _strchr_null ; if cmp is ok (equal null) -> jump to strchr_null
INC RCX ; increment the counter
JMP _strchr_loop ; loop
_strchr_found:
MOV RAX, RDI ; put the string into RAX (return value)
ADD RAX, RCX ; add the counter to delete the string before the letter, into RAX
POP RCX ; restore the counter
RET ; leaving
_strchr_null:
XOR RAX, RAX ; restore RAX if nothing _strchr_found
POP RCX ; resrore the counter
RET ; leave
; char *strchr(const char *s, int c)
; RDI = const char *s
; RSI = int c