Q(uick)BASIC Statement: POKE

Quick View

POKE

A memory statement that writes a byte into a memory location

Worth knowing

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

Syntax
  • PEEK(address)
  • POKE address,byte%
Description/Parameter(s)
address A byte position relative to the current segment address set by DEF SEG; a value in the range 0 through 65,535.
byte% A byte value to write to the specified memory location; a value in the range 0 through 255.
Example
DEF SEG = 0 Status% = PEEK(&H417) 'Read keyboard status. POKE &H417, (Status% XOR &H40) 'Change Caps Lock state, bit 6.

See also:

Syntax
  • POKE address,byte
Description/Parameter(s)

The expression address is a value that represents the address of the memory location; address must be in the range 0-65,535.

The expression byte is the data byte to be written; it is an integer value in the range 0-255.

The address is treated as the offset from the current default segment (as set by the DEF SEG statement).

If the argument is a single- or double-precision floating-point value or a long integer, it is converted to a two-byte integer.

The complementary function to POKE is PEEK.

Warning

  • Use POKE carefully. If used incorrectly, it can cause BASIC or the operating system to fail.
Example

See the DEF SEG statement programming example , which uses the POKE

Syntax
  • POKE address, byte%
Description/Parameter(s)
address An offset into the memory segment specified by the current default segment (as set by the DEF SEG statement). The value of address is between 0 and 65,535, inclusive.
byte% The data byte to be written into the memory location. The byte% argument is an integer value between 0 and 255, inclusive.

Usage Notes

  • If the argument is a single- or double-precision floating-point value or a long integer, it is converted to a 2-byte integer.
  • The POKE statement complements the PEEK function.
  • Before using POKE to directly manipulate data stored in far memory, use the DEF SEG statement and the SSEG function to set the current segment address. For example:

  • ' Set the current segment address to the address of a$.
  • DEF SEG = SSEG(a$)
  • ' Determine the string's location within the segment.
  • Offset1 = SADD(a$)
  • ' Write the byte stored in
  • POKE Offset1, PEEK(Offset2)
Note: BASIC moves string locations during run time. Therefore, the DEF SEG statement must be executed immediately before using the POKE statement.

Warning

  • Use POKE carefully. If used incorrectly, it can cause BASIC or the operating system to fail.

POKE and Expanded Memory Arrays

  • Do not use POKE to manipulate expanded memory arrays. 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 POKE to manipulate 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

  • Any address referred to by POKE must be open for writing. If POKE refers to a memory address for which your process does not have write permission, the operating system may generate a protection exception, or BASIC may generate the error message, "Permission denied."
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. DEF SEG = 0 'Set Caps Lock off (turn off bit 6 of &H0417). POKE &H417, PEEK(&H417) AND &HBF DEF SEG END SUB SUB CapsOn STATIC 'Turn Caps Lock on. 'Set segment to low memory. DEF SEG = 0 'Set Caps Lock on (turn on bit 6 of &H0417). POKE &H417, PEEK(&H417) OR &H40 'Restore segment. DEF SEG 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