-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrcmp.asm
53 lines (39 loc) · 1.11 KB
/
strcmp.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
BITS 64 ; 64-bits mode
section .text
global strcmp ; The "strcmp" function must be callable outside
strcmp:
; "rdi" corresponds to the first string
; "rsi" corresponds to the second string
mov al, [rdi] ; Stores the current character (first string)
mov ah, [rsi] ; Stores the current character (second string)
cmp al, 0 ; Checks if the first string is finished
je strcmp_end_first
cmp ah, 0 ; Checks if the second string is finished
je strcmp_end_second
cmp al, ah ; Compares the current characters
je strcmp_equal
ja strcmp_greater
jb strcmp_lower
strcmp_equal:
inc rdi ; Moves to the next character (first string)
inc rsi ; Moves to the next character (second string)
jmp strcmp ; Recursivity
strcmp_greater:
mov rax, 1
ret
strcmp_lower:
mov rax, -1
ret
strcmp_end_first:
cmp ah, 0 ; Checks if the both strings are finished
je strcmp_end_both
mov rax, -1
ret
strcmp_end_second:
cmp al, 0 ; Checks if the both strings are finished
je strcmp_end_both
mov rax, 1
ret
strcmp_end_both:
mov rax, 0
ret