summaryrefslogtreecommitdiff
path: root/zap/source/amd64/mem/memfnd.S
blob: 0b5a47e62da4b2d327ffe30834486b0b4159b928 (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
# 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_memfnd

zap_memfnd:
# Address of the current element:
#define addr %rax
# Address of the first element:
#define start %rdi
# Address of the element after the last element:
#define afterbuf %rsi
# Byte value:
#define cmp %dl
# Current byte:
#define val %cl

	movq start,addr # addr = start

	addq start,afterbuf # afterbuf += start

	# Iterate over the array:
.loop:

	# Check if we have reached the end of the array:
	cmpq addr,afterbuf # if (addr == afterbuf)
	je .nfnd           # goto nfnd
	
	# Check if we have found the byte value:
	movb (addr),val # val = *addr
	cmpb val,cmp    # if (val == cmp)
	je .fnd         # goto fnd

	# Continue to the next byte:
	incq addr # ++addr
	jmp .loop # goto loop

	# Found:
.fnd:

	# Get the offset of the byte:
	subq start,addr # addr -= start
	ret             # return addr

	# Not found:
.nfnd:

	movq $0xFFFFFFFFFFFFFFFF,addr # addr = FFFFFFFFFFFFFFFF
	ret                           # return addr