flat assembler
Message board for the users of flat assembler.
Index
> Main > Mixing x86 with x64 code |
Author |
|
Andy 02 Dec 2023, 23:51
I usually use fasm to generate binary code that will be called from a high level language but I am tired to write one version of code to compiled as 32 bit application and then to rewrite the code to compiled as 64 bit application. To illustrate what I mean I have the code below that is a basic function that adds two integers.
Code: mov ax, cs cmp ax, 33h ; cs = 0x23/0x33 -> x86/x64 mode je x64 use32 mov eax, [esp+4] add eax, [esp+8] ret 8 use64 x64: mov rax, rcx add rax, rdx ret I read here that windows use a call to fs:[0xC0] to switch into 64 bit mode when running a 32 bit application on 64 bit CPU. I wonder if I can use this method to switch immediately into 64 bit mode when my function it's called so I don't have to write the same code for 32 bit also? If the answer it's yes, can I have an example? |
|||
02 Dec 2023, 23:51 |
|
bitRAKE 03 Dec 2023, 04:41
I think this still works:
https://board.flatassembler.net/topic.php?p=97551#97551 (There is other code on the board - I just couldn't find it, atm.) _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
03 Dec 2023, 04:41 |
|
bitRAKE 03 Dec 2023, 17:02
From the high-level language (HLL) perspective there is no 32-bit/64-bit common solution for Windows. From the low-level perspective a general adaptor layer is non-trivial.
If the goal is to have your functions accessible from many high-level languages, both 32-bit and 64-bit execution environments, then my recommendation is to use macros to abstract the parameter passing on your functions - to meet the needs of the execution environment. This will allow a single assembly function to be compiled for a large array of environments -- WITHOUT a runtime adaptor layer and getting the full benefit of assembly. Also, it requires no change on the HLL side. https://en.wikipedia.org/wiki/X86_calling_conventions#List_of_x86_calling_conventions Also, see the PROC macros in fasm[g]. |
|||
03 Dec 2023, 17:02 |
|
Andy 03 Dec 2023, 18:15
bitRAKE wrote: If the goal is to have your functions accessible from many high-level languages, both 32-bit and 64-bit execution environments, then my recommendation is to use macros to abstract the parameter passing on your functions - to meet the needs of the execution environment. This is exactly my goal but I still don't fully understand how this can be done with proc macros. Do I have to write a macro for stdcall procedure that will be called from a 32 bit environment and another one for fastcall when the procedure it's called from 64 bit environment? I would be greateful if you can show me a basic example. If you want take the Add function from the first post as example so I can understand better. I am a beginner so it would be easier for me to understand. |
|||
03 Dec 2023, 18:15 |
|
bitRAKE 03 Dec 2023, 19:05
With fasmg we don't need to get terribly complex - I like all the information for a function in one place = fewer surprises. Here is a terse example:
Code: ; set this on the command line for fasmg (example: -I "ABI := 2"): ;ABI := 1 ; 32-bit, __cdecl ABI := 2 ; 64-bit, Microsoft x64 calling convention ;ABI := 3 ; 64-bit, System V AMD64 ABI MyAdd: iterate <parm0, parm1, return>,\ [esp+4], [esp+8], ret 8,\ ecx, edx, retn,\ edi, esi, retn indx ABI ; table selection based on convention mov eax, parm0 add eax, parm1 return break ; don't loop end iterate At the other extreme, we could build a custom syntax: Code: func MyAdd parm0:dword, parm1:dword mov eax, parm0 add eax, parm1 return endf edit: for fasm JohnFound has done a lot of work on creating a common framework for using x86 in multiple environments with Fresh IDE and its supporting code library. _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
03 Dec 2023, 19:05 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.