string lesson

 BBS: Inland Empire Archive
Date: 05-30-92 (22:07)             Number: 168
From: QUINN TYLER JACKSON          Refer#: NONE
  To: ALL                           Recvd: NO  
Subj: string lesson                  Conf: (2) Quik_Bas
' This short program demonstrates how QuickBASIC deals with String Variables
' in memory.  VARPTR points to a four byte string descriptor which is in
' the format: LL LH AL AH, or LengthLow LengthHigh AddressLow AddressHigh.
' With this information in hand, the intermediate programmer can perform
' string operations directly upon descriptors, thus giving him the power
' to deal with pointers, rather than with the strings themselves.  For
' example, by swapping the 4 descriptor bytes of two string variables, one
' has performed a SWAP function without actually having to move the elements
' inside the string.  If the strings are long, say 5000 bytes each, a total
' of 8 elements are swapped instead of 10000, which is faster by any
' programmer's standards!  I'll leave the mechanics of this up to the
' members of this echo!

' Cheers,  Quinn Tyler Jackson

' STRDESC.BAS v1.0 for release to public domain: May 1992

CLS
DO

INPUT "Please input a string: ", our.string$


' Be aware that in a larger, multi-segmented program, or in programs compiled
' with the /AH option, it is safest to also keep track of the VARSEG, since
' it may not be the same as the DGROUP!

Descriptor.Address = VARPTR(our.string$)

StrOne$ = CHR$(PEEK(Descriptor.Address))
StrTwo$ = CHR$(PEEK(Descriptor.Address + 1))
AddOne$ = CHR$(PEEK(Descriptor.Address + 2))
AddTwo$ = CHR$(PEEK(Descriptor.Address + 3))

' Convert these LB HB values into decimal format for humans
' Here, I have used a trick using CVI rather than the more lengthy
' algebraic convesion algorithm.  It is much simpler this way!

String.Length = CVI(StrOne$ + StrTwo$)
String.Address = CVI(AddOne$ + AddTwo$)


PRINT "String Length: "; String.Length
PRINT "String HEX Offset: "; HEX$(String.Address)
PRINT
PRINT "The string itself: '";

' This loop reads the string from the memory addresses pointed to
' by bytes AL and AH in the descriptor....

FOR q = 0 TO String.Length - 1
        PRINT CHR$(PEEK(q + String.Address));
NEXT q
PRINT "'"

PRINT : PRINT : PRINT : PRINT "Any key to continue, Q to quit..."
SLEEP
LOOP UNTIL UCASE$(INKEY$) = "Q"




--- Maximus/2 2.01wb
 * Origin: The Nibble's Roost, Richmond BC Canada 604-244-8009 (1:153/918)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET