Paralyz3d Paralyz3d - 2 months ago 11
C++ Question

Can a 64 bit system run a 32 bit software which contains some inline assembly?

So, I have a part in my code which can't be done without assembly but my software needs to be able to run on 64 bit systems, so if If compile the code on a x86 machine will it run on a 64 bit machine? If not what can I do?

My function which is written in assembly needs to perform some XOR operations at runtime of the program's memory(kind of protection against debugging), to be specific it encrypts some functions then decrypts when needed.

__asm
{
push eax
push ecx
mov ecx, StartAddress
add ecx fSize
mov eax StartAddress

protect:
xor byte ptr ds:[eax], 0x25
inc eax
cmp eax, ecx
jl protect;

pop ecx
pop eax
}

Answer

In general, no.

In x64 the single byte inc/dec instructions have been repurposed as REX prefixes.
That means your code will fail at:

    protect:
        xor byte ptr ds:[eax], 0x25
        inc eax                       <<-- will not work in X64
        cmp eax, ecx

Apart from that

    mov ecx, StartAddress   
    ....
    mov eax StartAddress

Will not work, you need to load pointers into 64 bit registers, or your code will fail if it's loaded at a high address.

Even though many of the opcodes are the same between x86 and x64.
A single run of 32-bit code in a 64-bit process will fail pretty quickly.

If not what can I do?

Change the code so that it conforms to 64-bit.
You need to change all pointers/addresses to be 64 bits wide.
You need to change all registers addressing memory to 64-bits.

__asm
{
    push rax
    push rcx
    mov rcx, StartAddress
    add rcx fSize
    mov rax StartAddress

    protect:
        xor byte ptr [rax], 0x25  //ds is the default.
        inc rax
        cmp rax, rcx
    jl protect;

    pop rcx
    pop rax
}

You cannot run a 32 bit program inside a 64-bit process.
The only reason Win64 et al manage this trick is that the OS performs a privileged mode change back to 32-bit mode as needed.

Because Microsoft C++ compilers are crippled and do not support inline 64-bit assembly you'll have to either use Embarcadero's C++builder of G++ if you want to use 64-bit assembly.

Comments