News:

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

Main Menu

Calling C functions from masm

Started by svzs, June 28, 2021, 03:11:43 AM

Previous topic - Next topic

svzs

Greetings,

Yet another computer architecture student here following a textbook with examples set up for Visual Studio.

One example calls functions from C and masm files provided among the textbook resources that are supposed to be imported into Visual Studio somehow. I'm not sure how to properly compile those as libraries and what to make of the rest of the stuff in the same directory like framework.rc  :undecided: .

Code (example.asm) Select
; Example assembly language program -- adds two numbers
; Author:  R. Detmer
; Date:    1/2008

.586
.MODEL FLAT

INCLUDE \masm32\detmer\io.h            ; header file for input/output

; ... snipped for brevity



Code (io.h) Select
; IO.H -- header file for I/O macros (listing suppressed)
.NOLIST      ; turn off listing

; 32-bit version with windows I/O
; must be used with project framework defining showOutput and getInput
; R. Detmer   October 2007
.586
EXTRN _getInput:NEAR32, _showOutput:NEAR32, atodproc:NEAR32, dtoaproc:NEAR32, wtoaproc:NEAR32, atowproc:NEAR32

; ... snipped


_getInput and _showOutput are defined in framework.c; the rest in io.asm. I attached the VS textbook resources zip just in case as well.

Any help is much appreciated  :smiley:


Edit: remove fluff.

TouEnMasm


The question is what is Your Question ?
It's a Visual Studio project,let's it compile it for you and you get the attached.
I had used VS2019
Fa is a musical note to play with CL

svzs

Quote from: TouEnMasm on June 28, 2021, 03:53:04 PM
The question is what is Your Question ?

How do I compile and link everything using the tools provided by masm32?

Vortex

Hi svzs,

You need the Visual Studio compiler to process the C source code. The Masm32 package provides ml.exe, the Microsoft Macro assembler. ( ml.exe )

svzs

I see. I was hoping there is a way to compile C in the command line but that's not important.
So once it's compiled, how do I link it with the masm stuff?

I assume it goes something like:
ml /c /coff io.asm
ml /c /coff example.asm
link /SUBSYSTEM:CONSOLE example.obj

But how is io.h made aware of the C functions and routines made PUBLIC by io.asm? I'm not sure what supply to link.

jj2007

Quote from: svzs on June 28, 2021, 05:14:42 PMHow do I compile and link everything using the tools provided by masm32?

The MASM tools, mainly \Masm32\bin\ml.exe and \Masm32\bin\link.exe, produce the Assembly part. There are two options:

1. You produce a DLL, and then use (in C) LoadLibrary & GetProcAddress, as with any other *.dll file
2. You make a static library, and let the VS linker do the job as with any other *.lib file

Use forum search to find out which commandline options you need. Here is a page to get you started with the Masm32 stuff.

P.S.: Try link -lib /SUBSYSTEM:CONSOLE example.obj

hutch--

You can use both C modules in MASM and MASM module in C. What you must do is prototype the functions so that the other language can understand how and what to call. In 32 bit MASM you can specify either STDCALL as is used in most API functions or you can specify a C calling convention if you choose to run code like that.

You will need to look up the documentation for the C compiler on how to write a prototype for a MASM function. Just make sure they match up, a C prototype for a module written with a C calling convention or a STDCALL prototype for a module written using the STDCALL convention.

The main difference is how the stack is balanced, with STDCALL the procedure being called balances the stack where a C call balances that stack at the caller end.

svzs

jj2007 and hutch--, thank you! This makes things clearer. Namely, I know where to dig for more info now ;)