Microsoft KB Archive/49383

From BetaArchive Wiki
Knowledge Base


Example Passing Numerics from Basic to MASM by Near Reference

Article ID: 49383

Article Last Modified on 11/21/2006

This article was previously published under Q49383

SUMMARY

The two programs shown below demonstrate how a Microsoft Basic program can pass standard numeric types to assembly language routines.

This information about interlanguage calling applies to QuickBasic versions 4.00, 4.00b, and 4.50 for MS-DOS, to Microsoft Basic Compiler versions 6.00 and 6.00b for MS-DOS and MS OS/2, and to Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10 for MS-DOS and MS OS/2.

MORE INFORMATION

For more information about passing other types of parameters between Basic and MASM, search in the Microsoft Knowledge Base using the following word:

BAS2MASM


Code Example

The following Basic program is BNUMNEAR.BAS, which passes standard numeric types (by near reference) to assembly language routines:

 DECLARE SUB Numint(i%)
   DECLARE SUB Numlong(l&)
   DECLARE SUB Numsng(s!)
   DECLARE SUB Numdbl(d#)
   i% = 2                   ' Initialize values
   l& = 4
   s! = 3.4
   d# = 5.6
   CLS
   PRINT "         BEFORE","AFTER"
   PRINT "Integer: ";i%,,
   CALL Numint(i%)
   PRINT i%

   PRINT "Long   : ";HEX$(l&),,
   CALL Numlong(l&)
   PRINT HEX$(l&)

   PRINT "Single : ";s!,
   CALL Numsng(s!)
   PRINT s!

   PRINT USING "Double : ##.####            ";d#,
   CALL Numdbl(d#)
   PRINT USING "##.####"; d#

   END
                

The following assembler program is ANUMNEAR.ASM, which accepts standard numerics by near reference and alters their values:

; The following handy .MODEL MEDIUM,Basic directive is found in MASM
; 5.10 but not in earlier versions:
.MODEL MEDIUM, Basic
.CODE
        PUBLIC Numint, Numlong, Numsng, Numdbl
Numint  PROC
        push bp
        mov bp, sp        ; set stack frame
        mov bx, [bp+6]
        mov ax, [bx]   ; get integer
        shl ax, 1         ; multiply by 2
        mov [bx], ax   ; put new value back
        pop bp
        ret 2
Numint  ENDP

Numlong PROC
        push bp
        mov bp, sp        ; set stack frame
        mov bx, [bp+6]
        mov cx, [bx]   ; get long
        mov ax, [bx+2] ; switch high and low words
        mov [bx+2], cx ; put new value back
        mov [bx], ax
        pop bp
        ret 2
Numlong ENDP

Numsng  PROC
        push bp
        mov bp, sp        ; set stack frame
        mov bx, [bp+6]
        or byte ptr [bx+2],80h   ; Set sign bit
        pop bp
        ret 2
Numsng  ENDP

Numdbl  PROC
        push bp
        mov bp, sp         ; set stack frame
        mov bx, [bp+6]
        or byte ptr [bx+6],80h  ; Set sign bit
        pop bp
        ret 2
Numdbl  ENDP

        END
                

To demonstrate these programs from an .EXE program, compile and link as follows:

BC /O BNUMNEAR.BAS;
MASM ANUMNEAR.ASM;
LINK BNUMNEAR ANUMNEAR;


BNUMNEAR.EXE produces the following output:

           BEFORE     AFTER
   Integer:  2          4
   Long   :  4          40000
   Single :  3.4       -3.4
   Double :  5.6000    -5.6000
                


Additional query words: QuickBas BasicCom

Keywords: KB49383