Search code examples
x86assemblyi386x86-64

Portable source code between 386 and amd64?


Goal: have a single assembler source file which will assemble both to x86 (i386) and to x86_64 (amd64)?

Is this possible, for instance with YASM?


Solution

  • If you use conditional assemblying, you can. But don't expect to write all the code once for x86 and have it work the same on x64 or the other way around. Use %if and %ifdef.

    You could also create some macros that would expand to different things. For example, some RAXPTR would expand to EAX or RAX depending on what platform you're compiling and you can use this RAXPTR where EAX/RAX is used as a pointer. Likewise you could create some macros PARAM1/2/etc that would expand to the routine params, to things like ECX/RCX and EDX/RDX for the first two parameters in the fastcall convention and [EBP+some constant]/[RBP+some constant] for the rest. Using macros in this manner can help you write mostly portable x86/x64 assembly code, but still, you can't do this without things like %if and %ifdef.