Author Topic: Calling C functions from masm  (Read 584 times)

svzs

  • Regular Member
  • *
  • Posts: 4
Calling C functions from masm
« on: June 28, 2021, 03:11:43 AM »
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.
« Last Edit: June 28, 2021, 05:16:17 PM by svzs »

TouEnMasm

  • Member
  • *****
  • Posts: 1805
    • EditMasm
Re: Calling C functions from masm
« Reply #1 on: June 28, 2021, 03:53:04 PM »

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

  • Regular Member
  • *
  • Posts: 4
Re: Calling C functions from masm
« Reply #2 on: June 28, 2021, 05:14:42 PM »
The question is what is Your Question ?

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

Vortex

  • Member
  • *****
  • Posts: 2583
Re: Calling C functions from masm
« Reply #3 on: June 28, 2021, 05:32:47 PM »
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

  • Regular Member
  • *
  • Posts: 4
Re: Calling C functions from masm
« Reply #4 on: June 28, 2021, 06:13:55 PM »
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:
Code: [Select]
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

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: Calling C functions from masm
« Reply #5 on: June 28, 2021, 06:15:09 PM »
How 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--

  • Administrator
  • Member
  • ******
  • Posts: 8497
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Calling C functions from masm
« Reply #6 on: June 28, 2021, 06:19:34 PM »
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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

svzs

  • Regular Member
  • *
  • Posts: 4
Re: Calling C functions from masm
« Reply #7 on: June 28, 2021, 06:31:06 PM »
jj2007 and hutch--, thank you! This makes things clearer. Namely, I know where to dig for more info now ;)