POKE: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "The '''POKE''' statement sets the value of a specified memory address offset. '''QB64 currently has limited access!''' {{PageSyntax}} :: POKE ''segment_offset'', ''offset_value'' * Writes a value to the ''segment_offset'' address in memory. * POKE can only be used to set a value from 0 to 255 (one byte). * A segment should be defined using DEF SEG, if you don't define a segment qbasics ordinary segment will be used. * POKE sends byte values to memory areas. It d...")
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
The '''POKE''' statement sets the value of a specified memory address offset. '''QB64 currently has limited access!'''  
The '''POKE''' statement sets the value of a specified memory address offset. '''QB64 currently has limited access!'''




Line 6: Line 6:




{{PageDescription}}
* Writes a value to the ''segment_offset'' address in memory.
* Writes a value to the ''segment_offset'' address in memory.
* POKE can only be used to set a value from 0 to 255 (one byte).
* POKE can only be used to set a value from 0 to 255 (one byte).
* A segment should be defined using [[DEF SEG]], if you don't define a segment qbasics ordinary segment will be used.
* A segment should be defined using [[DEF SEG]], if you don't define a segment QBasic's ordinary segment will be used.
* POKE sends byte values to memory areas. It does not directly access registers.
* POKE sends byte values to memory areas. It does not directly access registers.
* Important [[SCREEN (statement)|SCREEN]] segments using [[PEEK]] and [[POKE]] include &HB800 (text segment) and &HA000 (graphics segment).
* Important [[SCREEN]] segments using [[PEEK]] and [[POKE]] include &HB800 (text segment) and &HA000 (graphics segment).
* [[DEF SEG]] should always be used to reset the default segment when access to other memory is no longer necessary.
* [[DEF SEG]] should always be used to reset the default segment when access to other memory is no longer necessary.
* POKE is safer to use than [[OUT]] which could damage a PC register.
* POKE is safer to use than [[OUT]] which could damage a PC register.
* '''Warning: DEF SEG, VARSEG , VARPTR, PEEK or POKE access QB64's emulated 16 bit conventional memory block!'''  
* '''Warning: DEF SEG, VARSEG , VARPTR, PEEK or POKE access QB64's emulated 16 bit conventional memory block!'''
: '''It is highly recommended that QB64's [[_MEM]] memory system be used to avoid running out of memory.'''
: '''It is highly recommended that QB64's [[_MEM]] memory system be used to avoid running out of memory.'''




{{PageExamples}}
:''Example 1:'' Turning keyboard Lock and Insert modes on and off.
:''Example 1:'' Turning keyboard Lock and Insert modes on and off.
{{CodeStart}} '' ''
{{CodeStart}}
  DEF SEG = 0
  DEF SEG = 0
  oldsetting% = PEEK(1047)
  oldsetting% = PEEK(1047)
Line 29: Line 31:
{{CodeEnd}}
{{CodeEnd}}
:''Note: Use [[XOR]] instead of [[OR]] above to alternate between on and off modes.''
:''Note: Use [[XOR]] instead of [[OR]] above to alternate between on and off modes.''
{{CodeStart}} '' ''
{{CodeStart}}
  {{Cl|DEF SEG}} = 0
  {{Cl|DEF SEG}} = 0
  oldsetting% = {{Cl|PEEK}}(1047)
  oldsetting% = {{Cl|PEEK}}(1047)
Line 36: Line 38:
  POKE 1047,PEEK(1047) AND 191 ' TURNS OFF CAPS LOCK (191 = 255 - 64)
  POKE 1047,PEEK(1047) AND 191 ' TURNS OFF CAPS LOCK (191 = 255 - 64)
  POKE 1047,PEEK(1047) AND 127 ' TURNS OFF INSERT MODE (127 = 255 - 128)
  POKE 1047,PEEK(1047) AND 127 ' TURNS OFF INSERT MODE (127 = 255 - 128)
  {{Cl|DEF SEG}} '' ''
  {{Cl|DEF SEG}}
{{CodeEnd}}
{{CodeEnd}}
:''Note: Using [[AND]] requires that the bit value is subtracted from 255 to turn off a bit.'' The above examples won't work in NT.
:''Note: Using [[AND]] requires that the bit value is subtracted from 255 to turn off a bit.'' The above examples won't work in NT.
Line 44: Line 46:


