article

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

Email
Submitted on: 1/4/2015 5:41:00 PM
By: Poltergeist (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 1940
 
     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 ;)

 
				

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
    }
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.