Skip to content

Commit

Permalink
Implement assembly functions using inline __asm
Browse files Browse the repository at this point in the history
This avoids having to rely on MASM.
  • Loading branch information
friendlyanon committed May 2, 2024
1 parent 16298c1 commit 0a0d956
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 81 deletions.
9 changes: 2 additions & 7 deletions wrappers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ target_compile_definitions(corkdebug PRIVATE _CRT_SECURE_NO_WARNINGS=1)

add_library(
detect486 OBJECT
detect486.asm
)

add_library(
cas OBJECT
cas.asm
detect486.c
)

include(CheckSymbolExists)
Expand All @@ -31,7 +26,7 @@ add_library(
comdlg32.c
corkel32.def
)
target_link_libraries(corkel32 PRIVATE corkdebug detect486 cas)
target_link_libraries(corkel32 PRIVATE corkdebug detect486)
target_compile_definitions(corkel32 PRIVATE _CRT_SECURE_NO_WARNINGS=1)

# NTDLL => CORNT
Expand Down
17 changes: 0 additions & 17 deletions wrappers/cas.asm

This file was deleted.

10 changes: 0 additions & 10 deletions wrappers/cas.h

This file was deleted.

28 changes: 0 additions & 28 deletions wrappers/detect486.asm

This file was deleted.

26 changes: 26 additions & 0 deletions wrappers/detect486.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "detect486.h"

int __declspec(naked) is_cpu_486_or_newer(void)
{
__asm {
pushfd
pop eax
mov ebx, eax
xor eax, 40000h ; toggle the AC bit in EFLAGS (only available in 486 or newer)
push eax
popfd
pushfd
pop eax
cmp eax, ebx
jz is_386
push ebx
popfd
xor eax, eax
inc eax
ret

is_386:
xor eax, eax
ret
}
}
6 changes: 1 addition & 5 deletions wrappers/detect486.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#ifndef DETECT_486
#define DETECT_486

#ifndef STDCALL
#define STDCALL __stdcall
#endif

int STDCALL is_cpu_486_or_newer(void);
int is_cpu_486_or_newer(void);

#endif // DETECT_486
41 changes: 27 additions & 14 deletions wrappers/kernel32.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <stdio.h>
#include <windows.h>

#include "cas.h"
#include "debug.h"
#include "detect486.h"

Expand Down Expand Up @@ -155,22 +154,36 @@ BOOL WINAPI CORKEL32_DeviceIoControl(HANDLE hDevice, DWORD dwIoControlCode, LPVO
return e;
}

static char const* InterlockedCompareExchange_name = "InterlockedCompareExchange";

// Reimplemented
LONG WINAPI CORKEL32_InterlockedCompareExchange(LONG* dest, LONG xchg, LONG compare)
__declspec(naked) LONG WINAPI CORKEL32_InterlockedCompareExchange(LONG* dest, LONG exchange, LONG compare)
{
LONG temp;
if (has_cmpxchg) {
return InterlockedCompareExchange_486(dest, xchg, compare);
__asm {
cmp has_cmpxchg, 0
jz ICE_is_386
mov edx, DWORD PTR [esp + 4] ; dest
mov ecx, DWORD PTR [esp + 8] ; exchange
mov eax, DWORD PTR [esp + 12] ; compare
lock cmpxchg DWORD PTR [edx], ecx
ret 12

ICE_is_386:
push InterlockedCompareExchange_name
push TRACE_FORCE_DONT_PRINT
call Trace
add esp, 8

mov edx, DWORD PTR [esp + 4] ; dest
mov eax, DWORD PTR [edx]
cmp eax, DWORD PTR [esp + 12] ; compare
jne ICE_exit
mov ecx, DWORD PTR [esp + 8] ; exchange
mov DWORD PTR [edx], ecx

ICE_exit:
ret 12
}

temp = *dest;
Trace(TRACE_FORCE_DONT_PRINT, "InterlockedCompareExchange");

if (compare == *dest) {
*dest = xchg;
}

return temp;
}

HANDLE WINAPI CORKEL32_CreateToolhelp32Snapshot(DWORD param_0, DWORD param_1)
Expand Down

0 comments on commit 0a0d956

Please sign in to comment.