''Example 2:'' A small PEEK and POKE fractal program.
''Example 2:'' A small PEEK and POKE fractal program.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|SCREEN (statement)|SCREEN}} 13
{{Cl|SCREEN}} 13
{{Cl|DEF SEG}} = {{Cl|&H}}A000    'set to read screen buffer
{{Cl|DEF SEG}} = {{Cl|&H}}A000    'set to read screen buffer
{{Cl|DO}}
{{Cl|DO}}
Line 53: Line 55:
     {{Cl|_LIMIT}} 25
     {{Cl|_LIMIT}} 25
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} <> ""
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} <> ""
{{Cl|DEF SEG}} '' ''
{{Cl|DEF SEG}}
{{CodeEnd}}  
{{CodeEnd}}




Line 67: Line 69:
     {{Cl|IF}} selection {{Cl|THEN}} selectRow selection, minX, maxX, 0
     {{Cl|IF}} selection {{Cl|THEN}} selectRow selection, minX, maxX, 0
     x = {{Cl|CINT}}({{Cl|_MOUSEX}})
     x = {{Cl|CINT}}({{Cl|_MOUSEX}})
     y = {{Cl|CINT}}({{Cl|_MOUSEY}})  
     y = {{Cl|CINT}}({{Cl|_MOUSEY}})
     {{Cl|IF}} x >= minX {{Cl|AND (boolean)|AND}} x <= maxX {{Cl|AND (boolean)|AND}} y >= minY {{Cl|AND (boolean)|AND}} y <= maxY {{Cl|THEN}}
     {{Cl|IF}} x >= minX {{Cl|AND (boolean)|AND}} x <= maxX {{Cl|AND (boolean)|AND}} y >= minY {{Cl|AND (boolean)|AND}} y <= maxY {{Cl|THEN}}
       selection = y
       selection = y
Line 74: Line 76:
     {{Cl|END IF}}
     {{Cl|END IF}}
     'Highlight any selected row
     'Highlight any selected row
     {{Cl|IF}} selection {{Cl|THEN}} SelectRow selection, minX, maxX, 2  
     {{Cl|IF}} selection {{Cl|THEN}} SelectRow selection, minX, maxX, 2
     {{Cl|IF}} {{Cl|_MOUSEBUTTON}}(1) {{Cl|THEN}} {{Cl|LOCATE}} 1, 2: {{Cl|PRINT}} x, y, selection  
     {{Cl|IF}} {{Cl|_MOUSEBUTTON}}(1) {{Cl|THEN}} {{Cl|LOCATE}} 1, 2: {{Cl|PRINT}} x, y, selection
   {{Cl|END IF}}
   {{Cl|END IF}}
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} <> ""
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|INKEY$}} <> ""
Line 87: Line 89:
   addr& = addr& + 2
   addr& = addr& + 2
{{Cl|NEXT}}
{{Cl|NEXT}}
{{Cl|END SUB}} '' ''
{{Cl|END SUB}}
{{CodeEnd}}
{{CodeEnd}}


 
=== More Examples ===
''See Example:'' [[SelectScreen]] (Screen mode selection)
* [[SelectScreen]]




''See also:''
{{PageSeeAlso}}
* [[DEF SEG]], [[DEF SEG = 0]] {{text|(reference)}}
* [[DEF SEG]], [[DEF SEG = 0]]
* [[PEEK]] {{text|(read memory)}}, [[OUT]] {{text|(write to register)}}
* [[PEEK]], [[OUT]]
* [[VARSEG]], [[VARPTR]]
* [[VARSEG]], [[VARPTR]]
* [[_MEMGET (function)]], [[_MEMPUT]]
* [[_MEMGET (function)]], [[_MEMPUT]]
* [[Scancodes]] {{text|(demo)}}, [[Screen Memory]]  
* [[Scancodes]], [[Screen Memory]]
* [[PEEK and POKE Library]]
* [[PEEK and POKE Library]]




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 22:19, 2 February 2023

