News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

masm example

Started by Gunther, January 13, 2013, 07:48:33 AM

Previous topic - Next topic

Gunther

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
You have to know the facts before you can distort them.

dedndave

i seem to recall a thread in the old forum about "c-callable" routines

qWord

That is very simple if you use PROC/ENDP
include \masm32\include\masm32rt.inc
.code
; double foo(double x); // assuming __cdecl
foo proc c x:REAL8

fld x
fmul st,st
ret

foo endp
end

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

Thank you qWord, that helped really.  :t

Gunther
You have to know the facts before you can distort them.

MichaelW

A minimal-overhead (32-bit integer) compare procedure for the CRT qsort function:

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

align 4
compare proc C p1:DWORD, p2:DWORD
    mov eax, [esp+4]
    mov edx, [esp+8]
    mov eax, [eax]
    mov edx, [edx]
    sub eax, edx
    ret
compare endp

OPTION PROLOGUE:PrologueDef
OPTION 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

Hi Michael,

Quote from: MichaelW on January 13, 2013, 09:05:10 AM
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
You have to know the facts before you can distort them.

Vortex

Hi Gunther,

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

#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

Thank you for the example, Erol.  :t

Gunther
You have to know the facts before you can distort them.

goofprog

These are cool.  Use a function pointer and assign it.