# 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