Microsoft KB Archive/49391

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 09:19, 21 July 2020 by X010 (talk | contribs) (Text replacement - ">" to ">")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Base


Example of Passing Array of Basic String Descriptors to MASM

Article ID: 49391

Article Last Modified on 8/16/2005



APPLIES TO

  • Microsoft QuickBasic 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBasic 4.5 for MS-DOS
  • Microsoft BASIC Professional Development System 7.0
  • Microsoft BASIC Professional Development System 7.1
  • Microsoft BASIC Compiler 6.0
  • Microsoft BASIC Compiler 6.0b



This article was previously published under Q49391

SUMMARY

The two programs shown below demonstrate how a Microsoft Basic program can pass an array of string descriptors to assembly language.

For Microsoft Basic PDS versions 7.00 or 7.10, this example works only with near strings. If using far strings (BC /Fs or in QBX.EXE), you must use SSEG and SADD to gain access to the location of strings.

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 BSTR.BAS, which gets an array of strings from the user and calls an assembly language program that capitalizes the strings:

' This program demonstrates passing an array of strings
' to an assembly language routine. The assembly language
' routine then receives the address of the array and
' interprets the array as an array of string descriptors.
' It then uses the descriptors to get the length and address
' of the strings. It uses these two values to capitalize all of
' the lowercase alphabetic characters in any of the strings, and
' to skip all others.
' It is very important to pass the assembly routine the number
' of elements in the array.

OPTION BASE 0
DECLARE SUB UpCaseArray (BYVAL ArrayAddress%, arraylen%)
' BYVAL is necessary because we want to pass the VALUE of
' the address, not a pointer to the address.
DIM Num%, Array1$(20)
CLS

WHILE NOT a$ = "quit"
   INPUT "Enter a string ('quit' to end): ", a$
   Array1$(Num%) = a$
   Num% = Num% + 1
WEND

CALL UpCaseArray(VARPTR(Array1$(0)), Num%)
CLS
FOR i% = 0 TO (Num% - 1)
   PRINT Array1$(i%)
NEXT
END
                

The following program is ASTR.ASM. It accepts an array of Basic string descriptors. ASTR.ASM goes through each of the strings in the array and capitalizes all of the letters.

; The following handy .MODEL MEDIUM,Basic directive is found in MASM
; 5.10 but not in earlier versions:
.MODEL MEDIUM,Basic
.CODE
        PUBLIC UpCaseArray
UpCaseArray PROC FAR
        push bp
        mov  bp,sp
        push di
        mov bx,[bp+6]    ; Argument #2: Number of array elements.
        mov cx,[bx]      ; Get the actual number of array elements.
        jcxz EndOutLoop  ; If the array has 0 elements, then quit.
        mov bx,[bp+8]    ; Argument #1: Which is a pointer to an
                         ; array of descriptors.
OutLoop:                 ; CX is the outer-OutLoop counter.
        push cx          ; Save the outer loop counter.
        mov cx,[bx]      ; Get the first 2 bytes of the current
                         ; descriptor which is the string length.
        jcxz EndInLoop   ; If zero length, end the inner loop.
        mov di,[bx+2]    ; The second 2 bytes is the address.
                         ; DI = pointer to current string.
InLoop:                  ; Check if the char needs to be capitalized.
        cmp byte ptr [di],'a'  ; Is it < a ?
        jb I1                  ; If so, then move to the next char.
        cmp byte ptr [di],'z'  ; Is is > z ?
        ja I1                  ; If so, then move on to the next char.
        and byte ptr [di],05Fh ; Make uppercase. Mask -> (0101 1111).
I1:     inc di                 ; Move on to next character in the
                               ;    string.
        loop InLoop            ; Do it for all characters
                               ;    (until CX = 0).
                               ; Note: 'loop' decrements CX.
EndInLoop:
        add bx,4               ; Move on to next descriptor.
        pop cx                 ; Restore the outer loop counter.
        loop OutLoop           ; Do for all descriptors
                               ;    (until CX = 0).
EndOutLoop:
        pop di
        pop bp
        ret 4
UpCaseArray ENDP
        END
                

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

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

BSTRF.EXE produces the following output:

   Enter a string ('quit' to end): First String
   Enter a string ('quit' to end): Second String
   Enter a string ('quit' to end): quit

   FIRST STRING
   SECOND STRING
                


Additional query words: QuickBas BasicCom

Keywords: KB49391