Assembly Function Returning Variable-Length String to Basic (64428)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.1

This article was previously published under Q64428

SUMMARY

The two programs below demonstrate how an assembly language function can return a variable-length string to a Microsoft Basic program.

Note: This routine will not work inside the QuickBasic Extended environment (QBX.EXE) or when compiled using the Far Strings option (BC /Fs) in Microsoft Basic Professional Development System (PDS) version 7.00 or 7.10. For information on interlanguage programming with far strings in Microsoft Basic PDS, see Chapter 13, "Mixed Language Programming with Far Strings," in the "Microsoft Basic 7.0: Programmer's Guide" for versions 7.00 and 7.10.

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 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, query in the Microsoft Knowledge Base using the following word:

BAS2MASM

Code Example

The following Basic program is SFUNC.BAS, which displays a variable-length string returned from an assembly language function (QPrint).
   DECLARE FUNCTION QPrint$(slen%)
   CLS
   FOR i% = 1 to 3
      TString$ = QPrint$(i%)  ' i% is the length of the string
      PRINT TSTring$, LEN(TString$)
   NEXT
				
The following assembly language program is ASTR.ASM, which contains the function QPrint. The QPrint function returns a string, and the passed integer parameter (argument) returns the length of the string.
; The following handy .MODEL MEDIUM,Basic directive is found in MASM
; 5.10 but not in earlier versions:
.MODEL MEDIUM, Basic
.DATA
        str      db 10 dup (?)    ; my own string
        mystring dw ?             ; my own descriptor (length)
                 dw ?             ;  (offset)

.CODE
        PUBLIC QPrint
QPrint  PROC
        push bp
        mov bp, sp
        push ds
        push es

        mov bx, [bp+6]      ; get the ptr to the string descriptor.
        mov cx, [bx]

        push ds
        pop es                    ; set es = ds

        mov di, offset dgroup:str ; load the offset into di
        mov al, 'a'               ; load character to fill
        rep stosb                 ; store "a" into the string
        mov cx, [bx]              ; put the length in cx again
        mov bx, offset dgroup:mystring ; put offset of string
                                       ;  descriptor in bx
        mov [bx], cx                   ; length in first two bytes
        mov [bx+2], offset dgroup:str  ; offset into second two bytes
        mov ax, bx                     ; load address of descriptor
                                       ;   into ax
        pop es
        pop ds
        pop bp

        ret 2
QPrint  ENDP
        END
				
To demonstrate these programs from an .EXE program, compile and link as follows:

BC SFUNC.BAS;
MASM ASTR.ASM;
LINK SFUNC ASTR;

SFUNC.EXE produces the following output:

a
aa
aaa


Modification Type:MinorLast Reviewed:1/9/2003
Keywords:KB64428