The MASM Forum

General => The Campus => Topic started by: favq on June 06, 2015, 12:54:00 AM

Title: Linking to the C Runtime Library
Post by: favq on June 06, 2015, 12:54:00 AM
Hello,

I downloaded and installed MASM (version 6.14.8444, from here: http://masm32.com/masmdl.htm (http://masm32.com/masmdl.htm)); I installed it at c:\masm32, and I added the directory c:\masm32\bin to my PATH variable. I have the following MASM code (as you can see, it's a simple Hello World example), in the file "helloworld.asm":


.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib

.data

msg db "Hello world.", 0
cpt db "Caption", 0

.code

main:

invoke MessageBox, NULL, addr msg, addr cpt, MB_OK

invoke ExitProcess, NULL

ret 0

public main

end


Notice that I didn't define any entry point in that code. When I try to assemble and link it using the following commands:

ml /c /coff helloworld.asm
link /subsystem:console helloworld.obj


I get the following linker error:

LINK : error LNK2001: unresolved external symbol _mainCRTStartup

(if I try to use "/subsystem:windows" instead of "/subsystem:console", I get a similar error, but the unresolved external symbol is "_WinMainCRTStartup".)

However, if I replace the name "main" with "mainCRTStartup" in the code above, or define "main" as the entry point for the linker (by ending my code with "end main" or by giving the switch "/entry:main" to the linker), it works. So far, so good.

My problem is: out of curiosity, I'd like to link my "helloworld.obj" to the C Runtime Library (CRT).

Further explanation: Please correct me if I'm wrong, but, from what I've found online, the CRT is supposed to provide a "mainCRTStartup" symbol (or "WinMainCRTStartup", if I link with the "windows" subsystem), which refers to an initialization code that does the work of calling "main" (or "WinMain"). So, as far as my limited understanding goes, if I link my executable to the CRT, it will provide a "mainCRTStartup" routine for me (which will be the entry point for my executable), and that would jump to my "main" label, so that I'd only need to have a label called "main" at my "helloworld" module.

So, my question is: how can I link to the CRT, so that it provides the "mainCRTStartup" entry point for me? I've tried to link to "c:\masm32\lib\msvcrt.lib", but I got the same results.
Title: Re: Linking to the C Runtime Library
Post by: dedndave on June 06, 2015, 01:14:45 AM
the last line is "end"
you want to reference the entry point...

    end     main

no need for "ret 0" or "public main"
Title: Re: Linking to the C Runtime Library
Post by: dedndave on June 06, 2015, 01:19:03 AM
i would also use ".486", rather than ".386"
it allows more addressing modes

        .486
        .model  flat, stdcall
        option  casemap:none

        include     \masm32\include\windows.inc
        include     \masm32\include\kernel32.inc
        include     \masm32\include\user32.inc
        includelib  \masm32\lib\kernel32.lib
        includelib  \masm32\lib\user32.lib

        .data

msg db "Hello world.", 0
cpt db "Caption", 0

        .code

main:

    invoke MessageBox, NULL, addr msg, addr cpt, MB_OK
    invoke ExitProcess, NULL

end main
Title: Re: Linking to the C Runtime Library
Post by: dedndave on June 06, 2015, 01:21:38 AM
you can also use masm32rt.inc
it adds the preamble, including many commonly used inc's and lib's

as a matter of personal preference, i also made "main" a PROC

        include     \masm32\include\masm32rt.inc

        .data

msg db "Hello world.", 0
cpt db "Caption", 0

        .code

main    PROC

    invoke MessageBox, NULL, addr msg, addr cpt, MB_OK
    invoke ExitProcess, NULL

main    ENDP

        end     main
Title: Re: Linking to the C Runtime Library
Post by: jj2007 on June 06, 2015, 01:55:13 AM
Quote from: favq on June 06, 2015, 12:54:00 AM
Hello,

I downloaded and installed MASM (version 6.14.8444, from here: http://masm32.com/masmdl.htm (http://masm32.com/masmdl.htm)); I installed it at c:\masm32, and I added the directory c:\masm32\bin to my PATH variable.

Welcome to the forum :icon14:

0. Follow Dave's advice. He knows what he is doing ;-)

1. Forget the PATH and other environment variables. Instead, use \masm32\<somepath> where necessary. This will make sure that other people can reuse your code.

2. In assembler, you will never need the mainCRTStartup - and it's nowhere defined in \Masm32\include

3. Here is a template for console apps:
include \masm32\include\masm32rt.inc
.code
start: MsgBox 0, "Hello World", "Masm32 is great;", MB_OK
exit
end start
Title: Re: Linking to the C Runtime Library
Post by: dedndave on June 06, 2015, 03:35:11 AM
i have my masm32\bin folder in the path
it is helpful because my command prompt can find the batch files from any folder on the same drive
(same drive is a masm32 package requirement)

also - Jochen's little program will assemble and run as either a CONSOLE or a WINDOWS app   :t
it uses the MsgBox and exit macros
you may examine the macros in the file, \masm32\macros\macros.asm
that way, you know what the actual code is
Title: Re: Linking to the C Runtime Library
Post by: Vortex on June 06, 2015, 04:42:43 AM
Hi favq,

Here is a quick example using the MS VC dynamic run-time libary :

include     \masm32\include\masm32rt.inc

.data

str1        db 'What is your name?',13,10,0
str2        db '%s',0
str3        db 13,10,'Hello %s , nice to meet you.',0

.data?

buffer      db 64 dup(?)

.code

start:

    invoke  crt_printf,ADDR str1
    invoke  crt_scanf,ADDR str2,ADDR buffer
    invoke  crt_printf,ADDR str3,ADDR buffer
    invoke  ExitProcess,0

END start
Title: Re: Linking to the C Runtime Library
Post by: Gunther on June 07, 2015, 02:18:29 AM
Hi favq,

you've received a lot of help by Dave, Jochen and Erol. Please follow those advices. And welcome to thev forum.

Gunther