I found this in "Programmer's Guide", but worked not.

books says just
"Assemble with ML's /Cx switch to preserve the case of nonlocal names"

??:confused: :confused:

.MODEL small, C ; step 1: declare C conventions
EXTERN _acrtused:abs ; step 2: bring in C startup

printf PROTO NEAR, ; step 3: prototype
pstring:NEAR PTR BYTE, ; external C
num1:WORD, num2:VARARG ; routines

format BYTE '%i %i', 13, 0


main PROC PUBLIC ; step 4: C startup calls here

invoke printf, offset format, ax, bx

END ; step 5: no label on END


Posted on 2002-07-15 08:18:59 by muzidowa
a "ret" is missing after the printf. perhaps deleted by bAZik?
Posted on 2002-07-15 17:57:02 by japheth
Nope, I was carefull and just added the tags :)
Posted on 2002-07-15 18:07:32 by bazik
Looks like old dos .com code to me.
Posted on 2002-07-15 18:45:34 by Roy Cline
C doesn't require a ret on the main proc
Posted on 2002-07-15 19:00:46 by Joshua

You did not provide other relevant information. How does you code break? Is it a linking problem? Or, is it a run-away process problem?

If it is a linking problem, the only thing I can say is that you need to study your C library to find out what are required to be linked.

If it is a run-away process, the answer is simple. japheth already mentioned it. Put ret.


I don't know what C compiler you are using, but it is clear that your compiler is quite unique in that your compiler does not use any start-up code and use main() as the machine level entry point. Although my experience is limited to 5 vendors, none of them does what you said. I will not talk about the C standard, because this is not the place for C. But, I'm just curious. What compiler are you using? How does your compiler handle the two standard arguments to main()? How does your compiler return the exit code from your C program to the OS?
Posted on 2002-07-15 19:45:29 by Starless
Note: If he meant the C calling convention then this post will not apply else...

He's using void main(). Even if you use void main() it will still return 0.

The last part of the code will be the same on both C-Style Coding.

xor eax, eax

void main()


is the same as

int main(void)
return 0;
You app will crash if you don't have a ret. Of course, you can always jmp instead of ret. ;)
Posted on 2002-07-15 21:16:43 by stryker

You do know what void means in C, don't you? :)
If a compiler decides to return 0 when it sees illegal void main(), then that's specific to the compiler. I mean, supporting non-standard code is purely implementation specific. You cannot say void main() is the same as int main() returning 0 without refering to a specific compiler.

Well, I think I should shut up about C. :grin: I can see people frowning at me, talking about a HLL in an asm forum. ;) (duck)
Posted on 2002-07-16 02:11:12 by Starless
In any type of traditional function call (including the void type) there is a return. You might not see it in the C code but it's there. Without it your program would run off into the abyss and most likely crash. ;)
Posted on 2002-07-16 03:00:30 by iblis
Well, I'm using VC 6. Even if it's compiler specific, I doubt that any other compiler out there would return something else other than 0 on a voided function. Can anyone test this on BCC ... and decompile the code?

In C, to return 0 on the main function means success, to state that the function has successfully executed it's code. Other return values has a different meaning. If you want your function to return a specific value other than 0(or even if it's 0) to state something happened, you wouldn't use a voided function in the first place. Anyway, the point of my thread above is the same as was stated by iblis.

Also, void main() isn't even the standard. ;) You'll see a lot of discussion about this on the NG comp.lang.c . Never used it, never has, never will. :tongue:

P.S. they'll flame you if you use this...
Posted on 2002-07-16 03:15:14 by stryker
The code is copied from the MASM Programers Reference.
Note the Model is "small". The code was written in about
1985, so I suggest you start up VC 1.5. The linker needed
is an 16bit one. Note, also the code is intended to be 16bit.
Posted on 2002-07-16 09:17:58 by Roy Cline
Oneday, my friend said couldn't use any C Run-time library in
masm like "printf".

Reading this Programmer's Guide, thought found something,
but nothing happeded.

i have no idea using 16 bit code, since not used.

Then, Is there any way to call C Run-time library?
Posted on 2002-07-16 09:56:06 by muzidowa

with "extern _acrtused:abs" you DO include the crt. Thats the reason why you dont need to define startup code and no "ExitProcess" (win32) or "mov ax,4c00h int21h" (win16). By including the crt you can call printf.


void main() {

will return an "undefined" value in eax (may be 0 by chance of course).
Posted on 2002-07-16 10:11:09 by japheth
Posted on 2002-07-16 10:24:19 by stryker
I modified like this, and assembled

ml /Cx /c /coff ccode.asm
link /subsystem:console ccode.obj libc.lib

occured "unresolved external symbol __acrtused",
"unresolved external symbol _main"

how shoul i link them?

.MODEL flat, C ; step 1: declare C conventions
EXTERN _acrtused:abs ; step 2: bring in C startup

printf PROTO NEAR, ; step 3: prototype
pstring:NEAR PTR BYTE, ; external C
num1:WORD, num2:VARARG ; routines

format BYTE '%i %i', 13, 0


invoke printf, offset format, ax, bx

END start ; step 5: no label on END
Posted on 2002-07-16 10:29:17 by muzidowa
muzidowa, you are a beginner and should read docs (including this thread). THere are many errors in your prog.

your error messages tell:

"unresolved external symbol __acrtused": this line was needed to ensure 16-bit crt is included. Now you have changed to 32-bit and there this symbol isnt defined in libc.lib.

"unresolved external symbol _main": the crt startup code wants to call your code and assumes it defines a symbol main or WinMain (for GUI apps). the leading underscore in the error message is name decoration.

So you should code:

.MODEL flat, stdcall ; <---- better use stdcall here
;EXTERN _acrtused:abs ; <---- not needed here

printf PROTO near c pstring:PTR BYTE, args:VARARG


format BYTE '%d %d', 10, 0


main proc c ; <--- call convention c needed now
invoke printf, offset format, eax, ebx
main endp
END ; <--- no label
Posted on 2002-07-17 03:47:57 by japheth
If your intention is to create a 32 bit windows app in asm then why not just use asm. If you want to create a 32 bit windows app in c but will still need asm for some functions then consider using inline asm in your c code.
Posted on 2002-07-17 04:32:56 by Kudos
thanks, japheth

works well.

but, i couldn't understand why linker wants "_main", if i omitted
"main proc c ... main endp".
is this because of "libc.lib"? is this libs wants to link with "_main"?

what i want is, not call "main proc ..." but just call "printf"
in usual apps.

something like this form...

.MODEL flat, stdcall

printf PROTO near c pstring:PTR BYTE, args:VARARG


format BYTE '%d %d', 10, 0


invoke printf, offset format, eax, ebx

END start

Posted on 2002-07-17 05:55:11 by muzidowa