3

The Microsoft Visual Studio Pro 2008 C++ makes a great IDE for writing, assembling, and linking pure assembly language programs. While I can build asm programs that link with the Win32 API (for console input and output), I cannot get linked to the C run-time library. Well, actually it links okay, but it gets an r6032 error at run-time - library is not being initialized/loaded correctly error. Can someone please tell me how to set up the IDE (and the code) to call C library functions? Below is an example for calling printf. By the way, I've been using the msvcrt.lib. I've also tried the libcmt.lib.

Thanks in advance.

        .586
        .model flat                             

        extern printf:near

        .data

msgTestClib db 'Hello from the C library.', 0

        .code

main PROC

        push    ebp

        mov ebp, esp

        push offset msgTestClib

        call printf

        add esp, 4

        pop ebp

        ret

main ENDP

        END
4

Okay, I have found the answer. Actually two solutions exist. But first, the reason for the error is that changes were made to VS 2005 and later, changing how assemblies are put together, and that leads to technical information dealing with the underpinnings of VS. The "how to" is much more important here. First I will put up the sample code, and it is basically the same code regardless of which method is used.

The code:

;sh3.asm - test using c run-time library functions

.586
.model flat

EXTRN   _printf:PROC

        .data

msgHello DB 'Hello from C library!', 0ah, 00h

.code

_main PROC

push OFFSET msgHello

call _printf

add esp, 4

ret 0

_main ENDP

END

Okay, method 1:

  1. Start with an empty project; add a source file with .asm extension and type in the code. Make sure you name the entry point of your code with _main. Method two with deal with using a different name.

  2. Next, set the project for using MASM by right-clicking on the project name in the solution explorer and selecting custom build rules; check the Microsoft Macro Assembler box.

  3. Next, go to the project -> properties -> linker -> additional dependencies and type in the C run-time library name. For example, msvcrt.lib for release code, or msvcrtd.lib for debug phase. Important note: do not make any entries for entry point or subsystem.

  4. Assemble your code and run it.

Method 2:

  1. Start with an empty project; add a source file with .asm extension and type in the code. Name the entry point of your code with a label of your choice. For example, start.

  2. Next, set the project for using MASM by right-clicking on the project name in the solution explorer and selecting custom build rules; check the Microsoft Macro Assembler box.

  3. Next, go to the project -> properties -> linker -> additional dependencies and type in the C run-time library name. For example, msvcrt.lib for release code, or msvcrtd.lib for debug phase.

  4. Next, while in the Linker pages, select system and change to console, then go to advanced and in the entry point box type in your entry label, like start for example.

  5. Next, while in the linker pages, select manifest files -> additional manifest dependencies and type in this: type='win32' name='Microsoft.VC90.CRT' version='9.0.21022.8' processorArchitecture='x86' publicKeyToken='1fc8b3b9a1e18e3b' all as one line.

  6. Assemble and run your code.

You might be wondering where a person finds the stuff for the additional dependencies, as this information is most likely VS and Windows version dependent. Create a simple project using method 1, then find its manifest file and read it in a text editor. You will see the dependency line containing the information.

Personally, I find method 1 to be the easiest way to do the job. Being forced to name my entry point as _main is a very small price to pay for having the flexibility in using C run-time library code. It also has the advantages of not having to set the subsystem too. Now, as a special note - If you are not using C run-time libraries then use method 2 and leave out the step for adding additional manifest dependencies; it is not needed!

By the way, I also tested method 1 using calls to the Win32 API and the C run-time library and the code worked fine. In other words you can use both windows api calls and c run-time library calls in the same code.

I hope you find this information helpful in writing assembly code in Windows using Visual Studio. Good Luck and have fun!

~jiangshi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.