Q(uick)BASIC Routine: StringLength

Quick View

StringLength

This routine is used in mixed-language programming. A non-BASIC routine uses StringLength to find the number of characters in a BASIC variable-length string

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • length = StringLength(string-descriptor%);
Description/Parameter(s)
string-descriptor% The near address of a string descriptor within a non-BASIC routine.
  • The preceding syntax is for the C language.
  • For C, MASM, Pascal, and FORTRAN examples, see Chapter 13, "Mixed-Language Programming with Far Strings" in the Programmer's Guide.

Usage Notes

  • Calls to the StringLength routine are usually made from a non-BASIC routine.
    Note:If you are not doing mixed-language programming, there is usually no reason to use StringLength. Instead, use LEN to find the length of a variable-length string.
  • As an example, assume that a MASM routine takes a pointer to a string descriptor as a parameter. MASM can find the length of the string data by using the following code:
    • .model
      extrn stringlength: far
      .code
      mov          ax, psdparm           ; offset of descriptor
      push AX
      call stringlength
    The length of the string is returned in AX.
  • For detailed instructions on mixed-language programming with strings, see Chapter 12, "Mixed-Language Programming" and Chapter 13, "Programming with Near Strings" in the Programmer's Guide.
Example

This example uses the StringAssign routine to pass a BASIC string to an assembly-language program. The StringLength routine is used to determine the length of the string.
Note: To use this program, you must compile the following code and link it with the assembled MASM program (shown below in remarks) and the appropriate runtime library (BCL71ENR.LIB).

DEFINT A-Z 'Create a variable-length string. Message1$ = "This string was sent to MASM for printing." 'Pass it to MASM to be printed. CALL PrintMessage1(Message1$) '; *************************PrintMessage1************************ '; This MASM procedure prints a string passed to it by BASIC. ' ' .model medium, basic ;Use same model as BASIC ';Define external (BASIC library) procedures ' extrn StringAddress: far ' extrn StringLength: far ' ' .code ';Define procedure with one word argument 'PrintMessage1 proc uses ds, descriptor ' ' mov ax, descriptor ;Find length of string. ' push ax ' call StringLength ' push ax ;Save length on stack. ' ' mov ax, descriptor ;Go find out the ' push ax ;address of the string ' CALL StringAddress ;data. ' ' mov ds, dx ;Address of string.l. ' mov dx, ax ' mov bx, 01 ;Handle of printer. ' pop cx ;Length of string. ' mov ah, 40h ;Have DOS print it. ' int 21h ' ret 'PrintMessage1 endp ' ' end