flat assembler
Message board for the users of flat assembler.

flat assembler > High Level Languages > GNU 'g77' Fortran interface with FASM subroutine (ia32)

Author
Thread Post new topic Reply to topic
NimbUs



Joined: 24 Jun 2017
Posts: 3
Hi ! I want to write an assembly language "subroutine" (in Fortran parlance) callable from a Main driver written in Fortran - GNU g77 dialect. Intel X86-IA32 specific.

Is FASM suitable for the task at hand ? If applicable I would appreciate some guide/template/sample/'hello world'... demonstrating the whole build process.

P.S. I have googled "fortran site:board.flatassembler.net" to no avail.

TIA

--
NimbUs
Post 24 Jun 2017, 14:48
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15870
Location: 162173 Ryugu
There are three methods I can think of that your Fortran compiler might support.
  1. Compile your assembly function as a DLL and use whatever Fortran mechanism it has to load and execute DLL procedures.
  2. Compile your assembly code as a linkable object and link it with the Fortran linkable object.
  3. Use inline assembly within the Fortran compiler (although this might not be supported (we're not sure what compiler you are using) and it wouldn't use fasm either).
Post 24 Jun 2017, 15:43
View user's profile Send private message Visit poster's website Reply with quote
NimbUs



Joined: 24 Jun 2017
Posts: 3
Hi revolution ! Thank you for the kind attention !
The devil - as we know - is in the details, and it is the interfacing details I am querying for. As for your genral suggestions,
1. I don't think g77 has a mechansm whereby compiled programs would run (MS windows) DLLs
3. g77 does not support inline assembly, unlike some newer GNU Fortran compilers.
2. This, I guess, is what I'm asking, a "how-to" : details of all the needed glue. I am not even sure of the format of object files used by Fortran, though I assume they would be Coff rather than old Intel/MS "OMF". Can FASM output object modules instead of statically compiled whole programs ? How do we instruct it it do so ? How does it exmport/import public names ? I'd really really need to see a sample, or template albeit incomplete.

--
NimbUs[/i]


Last edited by NimbUs on 24 Jun 2017, 16:15; edited 1 time in total
Post 24 Jun 2017, 16:12
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15870
Location: 162173 Ryugu
fasm comes with example files in the download. Look in the "EXAMPLES" folder for MSCOFF.ASM file. You should find this:
Code:
; example of making Win32 COFF object file format MS COFF extrn '__imp__MessageBoxA@16' as MessageBox:dword section '.text' code readable executable public _demo _demo: push 0 push _caption push _message push 0 call [MessageBox] ret section '.data' data readable writeable _caption db 'Win32 assembly',0 _message db 'Coffee time!',0
Post 24 Jun 2017, 16:14
View user's profile Send private message Visit poster's website Reply with quote
Xorpd!



Joined: 21 Dec 2006
Posts: 161
g77 can run DLLs. Just make a *.LIB file when you make the *.DLL and link against the *.LIB file and make sure the *.DLL is in your path when you run the executable. Now, if the *.DLL procedure is STDCALL that may be a problem.

You can find out the format of a *.OBJ file or *.o file with objdump.exe, which comes with the GNU utilities, thus you probably already have it in your path as a consequence of having installed g77.

FASM can compile to *.OBJ files. The secret sauce is 'format MS coff' or 'format MS64 coff' or 64-bit code. It can export name via the public keyword. A brief example:
Code:
D:\gfortran\clf\rdtsc>type rdtsc_32.asm format MS coff section '.text' code readable executable public _rdtsc_32_ _rdtsc_32_: rdtsc ret D:\gfortran\clf\rdtsc>fasm rdtsc_32.asm flat assembler version 1.71.49 (1048576 kilobytes memory) 1 passes, 114 bytes. D:\gfortran\clf\rdtsc>objdump -f rdtsc_32.obj rdtsc_32.obj: file format pe-i386 architecture: i386, flags 0x00000039: HAS_RELOC, HAS_DEBUG, HAS_SYMS, HAS_LOCALS start address 0x00000000 D:\gfortran\clf\rdtsc>type hello32.f PROGRAM HELLO32 IMPLICIT NONE INTEGER*8 RDTSC_32 EXTERNAL RDTSC_32 INTEGER*8 T0, TF T0 = RDTSC_32() WRITE(*,*) 'Hello, world' TF = RDTSC_32() WRITE(*,*) 'Time = ',TF-T0 END D:\gfortran\clf\rdtsc>gfortran hello32.f rdtsc_32.obj -ohello32 D:\gfortran\clf\rdtsc>hello32 Hello, world Time = 570736

I'm not sure whether this will work with g77, though. Use objdump.exe on a g77-generated *.o file to discover file format, name mangling, and calling convention.

g77 is kind of archaic and you will get much better help if you can move up to gfortran. You will want to anyhow when you find that 32-bit code doesn't have a hardware instruction for 64X64->128 multiply. Just sayin'.
Post 25 Jun 2017, 01:46
View user's profile Send private message Visit poster's website Reply with quote
NimbUs



Joined: 24 Jun 2017
Posts: 3
Back to report I have solved the actual Fortran-ASM interfacing problem,
using not FASM but NASM this time, another fine assembler (which I had not previously tried) that will natively assemble to a number of object file formats, including Coff and its win32 variant, which is exactly what was expected by the g77 compiler suite on my (Windows XP 32 bits) platform... The interested could find details in a recent thread in the usenet news group comp.lang.asm.x86 (also archived as "google groups").

@Revolution, @Xorpd! : thanks for your above replies, which show that there are probably ways to skin this cat using the FASM, too...
Post 27 Jun 2017, 11:23
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 © 2004-2018, Tomasz Grysztar.

Powered by rwasa.