Chapter 20 Mixed-Language Programming

Mixed-language programming allows you to combine the unique strengths of Microsoft Basic, C, FORTRAN, and Pascal with your assembly-language routines. Any one of these languages can call MASM routines, and you can call any of these languages from within MASM routines. This makes virtually all of the routines from extensive high-level-language libraries available to a mixed- language program.

MASM 6.0 has a number of new features that make the interface in assembly-language programs similar to the interface in high-level-language programs. For example, you can now use the INVOKE directive to call high-level-language procedures, and the assembler handles the argument-passing details for you. You can also use H2INC to translate C header files to MASM include files (see Chapter 16).

The new mixed-language features do not make the older methods of defining mixed-language interfaces obsolete. In most cases mixed-language programs written with previous versions of MASM will assemble and link correctly under MASM 6.0. (See Appendix A for more information.)

This chapter explains how to write assembly routines that can be called from high-level-language modules and how to call high-level language routines from MASM. It assumes that you have a basic understanding of the languages you wish to combine and that you already know how to write, compile, and link multiple-module programs with these languages.

This chapter is restricted to MASM's interface with C, Basic, FORTRAN, and Pascal; it does not cover mixed-language programming between high-level languages. The focus in this chapter is the Microsoft versions of C, Basic, FORTRAN, Pascal, and QuickPascal, but the same principles apply to other languages and compilers. The material in Section 7.3 on writing procedures in MASM and in Chapter 8 on multiple-module programming explains many of the techniques used in this chapter.

Section 20.1 looks at naming and calling conventions, and Section 20.2 provides a template for writing the MASM procedure. Specific implementations of this convention in C, Basic, FORTRAN, and Pascal are described in Section 20.3. These language-specific sections also provide details on how the language manages various data structures so that your MASM programs are compatible with the data from the high-level language. This chapter also contains examples of MASM procedures called from C, FORTRAN, Basic, Pascal, and QuickPascal.