README.md

December 4, 2022 · View on GitHub

Some String/Memory Functions in C/ASM (not yet commented)

Description

Some String/Memory Functions in C/ASM (no comments yet) I may submit some comments to when people ask for it. This code is IMO only usefull for who are coding an emulator and need some extra speed, which this code will probably give. Please note that a lot of the ASM code could still be optimized a bit, but that I leave to you ;)

More Info

Submitted On
ByPoltergeist
LevelIntermediate
User Rating5.0 (10 globes from 2 users)
CompatibilityC, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
CategoryMiscellaneous
WorldC / C++
Archive File

Source Code

typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned long DWORD;

DWORD MakeDWORD(WORD hb, WORD lb)
{
    _asm {
        mov eax, WORD PTR hb
        shl eax, 8
        or eax, WORD PTR lb
    }
}

BYTE HighByte(DWORD value)
{
    _asm {
        mov eax, wordval
        shr eax, 8
        and eax, 255
    }
}

BYTE LowByte(DWORD value)
{
    _asm {
        mov eax, wordval
        and eax, 255
    }
}

DWORD StrLen(BYTE *src)
{
    _asm {
        xor eax, eax
        mov esi, src
_nextchar:
        mov cl, [esi]
        inc esi
        inc eax
        cmp cl, 0
        jz _exit
        jmp _nextchar
_exit:
    }
}

void StrCopy(BYTE *src, BYTE *dst)
{
    _asm {
        mov esi, src
        mov edi, dst
_nextchar:
        mov al, [esi]
        inc esi
        mov [edi], al
        inc edi
        cmp al, 0
        jz _exit
_exit:
    }
}

void StrLeft(BYTE *src, BYTE *dst, DWORD len)
{
    _asm {
        mov esi, src
        mov edi, src
        mov ecx, len
_nextchar:
        mov al, [esi]
        inc esi
        mov [edi], al
        inc edi
        cmp al, 0
        jz _exit
        dec ecx
        jz _addnull
        jmp _nextchar
_addnull:
        mov [edi], 0
_exit:
    }
}

void StrLcase(BYTE *src)
{
    _asm {
        mov esi, src
_nextchar:
        mov al, [esi]
        cmp al, 'A'
        jb _writeit
        cmp al, 'Z'
        ja _writeit
        add al, 32
_writeit:
        mov [esi], al
        inc esi
        cmp al, 0
        jne _nextchar
    }
}

void StrUcase(BYTE *src)
{
    _asm {
        mov esi, src
_nextchar:
        mov al, [esi]
        cmp al, 'a'
        jb _writeit
        cmp al, 'z'
        ja _writeit
        sub al, 32
_writeit:
        mov [esi], al
        inc esi
        cmp al, 0
        jne _nextchar
    }
}

void MemCopy(BYTE *src, BYTE *dst, DWORD len)
{
    _asm {
        mov esi, src
        mov edi, dst
        mov ecx, len
_next:
        mov al, [esi]
        inc esi
        mov [edi], al
        inc edi
        dec ecx
        jz _exit
        jmp _next
_exit:
    }
}

void SwapMem(DWORD *mem1, DWORD *mem2)
{
    _asm {
        mov esi, mem1
        mov edi, mem2
        mov eax, [esi]
        mov ecx, [edi]
        mov [esi], ecx
        mov [edi], eax
    }
}