Q(uick)BASIC Function: SADD

Quick View

SADD

A memory function that returns the address of the specified string expression

Worth knowing

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

Syntax
  • SADD(stringvariable)
Description/Parameter(s)
  • stringvariable identifies the string for which you want the memory location

The SADD function returns the address of a string as an offset (near pointer) from the current data segment. The offset is a two-byte integer. SADD is most often used in mixed-language programming.

The argument may be a simple string variable or a single element of a string array. You may not use fixed-length string arguments.

Use this function with care because strings can move in the BASIC string space (storage area) at any time. SADD works only with string variables stored in DGROUP.

Note: Do not add characters to the beginning or end of a string passed using SADD and LEN. Adding characters may produce a run-time error.
Example

The following example uses SADD and LEN to pass a string to a function written in C. The C function returns the ASCII value of a character at a given position in the string.
The C program would be separately compiled and stored in a Quick library or explicitly linked to form an .EXE file. Note that BYVAL is the default for C.

'*** Programming example: SADD function *** ' ' Do not attempt to run this program unless you have already ' separately compiled the C function and placed it in a ' Quick library or linked it to the BASIC program. ' ' Pass a string to a C function using SADD and LEN. DEFINT A-Z ' Declare the function; DECLARE FUNCTION MyAsc CDECL(BYVAL A AS INTEGER, BYVAL B AS INTEGER, _ BYVAL C AS INTEGER) A$="abcdefghijklmnopqrstuvwxyz" PRINT "Enter a character position (1-26). Enter 0 to Quit." DO ' Get a character position. INPUT N ' End if the position is less than zero. IF N<=0 THEN EXIT DO ' Call C function; the function returns the ASCII code of the ' character at position N in A$. AscValue=MyAsc(SADD(A$),LEN(A$),N) PRINT "ASCII value: ";AscValue;"Character: ";CHR$(AscValue) LOOP END /* C function to return the ASCII value of the character at position pos in string c of length len. */ int far myasc(c,len,pos) char near *c; int len, pos; { if(pos>len) return(c[--len]);/* Avoid indexing off end. */ else if (pos<1) return(c[0]);/* Avoid indexing off start. */ else return(c[--pos]);/* pos is good. Return the character at pos-1 because C arrays (strings) are zero-indexed. */ }

Sample Output:

Enter a character position (1-26). Enter -1 to Quit. ? 24 ASCII value: 120 Character: x ? -1
Syntax
  • SADD (stringvariable$)
Description/Parameter(s)
stringvariable$ The string for which you want to find the offset. It can be a simple string variable or a single element of a string array. You cannot use fixed-length string arguments.

SADD returns the address of a string as an offset from the current data segment. It is typically used in mixed-language programming to obtain far addresses before passing far strings to procedures written in other languages. The offset is a 2-byte integer.

Usage Notes

  • SADD can be used with both near and far strings. To obtain the segment address of a far string, use the SSEG function.
  • In previous versions of BASIC, SADD was used only for near strings.

Important

  • Use this function with care because strings can move in the BASIC string space (storage area) at any time.
  • Do not add characters to the beginning or end of a string passed using SADD and LEN. Adding characters may cause BASIC to generate a run-time error.
Example

This example creates a string and then calculates its offset and segment using the SADD and SSEG functions. The information is then passed to a BASIC SUB procedure which mimics the performance of a non-BASIC print routine.

DEFINT A-Z 'Create the string. Text$ = ".... a few well-chosen words" 'Calculate the offset, segment and length of the string. Offset = SADD(Text$) Segment = SSEG(Text$) Length = LEN(Text$) 'Pass these arguments to the print routine. CALL printit(Segment, Offset, Length) END SUB printit (Segment, Offset, Length) CLS 'Set the segment for PEEKing. DEF SEG = Segment FOR i = 0 TO Length - 1 'Get each character from memory, convert to ASCII, and display. PRINT CHR$(PEEK(i + Offset)); NEXT i END SUB