0

What is the semantics of langtypes C and STDCALL for .MODEL in MASM assembly?

.MODEL memorymodel [[, langtype]] [[, stackoption]]

https://msdn.microsoft.com/en-us/library/ss9fh0d6.aspx

I notice that I can only call a .asm function from C++ code, if the .MODEL includes the C langtype.

But what does these langtypes mean?

I've tried Googling around, but even thus the above link mentions the possible langtypes, no description is provided.

Can someone clarify the semantics of langtypes and provide a link for proper documentation?

Shuzheng
  • 11,288
  • 20
  • 88
  • 186
  • 1
    I'm not sure why the documentation refers to them as "langtypes". This argument specifies the default *calling convention*, which is a much more easily Googleable term. See: https://en.wikipedia.org/wiki/X86_calling_conventions and https://en.wikibooks.org/wiki/X86_Disassembly/Calling_Conventions, for example. – Cody Gray - on strike Jul 09 '16 at 17:50
  • 1
    I'm very tempted to close this as a duplicate of a general question about stdcall vs cdecl, for example [this one](http://stackoverflow.com/questions/3404372/stdcall-and-cdecl). If there's something you're still confused about after perusing the links, please edit your question to ask about that specifically. – Cody Gray - on strike Jul 09 '16 at 17:52
  • But why does linking fails " cannot find external symbol" just because of a calling convention? Failure should happen at runtime only? – Shuzheng Jul 10 '16 at 05:14
  • 1
    From the documentation you linked to in your question: *"Optional parameter that sets the calling and **naming** conventions for procedures and public symbols."* Calling conventions use name decoration to prevent name clashes. For an exported symbol `foo` the decorated name might be `_foo@16`. When you are trying to call `foo`, the linker fails to find it. – IInspectable Jul 10 '16 at 08:45
  • If you use .model with C language type, the assembler will automatically prefix and suffix names as needed so they can be called from C. Usually this just means an underscore before the name. If you don't specify the C language type, you'll need to prefix (and perhaps suffix) function names in your asm code. – rcgldr Jul 11 '16 at 00:00
  • Thanks @rcgldr - could you provide a link to the source? – Shuzheng Jul 11 '16 at 05:37
  • @NicolasLykkeIversen - On page 34 of this old manual, it states that .model sets naming (language) and calling conventions (stdcall). [masm 6.1 pdf](http://people.sju.edu/~ggrevera/arch/references/MASM61PROGUIDE.pdf) – rcgldr Jul 11 '16 at 07:07
  • Thank you. Are such old manuals still relevant to MASM? Are there no changes made to it? – Shuzheng Jul 11 '16 at 09:06
  • the old manual is still valid for masm 32bit, on 64bit (ml64.exe) you don't need them at all --- if you use .model flat, stdcall you need some steps to call such a function from your C++ code: See: http://stackoverflow.com/a/39257836/6763144 –  Aug 31 '16 at 20:38

0 Answers0