The POKE statement sets the value of a specified memory address offset. QB64 currently has limited access!


Syntax

POKE segment_offset, offset_value


Description

  • Writes a value to the segment_offset address in memory.
  • POKE can only be used to set a value from 0 to 255 (one byte).
  • A segment should be defined using DEF SEG, if you don't define a segment QBasic's ordinary segment will be used.
  • POKE sends byte values to memory areas. It does not directly access registers.
  • Important SCREEN segments using PEEK and POKE include &HB800 (text segment) and &HA000 (graphics segment).
  • DEF SEG should always be used to reset the default segment when access to other memory is no longer necessary.
  • POKE is safer to use than OUT which could damage a PC register.
  • Warning: DEF SEG, VARSEG , VARPTR, PEEK or POKE access QB64's emulated 16 bit conventional memory block!
It is highly recommended that QB64's _MEM memory system be used to avoid running out of memory.


Examples

Example 1: Turning keyboard Lock and Insert modes on and off.
 DEF SEG = 0
 oldsetting% = PEEK(1047)
 POKE 1047,PEEK(1047) OR 16 ' ENABLES SCROLL LOCK
 POKE 1047,PEEK(1047) OR 32 ' ENABLES NUMBER LOCK
 POKE 1047,PEEK(1047) OR 64 ' ENABLES CAPS LOCK
 POKE 1047,PEEK(1047) OR 128 ' ENABLES INSERT MODE
 DEF SEG

Note: Use XOR instead of OR above to alternate between on and off modes.
 DEF SEG = 0
 oldsetting% = PEEK(1047)
 POKE 1047,PEEK(1047) AND 239 ' TURNS OFF SCROLL LOCK (239 = 255 - 16)
 POKE 1047,PEEK(1047) AND 223 ' TURNS OFF NUMBER LOCK (223 = 255 - 32)
 POKE 1047,PEEK(1047) AND 191 ' TURNS OFF CAPS LOCK (191 = 255 - 64)
 POKE 1047,PEEK(1047) AND 127 ' TURNS OFF INSERT MODE (127 = 255 - 128)
 DEF SEG
Note: Using AND requires that the bit value is subtracted from 255 to turn off a bit. The above examples won't work in NT.
Warning: The keyboard lights may NOT change so it is a good idea to restore the original settings!


Example 2: A small PEEK and POKE fractal program.

SCREEN 13
DEF SEG = &HA000     'set to read screen buffer
DO
    FOR a& = 0 TO 65535
        POKE a&, PEEK((a& * 2) AND &HFFFF&) + 1
    NEXT
    _LIMIT 25
LOOP UNTIL INKEY$ <> ""
DEF SEG


Example 3: Highlighting a row of text in Screen 0

minX = 20: maxX = 60: minY = 10: maxY = 24
selection = 0 'the screen Y coordinate of the previously highlighted item
FOR i% = 1 TO 25: LOCATE i%, 40: PRINT i%;: NEXT
DO: _LIMIT 100
  IF _MOUSEINPUT THEN
    'Un-highlight any selected row
    IF selection THEN selectRow selection, minX, maxX, 0
    x = CINT(_MOUSEX)
    y = CINT(_MOUSEY)
    IF x >= minX AND x <= maxX AND y >= minY AND y <= maxY THEN
      selection = y
    ELSE
      selection = 0
    END IF
    'Highlight any selected row
    IF selection THEN SelectRow selection, minX, maxX, 2
    IF _MOUSEBUTTON(1) THEN LOCATE 1, 2: PRINT x, y, selection
  END IF
LOOP UNTIL INKEY$ <> ""

SUB SelectRow (y, x1, x2, col)
DEF SEG = &HB800
addr& = (x1 - 1 + (y - 1) * _WIDTH) * 2 + 1
FOR x = x1 TO x2
  oldCol = PEEK(addr&) AND &B10001111   ' Mask foreground color and blink bit
  POKE addr&, oldCol OR ((col AND &B111) * &B10000) ' Apply background color
  addr& = addr& + 2
NEXT
END SUB

More Examples


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link