Author Topic: masm example  (Read 15532 times)

Gunther

  • Member
  • *****
  • Posts: 4198
  • 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
You have to know the facts before you can distort them.

dedndave

  • Member
  • *****
  • Posts: 8828
  • Still using Abacus 2.0
    • DednDave
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: 1475
  • The base type of a type is the type itself
    • SmplMath macros
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 __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

  • Member
  • *****
  • Posts: 4198
  • 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
You have to know the facts before you can distort them.

MichaelW

  • Global Moderator
  • Member
  • *****
  • Posts: 1196
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: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

  • Member
  • *****
  • Posts: 4198
  • 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
You have to know the facts before you can distort them.

Vortex

  • Member
  • *****
  • Posts: 2794
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: 4198
  • 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
You have to know the facts before 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.