Pelles C forum

Assembly language => Assembly discussions => Topic started by: Jokaste on October 31, 2017, 02:49:13 PM

Title: Inline
Post by: Jokaste on October 31, 2017, 02:49:13 PM
In WinNt.h I found this code:
Code: [Select]

__inline ULONGLONG NTAPI Int64ShrlMod32(ULONGLONG Value, DWORD ShiftCount) {
    __asm {
        mov ecx, ShiftCount
        mov eax, dword ptr[Value]
        mov edx, dword ptr[Value +4]
        shrd eax, edx, cl
        shr edx, cl
    }
}


When writing C code Mr Orinius does not accept __asm{...}! Why is there this code?
Title: Re: Inline
Post by: Vortex on October 31, 2017, 08:09:28 PM
Hi Jokaste,

Here is a quick example :

Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

__declspec(naked) char* UpperCase(char* szText) /* Turn off framing to get
a smaller sized function */
{
__asm{
mov eax,[esp+4] /* get the address of the string to be converted */
sub eax,1
__repeat:
add eax,1
movzx ecx,BYTE PTR [eax]
test ecx,ecx
je __end
cmp ecx,97 /* if ASCII(ecx) < 97 then ignore the charater */
jb __repeat
cmp ecx,122 /* if ASCII(ecx) > 122 then ignore the charater */
ja __repeat
sub BYTE PTR [eax],32 /* convert lowecase to uppercase */
jmp __repeat
__end:
mov eax,[esp+4]
ret 4 /* manual stack balance */
}
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
char msg[]="inline assembly programming";
MessageBox(0,UpperCase(msg),"Hello!",MB_OK);
return 0;
}
Title: Re: Inline
Post by: TimoVJL on November 01, 2017, 10:49:38 AM
In WinNt.h I found this code:
Code: [Select]

__inline ULONGLONG NTAPI Int64ShrlMod32(ULONGLONG Value, DWORD ShiftCount) {
    __asm {
        mov ecx, ShiftCount
        mov eax, dword ptr[Value]
        mov edx, dword ptr[Value +4]
        shrd eax, edx, cl
        shr edx, cl
    }
}


When writing C code Mr Orinius does not accept __asm{...}! Why is there this code?

Large Integer Functions (https://msdn.microsoft.com/en-us/library/aa383711(v=vs.85).aspx)

it is for x86 / i386 only.
for x64 there is
Code: [Select]
#define Int64ShrlMod32(a,b)  (((unsigned __int64)(a)) >> (b))