misc: Move to assembly for mem family functions

This commit is contained in:
mintsuki 2021-12-10 02:56:56 +01:00
parent 0f35483ee7
commit 48828f316b
4 changed files with 164 additions and 85 deletions

View File

@ -18,56 +18,6 @@ int tolower(int c) {
return c;
}
void *memcpy(void *dest, const void *src, size_t n) {
uint8_t *pdest = dest;
const uint8_t *psrc = src;
for (size_t i = 0; i < n; i++) {
pdest[i] = psrc[i];
}
return dest;
}
void *memset(void *s, int c, size_t n) {
uint8_t *p = s;
for (size_t i = 0; i < n; i++) {
p[i] = (uint8_t)c;
}
return s;
}
void *memmove(void *dest, const void *src, size_t n) {
uint8_t *pdest = dest;
const uint8_t *psrc = src;
if (src > dest) {
for (size_t i = 0; i < n; i++) {
pdest[i] = psrc[i];
}
} else if (src < dest) {
for (size_t i = n; i > 0; i--) {
pdest[i-1] = psrc[i-1];
}
}
return dest;
}
int memcmp(const void *s1, const void *s2, size_t n) {
const uint8_t *p1 = s1;
const uint8_t *p2 = s2;
for (size_t i = 0; i < n; i++) {
if (p1[i] != p2[i])
return p1[i] < p2[i] ? -1 : 1;
}
return 0;
}
char *strcpy(char *dest, const char *src) {
size_t i;

View File

@ -55,38 +55,3 @@ __divmoddi4:
mov dword [ecx+4], 0
xor edx, edx
ret
global memcpy32to64
memcpy32to64:
bits 32
push ebp
mov ebp, esp
push esi
push edi
push 0x28
call .p1
.p1:
add dword [esp], .mode64 - .p1
retfd
bits 64
.mode64:
mov rdi, [rbp + 8]
mov rsi, [rbp + 16]
mov rcx, [rbp + 24]
rep movsb
push 0x18
call .p2
.p2:
add qword [rsp], .mode32 - .p2
retfq
bits 32
.mode32:
pop edi
pop esi
pop ebp
ret

55
stage23/lib/mem.asm64 Normal file
View File

@ -0,0 +1,55 @@
section .text
global memcpy
memcpy:
mov rcx, rdx
mov rax, rdi
rep movsb
ret
global memset
memset:
push rdi
mov rax, rsi
mov rcx, rdx
rep stosb
pop rax
ret
global memmove
memmove:
mov rcx, rdx
mov rax, rdi
cmp rdi, rsi
ja .copy_backwards
rep movsb
jmp .done
.copy_backwards:
lea rdi, [rdi+rcx-1]
lea rsi, [rsi+rcx-1]
std
rep movsb
cld
.done:
ret
global memcmp
memcmp:
mov rcx, rdx
repe cmpsb
jecxz .equal
mov al, byte [rdi-1]
sub al, byte [rsi-1]
movsx rax, al
jmp .done
.equal:
xor eax, eax
.done:
ret

109
stage23/lib/mem.s2.asm32 Normal file
View File

@ -0,0 +1,109 @@
section .text
global memcpy
memcpy:
push esi
push edi
mov eax, dword [esp+12]
mov edi, eax
mov esi, dword [esp+16]
mov ecx, dword [esp+20]
rep movsb
pop edi
pop esi
ret
global memset
memset:
push edi
mov edx, dword [esp+8]
mov edi, edx
mov eax, dword [esp+12]
mov ecx, dword [esp+16]
rep stosb
mov eax, edx
pop edi
ret
global memmove
memmove:
push esi
push edi
mov eax, dword [esp+12]
mov edi, eax
mov esi, dword [esp+16]
mov ecx, dword [esp+20]
cmp edi, esi
ja .copy_backwards
rep movsb
jmp .done
.copy_backwards:
lea edi, [edi+ecx-1]
lea esi, [esi+ecx-1]
std
rep movsb
cld
.done:
pop edi
pop esi
ret
global memcmp
memcmp:
push esi
push edi
mov edi, dword [esp+12]
mov esi, dword [esp+16]
mov ecx, dword [esp+20]
repe cmpsb
jecxz .equal
mov al, byte [edi-1]
sub al, byte [esi-1]
movsx eax, al
jmp .done
.equal:
xor eax, eax
.done:
pop edi
pop esi
ret
global memcpy32to64
memcpy32to64:
push ebp
mov ebp, esp
push esi
push edi
push 0x28
call .p1
.p1:
add dword [esp], .mode64 - .p1
retfd
bits 64
.mode64:
mov rdi, [rbp + 8]
mov rsi, [rbp + 16]
mov rcx, [rbp + 24]
rep movsb
push 0x18
call .p2
.p2:
add qword [rsp], .mode32 - .p2
retfq
bits 32
.mode32:
pop edi
pop esi
pop ebp
ret