News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

calling c functions from dos

Started by allynm, April 15, 2011, 02:05:16 AM

Previous topic - Next topic

allynm

Hello everyone,

I've looked on the forum to an answer to the following question and found nothing.  Perhaps I've stated the question improperly.  If so, my apologies.

Anyway, here's the question.  If I want to call a c function such as printf, etc. in my dos program, how do I do it?  Can I do it?  I've played around with crt_printf and not had any success. 

These functions are easily called in 32 bit MASM, of course, but they must also be callable from 16 bit code too. 

I think...

Thanks,
Mark Allyn

MichaelW

eschew obfuscation

allynm

Hello MichaelW,

Thanks for responding and providing some help.  In the code you sent you seem to be linking against a 16 bit c library, if I read your .bat file correctly.  Is there a corresponding 16 bit c library as part of MASM32?  If not, can you suggest where to obtain one?

Second question raised by your response concerns /coff versus /omf files.  Out of naivete I have been assuming that I must use /omf switch to get 16 bit code.  You don't do this in the .bat file.  Am I wrong about /omf as a requirement for 16 bit code?
Perhaps I only need /omf when generating a .com?

Thanks for your help.

Regards,
Mark Allyn

dedndave

he uses the 16-bit linker   :U
COFF nor OMF apply to 16-bit files - they are MZ .EXE or .COM
as for getting the 16-bit files - i am guessing an old C-compiler

sinsi

Earlier versions of ML output OMF .obj files by default, these are the only ones the 16-bit linker recognises.
Later versions output COFF .obj files by default, so using the appropriate switch won't hurt any.
Even ml10 will output a proper 16-bit object file. Backwards compatibility, all the way to DOS1.

Dave, COFF is restricted to 32/64-bit object files, 16-bit must be OMF for the linker to know.
Light travels faster than sound, that's why some people seem bright until you hear them.

FORTRANS

Hi Mark,

   A C library should come with a C compiler.  If you do not
already have a compiler, you can get one from the Open
Watcom project or Borland's C compiler was released as a
free download.  And there are a number of free compilers
as well, but they may not have the same knowledge base
of users.  Using Google, I see Digital Mars and EMX support
DOS as well.

Regards,

Steve N.

allynm

Hello MichaelW, Dave, Sinsi, and Steve N.

The MASM 6.1 Programmers Guide is very unclear about what switches need to be set under what circumstances, near as I can tell.  From other forum discussions in which Sinsi made contributions, I had picked up the notion that no matter what .MODEL was being specified, in order to get link16 to function and produce EITHER a .com or .exe I had to specify a /omf switch.  This conclusion seemed to be consistent with the other conclusion I had drawn that  /COFF files were required in order to get the PE header stuff correct.  And probably other stuff too.  

I broke down and followed Dave's suggestion and bought a used copy of Borland C++ v. 5.02 for 49 bucks.  I can only hope that 1.  My wife will forgive me, and 2.  it works.

Regards,
Mark Allyn

dedndave

well - i didn't suggest borland - lol

i was thinking an older free ms compiler, actually
if i dig out my old stuff, i have msc version 6 someplace
a good google session would probably have yielded the results you needed
and - before you go spending money, i would have suggested 32-bit code

as for OMF - yes - i forgot that is the OBJ format

redskull

Strange women, lying in ponds, distributing swords, is no basis for a system of government

allynm

Hello everyone, especially MichaelW,

I copied the code that MichaelW directed me to (see above response from MichaelW).  I copied verbatim and attempted to assemble it using his command with exactly the same \c switch.  Unfortunately, I get error messages from ML.  First, it reports that in his line 36 there is an unresolved symbol: DGROUP.  Then there are three symbol type conflicts all involving the printf command.  Any idea what might be going on? 

Regards,
Mark Allyn

dedndave

see if you can get an "empty" program to assemble
        .MODEL  Small

        .DATA
;
;
;
        .CODE
        ASSUME  DS:DGROUP

_main   PROC    FAR

        mov     ax,@DATA       ;@DATA is an alias for DGROUP
        mov     ds,ax
;
;
;
        mov     ax,4C00h
        int     21h

_main   ENDP

        END     _main


EDIT - added .DATA section   :P

MichaelW

Although I have multiple C/C++ compilers from the DOS era, I used Turbo C++ 2.01 because it was (and apparently still is here, but now you must register to download it). The unfortunate thing about Turbo C++ 2.01 CRT is that the printf function does not support floating-point numbers.
eschew obfuscation

allynm

Hello Dedndave,

I did as you suggested and got back "undefined symbol : DGROUP" at lines 8 and 12.  Thanks for your help and also thanks as well to MichaelW.

I copied your code exactly and use ml /c dedndaveccall.asm  as the command.

Regards,
Mark Allyn

allynm

Further to Dedndave:

An addendum to my previous post:  If I assemble with ml /c /omf /Fl dedndaveccall.asm the assembler does its thing without complaint.  If I then do link16 and create an .exe file, the .exe runs.

I thought you'd find this interessin...

Regards,
Mark

dedndave

here is the batch file i use for 16-bit EXE's (a16.bat)
@echo off
if "x%1"=="x" goto ascusage
if exist %1.asm goto ascasm
:ascusage
echo Usage: asc asmfile
echo "asmfile" = asmfile.asm
goto batchexit
:ascasm
if exist %1.obj del %1.obj
c:\masm32\bin\ml /c /Fl %1.asm >c:\masm32\bin\asmbl.txt
if errorlevel 1 goto showtxt
if exist %1.exe del %1.exe
c:\masm32\bin\Link563 %1.obj; >>c:\masm32\bin\asmbl.txt
:showtxt
if exist %1.obj del %1.obj
type c:\masm32\bin\asmbl.txt
:batchexit
dir %1.*


the masm version is 6.15, although 6.14 and others should work
the /Fl switch creates a listing, and is optional
Link563.exe may be named Link16.exe - if so, modify the batch file accordingly
the batch file is in C:\masm32\bin, which is in the PATH environment variable

i added some test code to the program to display some text