I started studying assembly since I was interested in starting to make a polymorphic engine. I started writing some functions I'd be needing but they don't seem to do what they're supposed to. They both are supposed to do the same thing just that the second one has an extra loop. My guess is that I made some weird casting there that messed things up.

#include <iostream>
#include <windows.h>
#include <Tlhelp32.h>

#include <detours.h>
#pragma comment(lib, "detours.lib")

#include <ntdll.h>
#pragma comment(lib, "ntdll.lib")

#pragma region defines

#define word WORD
#define dword DWORD
#define qword unsigned __int64
#define llong long long
#define int8 __int8
#define int16 __int16
#define int32 __int32
#define int64 __int64

#define safedw_cast *reinterpret_cast<dword*>
#define safevoid_cast reinterpret_cast<void*>

#define reca(_type) reinterpret_cast<_type>
#define stca(_type) static_cast<_type>
#define saca(_type) safe_cast<_type>
#define dyca(_type) dynamic_cast<_type>

#pragma endregion

#pragma region patches

bool _patchloop(void *_addr, dword _end, byte _bytes[], int32 _siz)
{
byte *towrite = new byte[_siz];
memset(towrite, reca(int)(_bytes), _siz);

dword tempaddr = safedw_cast(_addr);

for (tempaddr; tempaddr < _end; ++tempaddr)
{
dword old{ 0 };
if (!VirtualProtect(safevoid_cast(_addr), _siz, PAGE_EXECUTE_READWRITE, &old))
return false;

memcpy(safevoid_cast(_addr), towrite, _siz);

if (!VirtualProtect(safevoid_cast(_addr), _siz, old, &old))
return false;
}

delete[] _bytes;
delete[] towrite;
return true;
}

bool _patchadd(void *_addr, byte _bytes[], int32 _siz)
{
byte *towrite = new byte[_siz];
memset(towrite, reca(int)(_bytes), _siz);

dword old{ 0 };
if (!VirtualProtect(safevoid_cast(_addr), _siz, PAGE_EXECUTE_READWRITE, &old))
return false;

memcpy(safevoid_cast(_addr), towrite, _siz);

if (!VirtualProtect(safevoid_cast(_addr), _siz, old, &old))
return false;

delete[] _bytes;
delete[] towrite;
return true;
}

#pragma endregion