5

My environment is 64-bit win7, VC2010.

Of course, the intel inside is 64-bit CPU.

Can I use the 64-bit instruction/native machine word(64-bit) in a 32-bit application? Since most of my code is 32-bit, I do not want to port it to 64-bit.

For some performance critical hot code, I want to manually optimize it by compiler intrinsic or inline assembly(Also 64-bit VC compiler do NOT support inline assembly), is it possible to run 64-bit code fox example mov rax, rbx in a 32-bit mode application?

zhaorufei
  • 2,045
  • 19
  • 18
  • possible duplicate of [Lightweight method to use Amd64 instructions under 32-bit Windows?](http://stackoverflow.com/questions/7133078/lightweight-method-to-use-amd64-instructions-under-32-bit-windows) – John Zwinck Sep 28 '13 at 02:13
  • 3
    @John Zwinck: no its not a duplicate. Here the operating system is 64 bit. – Andreas H. Sep 28 '13 at 02:53
  • By the way, you can still link in separately assembled code when targeting 64bit with MSVC. – harold Sep 28 '13 at 13:14
  • porting isn't hard provided that the code doesn't assume any system-specific features like 32-bit pointer. Most of the time simply change the build the 64-bit just works – phuclv Nov 05 '17 at 02:56

2 Answers2

13

Actually, you kind of can. The mechanism used to switch from 32 to 64 bit mode (which clearly must exist on a 64 bit OS capable of running 32 bit code) is not in any way protected, and can be used from user code. On Windows it is called Heaven's Gate, and it's pretty simple: just a far call with a segment selector of 33h.

So, how to run 64bit code?

call 33h:your64bitcode
...
your64bitcode:
; do something
retf

There are, of course, some limitations to what you can do from 64bit code entered in that way, because you're not truly in a 64bit process.

harold
  • 61,398
  • 6
  • 86
  • 164
  • Can you do 64-bit system calls from PE32-in-seg64 on Windows as [you can in Linux](http://stackoverflow.com/a/32384358/673852)? – Ruslan Feb 28 '17 at 15:41
  • @Ruslan as in raw syscalls? I've never tried that, but I see no immediate reason why they wouldn't work. The usual way of calling stuff in kernel32.dll is harder since the wrong version is loaded and you can't use `LoadLibrary` but there are some tricks to load it anyway – harold Feb 28 '17 at 15:59
  • I mean the raw syscalls — those called by `syscall` instruction. – Ruslan Feb 28 '17 at 16:02
4

If your application uses the 32-bit instruction set, then no, you cannot elect to use 64-bit instructions "sometimes" within the same application. There are some options, however:

  • Use SIMD instructions, such as SSE, AVX, etc. This is documented here: http://msdn.microsoft.com/en-us/library/t467de55(v=vs.90).aspx
  • Port your 32-bit code to 64-bit. Maybe it's not so hard?
  • Split your program in two: a 32-bit process with your legacy code, and a 64-bit process with new code. You could map memory between them, and one could launch the other, so it's a bit like a multithreaded program then. Whether it would perform well depends a lot on details we don't know at this time.
John Zwinck
  • 239,568
  • 38
  • 324
  • 436