Nightloader/i386/legacy-boot/src-asm/utility/memory.asm

152 lines
2.5 KiB
NASM
Executable File

mem_copy:
; todo(perf): Copy multiple bytes at once
.prolog:
push esi
sub esp, 64
mov esi, esp
; Save registers
mov [esi + (64 - 4)], eax
mov [esi + (64 - 8)], ebx
mov [esi + (64 - 12)], ecx
mov [esi + (64 - 16)], edx
mov [esi + (64 - 20)], edi
.body:
mov edx, [ebp - 4] ; Destination
mov edi, [ebp - 8] ; Source
xor eax, eax ; Current Byte Offset
.body.copy_loop:
mov ebx, edi
add ebx, eax
mov cl, [ebx]
mov ebx, edx
add ebx, eax
mov [ebx], cl
inc eax
cmp eax, [ebp - 12]
jb .body.copy_loop
.epilog:
; Restore registers
mov eax, [esi + (64 - 4)]
mov ebx, [esi + (64 - 8)]
mov ecx, [esi + (64 - 12)]
mov edx, [esi + (64 - 16)]
mov edi, [esi + (64 - 20)]
add esp, 64
pop esi
ret
; Furthest from EBP
; 3. Length of block 1 and block 2
; 2. Block 2
; 1. Block 1
; Nearest to EBP
mem_equal:
.prolog:
push esi
sub esp, 32
mov esi, esp
; Save registers
mov [esi + (32 - 4)], ebx
mov [esi + (32 - 8)], ecx
mov [esi + (32 - 12)], edx
mov [esi + (32 - 16)], edi
.body:
xor eax, eax ; Current Byte Offset
.comparison_loop:
mov edx, [ebp - 4] ; Block 1
mov ebx, edx
add ebx, eax
mov dl, [ebx]
mov edi, [ebp - 8] ; Block 2
mov ebx, edi
add ebx, eax
mov dh, [ebx]
inc eax
cmp dl, dh
je .length_condition
; On NOT equal
xor eax, eax
jmp .epilog
.length_condition:
inc eax
cmp eax, [ebp - 12]
jb .comparison_loop
mov eax, 1
.epilog:
; Restore registers
mov ebx, [esi + (32 - 4)]
mov ecx, [esi + (32 - 8)]
mov edx, [esi + (32 - 12)]
mov edi, [esi + (32 - 16)]
add esp, 32
pop esi
ret
; ebp - 4: Area Pointer
; ebp - 8: Length of Area
; ebp - 12: Byte Value to set
mem_set:
.prolog:
push esi
sub esp, 64
mov esi, esp
; Save registers
mov [esi + (64 - 4)], eax
mov [esi + (64 - 8)], ebx
mov [esi + (64 - 12)], ecx
mov [esi + (64 - 16)], edx
mov [esi + (64 - 20)], edi
.body:
mov ebx, [ebp - 4]
mov dl, [ebp - 12]
; Make EDI the Last Byte Pointer
mov edi, [ebp - 4]
add edi, [ebp - 8]
.body.setter_loop:
cmp ebx, edi
jae .epilog
mov [ebx], dl
inc ebx
jb .body.setter_loop
.epilog:
; Restore registers
mov eax, [esi + (64 - 4)]
mov ebx, [esi + (64 - 8)]
mov ecx, [esi + (64 - 12)]
mov edx, [esi + (64 - 16)]
mov edi, [esi + (64 - 20)]
add esp, 64
pop esi
ret