-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrncmp.asm
More file actions
58 lines (46 loc) · 912 Bytes
/
strncmp.asm
File metadata and controls
58 lines (46 loc) · 912 Bytes
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
54
55
56
57
58
;;
;; EPITECH PROJECT, 2025
;; B-ASM-400-LYN-4-1-asmminilibc-spencer.pay
;; File description:
;; strncmp
;;
BITS 64
section .text
global strncmp
strncmp:
; Prologue
push rbp
mov rbp, rsp
push rcx ; Save rcx since we'll modify it
; if n == 0, return 0
test rdx, rdx
jz return_zero
loop:
; if n == 0, strings are equal up to n
test rdx, rdx
jz return_zero
; compare current characters
movzx rax, byte [rdi]
movzx rcx, byte [rsi]
; if characters differ, return difference
cmp al, cl
jne return_diff
; if we hit a null terminator, strings are equal
test al, al
jz return_zero
; move to next character
inc rdi
inc rsi
dec rdx
jmp loop
return_diff:
sub rax, rcx
jmp end
return_zero:
xor rax, rax
end:
; Epilogue
pop rcx ; Restore rcx
mov rsp, rbp
pop rbp
ret