### Author Topic: masm example  (Read 12579 times)

#### Gunther

• Member
• Posts: 3585
• Forgive your enemies, but never forget their names
##### masm example
« on: January 13, 2013, 07:48:33 AM »
Does the masm32 package contain an example for a procedure which is callable from C (VC, gcc or whatever Windows C compiler)? I haven't found nothing.

Gunther
Get your facts first, and then you can distort them.

#### dedndave

• Member
• Posts: 8827
• Still using Abacus 2.0
##### Re: masm example
« Reply #1 on: January 13, 2013, 07:54:14 AM »
i seem to recall a thread in the old forum about "c-callable" routines

#### qWord

• Member
• Posts: 1473
• The base type of a type is the type itself
##### Re: masm example
« Reply #2 on: January 13, 2013, 08:03:45 AM »
That is very simple if you use PROC/ENDP
Code: [Select]
`include \masm32\include\masm32rt.inc.code; double foo(double x); // assuming __cdeclfoo proc c x:REAL8 fld x fmul st,st ret foo endpend`for the C program, add the corresponding *.obj file to the linker.
You may also take a look into Agner Fogs calling convention manual  to look up parameter passing and return values.
MREAL macros - when you need floating point arithmetic while assembling!

#### Gunther

• Member
• Posts: 3585
• Forgive your enemies, but never forget their names
##### Re: masm example
« Reply #3 on: January 13, 2013, 08:30:56 AM »
Thank you qWord, that helped really.  :t

Gunther
Get your facts first, and then you can distort them.

#### MichaelW

• Global Moderator
• Member
• Posts: 1209
##### Re: masm example
« Reply #4 on: January 13, 2013, 09:05:10 AM »
A minimal-overhead (32-bit integer) compare procedure for the CRT qsort function:
Code: [Select]
`OPTION PROLOGUE:NONEOPTION EPILOGUE:NONEalign 4compare proc C p1:DWORD, p2:DWORD    mov eax, [esp+4]    mov edx, [esp+8]    mov eax, [eax]    mov edx, [edx]    sub eax, edx    retcompare endpOPTION PROLOGUE:PrologueDefOPTION EPILOGUE:EpilogueDef`
But at least for the Microsoft compilers there is no need to code the procedure in MASM, because you can create a naked function in C that has the same minimal overhead, and that can use exactly the same assembly code. I can’t recall ever trying a naked function with GCC.

Well Microsoft, here’s another nice mess you’ve gotten us into.

#### Gunther

• Member
• Posts: 3585
• Forgive your enemies, but never forget their names
##### Re: masm example
« Reply #5 on: January 13, 2013, 10:42:11 AM »
Hi Michael,

But at least for the Microsoft compilers there is no need to code the procedure in MASM, because you can create a naked function in C that has the same minimal overhead, and that can use exactly the same assembly code. I can’t recall ever trying a naked function with GCC.

thank you for providing the code. With gcc I've experimented with naked functions some years ago. It works, too. I will find the example code on one of my older machines. But your proposal shows the right direction.

Gunther
Get your facts first, and then you can distort them.

#### Vortex

• Member
• Posts: 2337
##### Re: masm example
« Reply #6 on: January 13, 2013, 11:02:38 AM »
Hi Gunther,

Here is a Pelles C example calling the StdOut function from masm32.lib :

Code: [Select]
`#include <stdio.h>extern int __stdcall StdOut(char *);int main(int argc,char *argv[]){ StdOut("StdOut function called from masm32.lib\n"); return 0;}`

#### Gunther

• Member
• Posts: 3585
• Forgive your enemies, but never forget their names
##### Re: masm example
« Reply #7 on: January 13, 2013, 11:41:38 AM »
Thank you for the example, Erol.  :t

Gunther
Get your facts first, and then you can distort them.

#### goofprog

• Guest
##### Re: masm example
« Reply #8 on: September 15, 2013, 07:46:50 PM »
These are cool.  Use a function pointer and assign it.