flat assembler
Message board for the users of flat assembler.

Index > Main > Mixing x86 with x64 code

Author
Thread Post new topic Reply to topic
Andy



Joined: 17 Oct 2011
Posts: 55
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?
Post 02 Dec 2023, 23:51
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4060
Location: vpcmpistri
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
Post 03 Dec 2023, 04:41
View user's profile Send private message Visit poster's website Reply with quote
Andy



Joined: 17 Oct 2011
Posts: 55
Andy 03 Dec 2023, 13:30
Thanks. I tried the example that you suggested but it crash the application. I suppose I should switch back to 64 bit mode before exit.

Let's take my example above with Add function. If the code it's called from a 32 bit application I want to jump directly to function and call it, that's straightforward.

But if the code it's called from a 64 bit application when I jump into 32 bit mode there should be some additional steps:
- since the fastcall send the first two parameters in rcx and rdx probably I should push them to stack according to stdcall calling convention
- run the 32 bit code
- switch back to 64 bit mode

Am I right?
Post 03 Dec 2023, 13:30
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4060
Location: vpcmpistri
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].
Post 03 Dec 2023, 17:02
View user's profile Send private message Visit poster's website Reply with quote
Andy



Joined: 17 Oct 2011
Posts: 55
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.
Post 03 Dec 2023, 18:15
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4060
Location: vpcmpistri
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    
... obviously, there are many ways to accomplish this.

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    
... I would use the PROC macros as a guide for creating such an abstraction layer.

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
Post 03 Dec 2023, 19:05
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.