flat assembler
Message board for the users of flat assembler.

Index > Non-x86 architectures > fasmg: Mix arm64 with x86

Author
Thread Post new topic Reply to topic
alorent



Joined: 05 Dec 2005
Posts: 221
alorent 29 Nov 2022, 17:58
Hi,

I need to insert a specific ARM64 code sequence in my x86 assembly file.

I did something stupid like:

Code:
include 'win32ax.inc' 

.code

; ---------------------------
; This function is in x86
; ---------------------------
MyFunction1:
        mov     eax, 1
        ret

; ---------------------------
; This function is in ARM64
; ---------------------------
include 'cpu/aarch64.inc'

MyFunction2:
        ret

; ---------------------------
; Get back to x86 
; ---------------------------
include 'cpu/80386.inc'

  start:

        invoke  ExitProcess,0

.end start
    


Of course, assembler errors everywhere!

Is it possible to mix an ARM64 function in my x86 assembly file? You might think that I'm crazy, but I need to return from a x86 function the hex-bytes in the arm64 function.

Thanks!
Post 29 Nov 2022, 17:58
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8344
Location: Kraków, Poland
Tomasz Grysztar 29 Nov 2022, 18:50
Yes, it's possible with help of namespace separation:
Code:
include 'win32ax.inc'

define ARM64
namespace ARM64
    include 'cpu/aarch64.inc'
end namespace

.code

; ---------------------------
; This function is in x86
; ---------------------------
MyFunction:
    mov     eax, 1
    ret

namespace ARM64
    ; ---------------------------
    ; This function is in ARM64
    ; ---------------------------
    MyFunction:
        ret
end namespace

; Back to x86. Here access the function as ARM64.MyFunction

start:
    invoke  ExitProcess,0

.end start    
This is a "weak" separation, any x86 instructions that are not overridden by ARM64 ones are still visible in the embedded namespace, and the architectures may interfere with each other.

For a clean solution, you could make it a strong isolation:
Code:
; In the main namespace, define only PE format structures, with no specific CPU instructions:
PE.Settings.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE or IMAGE_FILE_32BIT_MACHINE or IMAGE_FILE_LINE_NUMS_STRIPPED or IMAGE_FILE_LOCAL_SYMS_STRIPPED
PE.Settings.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI
format binary as 'exe'
include 'format/pe.inc'

; When PE.INC was included manually, WIN32AX.INC does not do it itself:
include 'win32ax.inc'

; Define global anchors for the namespaces:
define X86
define ARM64

; Put instruction sets into separated namespaces. Now they cannot interfere with each other.
namespace X86
    include 'cpu/80386.inc'
    use32
end namespace

namespace ARM64
    include 'cpu/aarch64.inc'
end namespace

.code

namespace X86
    ; ---------------------------
    ; This function is in x86
    ; ---------------------------
    MyFunction:
        mov     eax, 1
        ret
end namespace

namespace ARM64
    ; ---------------------------
    ; This function is in ARM64
    ; ---------------------------
    MyFunction:
        ret
end namespace

namespace X86

    start:
        invoke  ExitProcess,0

end namespace

.end X86.start    
This is in fact a perfect demonstration of the intended purpose of fasmg's namespaces.

To confirm correctness you can include 'listing.inc' (put it after the ".code" line to avoid processing all the headers). I got:
Code:
[0000000000401000]                                    namespace X86
[0000000000401000]                                    MyFunction:
[0000000000401000] 00000200: B8 01 00 00 00           mov eax, 1
[0000000000401005] 00000205: C3                       ret
[0000000000401006]                                    namespace ARM64
[0000000000401006]                                    MyFunction:
[0000000000401006] 00000206: C0 03 5F D6              ret
[000000000040100A]                                    namespace X86
[000000000040100A]                                    start:
[000000000040100A] 0000020A: 6A 00 FF 15 40 20 40 00  invoke ExitProcess,0    
You can also mix different flavors of x86 in the exact same way.
Post 29 Nov 2022, 18:50
View user's profile Send private message Visit poster's website Reply with quote
alorent



Joined: 05 Dec 2005
Posts: 221
alorent 30 Nov 2022, 04:35
Hi Tomasz,

Thanks a lot! Amazing!

I never saw a more robust and flexible software in my life! Very Happy

I have done a quick test and it works perfectly!

Thanks again!
Post 30 Nov 2022, 04:35
View user's profile Send private message 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.