Q(uick)BASIC Statement: DEF SEG

Quick View

DEF SEG

a memory statement that sets the current segment address for a subsequent PEEK function or BLOAD, BSAVE, CALL ABSOLUTE, or POKE statement

Worth knowing

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

Syntax
  • DEF SEG [=address]
Description/Parameter(s)
address A segment address used by BLOAD, BSAVE, CALL ABSOLUTE, PEEK, or POKE; a value in the range 0 through 65,535. If address is omitted, DEF SEG resets the current segment address to the default data segment.
Example
DEF SEG = 0 Status% = PEEK(&H417) 'Read keyboard status. POKE &H417, (Status% XOR &H40) 'Change Caps Lock state, bit 6.
Syntax
  • DEF SEG [=address]
Description/Parameter(s)

For BLOAD, BSAVE, CALL ABSOLUTE, PEEKG, and POKE, address is used as the segment. The address is a numeric expression returning an unsigned integer in the range 0-65,535. A value outside this range produces the error message "Illegal function call." The previous segment is retained if an error occurs. If you omit address, the BASIC data segment is used.

Be sure to separate DEF and SEG with a space. Otherwise, BASIC interprets the statement to mean "assign a value to the variable DEFSEG."

Differences from BASICA

  • In QuickBASIC, the CALL and CALLS statements do not use the segment address set by DEF SEG.
Example

DEFSG_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the DEF SEG statement. To look at the program in the View window and, optionally, to run it, load DEFSG_EX.BAS using the File menu's Open Program command.
The program uses DEF SEG, PEEK, and POKE to turn the CAPSLOCK key on and off.
Note: The program contains hardware-specific instructions. It works correctly on IBM PC, XT, and AT computers.

Syntax
  • DEF SEG [=address]
Description/Parameter(s)

Usage Notes

  • The address is a numeric expression with an unsigned integer value between 0 and 65,535. DEF SEG sets the current segment to this value. If you use a value outside this range, BASIC generates the error message, "Illegal function call." The previous segment is retained if an error occurs.
  • To reset the current segment to the default data segment (DGROUP), use DEF SEG without any argument.
  • Be sure to separate DEF and SEG with a space. Otherwise, BASIC interprets the statement to mean "assign a value to the variable DEFSEG."
  • To set the current segment address to the address of data stored in far memory, you can use DEF SEG with the SSEG or VARSEG functions (SSEG returns the current segment address of a string; VARSEG returns the current segment address of numeric data). For example, this statement sets the current address for a far string named a$:

  • DEF SEG = SSEG(a$)

DEF SEG and Expanded-Memory Arrays

  • Do not use DEF SEG to set the segment of an expanded-memory array. If you start QBX with the /Ea switch, any of these arrays may be stored in expanded memory:
    • Numeric arrays < 16K in size
    • Fixed-length string arrays < 16K in size
    • User-defined-type arrays < 16K in size
  • If you want to use DEF SEG to set the segment of an array, first start QBX without the /Ea switch. (Without the /Ea switch, no arrays are stored in expanded memory.)
  • For more information on using expanded memory, see Using Expanded Memory .

Programming with OS/2 Protected Mode

  • When using DEF SEG in OS/2 protected mode, any DEF SEG must refer only to a valid selector. The DEF SEG statement itself does not generate any memory references using the selector, nor does it attempt to validate the selector. If a misdirected DEF SEG statement causes your program to refer to an illegal memory address, the operating system may generate a protection exception, or BASIC may generate the error message, "Permission denied." The default DEF SEG segment always constitutes a valid memory reference. Use caution when altering this reference in protected mode.

Differences from BASICA

  • In this version of BASIC, the CALL and CALLS statements do not use the segment address set by DEF SEG.
Example

This example uses the DEF SEG statement, the PEEK function, and POKE statement to turn the Caps Lock key on and off. The program uses the LINE INPUT statement to prompt the user to enter a string, demonstrating that the Caps Lock key is on.
Note: This program contains hardware-specific instructions. It works correctly on IBM PC, XT, and AT computers.

DECLARE SUB CapsOn () DECLARE SUB CapsOff () DECLARE SUB PrntMsg (R%, C%, M$) CLS CapsOn PrntMsg 24, 1, "<Caps Lock On>" LOCATE 12, 10 LINE INPUT "Enter a string (all characters are caps): ", S$ CapsOff PrntMsg 24, 1, " " PrntMsg 25, 1, "Press any key to continue..." DO WHILE INKEY$ = "": LOOP CLS END SUB CapsOff STATIC 'Turn Caps Lock off. <b>DEF SEG</b> = 0 'Set Caps Lock off (turn off bit 6 of &H0417). POKE &H417, PEEK(&H417) AND &HBF <b>DEF SEG</b> END SUB SUB CapsOn STATIC 'Turn Caps Lock on. 'Set segment to low memory. <b>DEF SEG</b> = 0 'Set Caps Lock on (turn on bit 6 of &H0417). POKE &H417, PEEK(&H417) OR &H40 'Restore segment. <b>DEF SEG</b> END SUB SUB PrntMsg (Row%, Col%, Message$) STATIC 'Print message at Row%, Col% without changing cursor. 'Save cursor position. CurRow% = CSRLIN: CurCol% = POS(0) LOCATE Row%, Col%: PRINT Message$; 'Restore cursor. LOCATE CurRow%, CurCol% END SUB