summaryrefslogtreecommitdiff
path: root/zap/source/amd64/mem/memeq.S
blob: b30a8848bb73696dc0310a20f73186311aaf9b04 (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Copyright 2022 Gabriel Jensen.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

.globl zap_memeq

zap_memeq:
# Left pointer:
#define laddr  %rdi
# Number of remaining elements:
#define rem    %rsi
# Right pointer:
#define raddr  %rdx
# Current left element:
#define lval8 %rax
#define lval1  %al
# Current right element:
#define rval8 %rcx
#define rval1  %cl

	# Compare words:
.wrdcmp:

	# Check if there's at least one word left:
	cmpq $0x8,rem # if (rem == 8)
	jl .bytecmp   # goto bytecmp
	
	# Copy the values into registers:
	movq (laddr),lval8 # lval8 = *laddr
	movq (raddr),rval8 # rval8 = *raddr

	# Check if the words are equal:
	cmpq lval8,rval8 # if (lval8 != rval8)
	jne .neq           # goto neq

	# Mark eight more bytes as equal:
	addq $0x8,laddr # laddr += 0x8
	addq $0x8,raddr # raddr += 0x8
	subq $0x8,rem   # rem -= 0x8

	# Continue to the next word:
	jmp .wrdcmp # goto wrdcmp

	# Compare bytes:
.bytecmp:

	# Check if there are any bytes left:
	testq rem,rem # if (rem == 0x0)
	jz .eq        # goto eq         // If we have reached the final element, all previous elements have compared equal, and the memory sequences are equal.

	# Copy the values into registers:
	movb (laddr),lval1 # lval1 = *laddr
	movb (raddr),rval1 # rval1 = *raddr

	cmpb lval1,rval1 # if (lval1 != rval1)
	jne .neq         # goto neq
	
	# Mark another byte as equal:
	incq laddr # ++laddr
	incq raddr # ++raddr
	decq rem   # --rem
	
	# Continue to the next byte:
	jmp .bytecmp # goto bytecmp

	# The memory sequences have compared equal:
.eq:
	movb $0xFF,%al
	ret            # return FF

	# The memory sequences have compared NOT equal:
.neq:
	movb $0x0,%al
	ret           # return 0