Microsoft KB Archive/39381

From BetaArchive Wiki
Knowledge Base

Basic Supports MASM 5.10 Update .MODEL and PROC Extensions

Article ID: 39381

Article Last Modified on 11/21/2006

This article was previously published under Q39381


The Microsoft Macro Assembler Version 5.10 includes several new features (not found in MASM Version 5.00 or earlier) that simplify assembly-language routines linked with high-level language programs Two of these features are as follows:

  1. An extension to the .MODEL directive that automatically sets up naming, calling, and return conventions for a given high-level language; for example, .MODEL MEDIUM,Basic
  2. A modification of the PROC directive that handles most of the procedure entry automatically. The PROC directive saves specified registers, defines text macros for passed arguments, and generates stack setup code on entry and stack tear-down code on exit.

These new features are supported by QuickBasic Versions 4.00, 4.00b, and 4.50 and the Microsoft Basic Compiler Versions 6.00 and 6.00b.

Section 5 of the "Microsoft Macro Assembler 5.1: 5.1 Update" manual discusses the new features.


Page 332 of the "Microsoft QuickBasic Version 4.00: Learning and Using QuickBasic" manual provides an example of an assembly-language function called by Basic. The example from Page 332 is modified below to demonstrate the new features in the Microsoft Macro Assembler Version 5.10.

Compile and Link Instructions are as follows:

BC power.bas;
MASM power.asm,powera;
LINK power + powera;

The following is the Basic Program, POWER.BAS, which invokes the assembly language function POWER2:

   DECLARE FUNCTION power2 (x%, y%)
   PRINT power2(3, 5)

The following is POWER2.ASM, an example of using the extended .MODEL and .PROC directives in MASM 5.10:

.model medium,Basic  ;Adds the language option ",Basic"
                     ;PUBLIC directive was removed. The label on
                     ;the PROC directive is now the function name.
Power2 PROC arg1:word, arg2:word  ;parameters added to PROC

   ;Note: PUSH BP and MOV BP,SP are no longer needed.
   ;The arguments are refered to by the names supplied on the
   ;PROC list instead of their offset from BP (Base Pointer):
       mov   bx,arg1
       mov   ax,[bx]
       mov   bx,arg2
       mov   cx,[bx]
       shl   ax,cl
   ;Note, POP BP is no longer needed.
       ret      ;RET is used now, instead of RET n, (where n is
                ;two times the number of passed arguments.)
Power2 endp

For comparison, the following is the equivalent, earlier (MASM Version 5.00) form for POWER2.ASM, taken from Page 332 of "Learning and Using QuickBasic" (for Versions 4.00 and 4.00b, and Basic Compiler Versions 6.00 and 6.00b):

.model medium
       PUBLIC Power2
Power2 PROC
       PUSH  BP
       MOV   BP,SP        ; Set stack framepointer
       mov   bx,[bp+8]    ; Load Arg1 into
       mov   ax,[bx]      ;   AX
       mov   bx,[bp+6]    ; Load Arg2 into
       mov   cx,[bx]      ;   CX
       shl   ax,cl        ; AX = AX * (2 to power of CX)
                          ; Leave return value in AX
       POP   BP           ; Exit sequence -- restore old BP
       ret   4            ; Return and restore 4 bytes.
Power2 endp

Additional query words: QuickBas BasicCom

Keywords: KB39381