PEEK and POKE Library: Difference between revisions
Jump to navigation
Jump to search
Code by Michael Calkins
Note: These functions and statements use a variable name reference pointer in memory instead of DEF SEG.
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
(Created page with "This header allows you to peek and poke bytes, words, and dwords in QB64. The functions seem to work okay when used with signed variables also. For example, I am able to assign the return of peekw~% to an INTEGER without problem. Regards, Michael Calkins Create ''PeekPoke.h'' text file in the QB64 folder: {{TextStart}} /* peek and poke for bytes, words, and dwords in qb64 public domain, sept 2011, michael calkins http://www.network54.com/Forum/648955/message/131595...") |
No edit summary |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 7: | Line 7: | ||
peek and poke for bytes, words, and dwords in qb64 | peek and poke for bytes, words, and dwords in qb64 | ||
public domain, sept 2011, michael calkins | public domain, sept 2011, michael calkins | ||
https://www.tapatalk.com/groups/qbasic/viewtopic.php?p=188921#p188921 | |||
<nowiki>*/ | <nowiki>*/ | ||
Line 28: | Line 28: | ||
{*(unsigned long int *)p = n;}</nowiki> | {*(unsigned long int *)p = n;}</nowiki> | ||
{{TextEnd}} | {{TextEnd}} | ||
{{ | {{Small|Code by Michael Calkins}} | ||
''PeekPoke.BAS'' | ''PeekPoke.BAS'' | ||
Line 36: | Line 36: | ||
' public domain, sept 2011, michael calkins | ' public domain, sept 2011, michael calkins | ||
{{Cl|DECLARE | {{Cl|DECLARE LIBRARY|DECLARE CUSTOMTYPE LIBRARY}} "peekpoke" | ||
{{Cl|FUNCTION}} peekb~%% ({{Cl|BYVAL}} p {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_OFFSET}}) 'Byte | {{Cl|FUNCTION}} peekb~%% ({{Cl|BYVAL}} p {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_OFFSET}}) 'Byte | ||
{{Cl|FUNCTION}} peekw~% ({{Cl|BYVAL}} p {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_OFFSET}}) 'Integer(Word) | {{Cl|FUNCTION}} peekw~% ({{Cl|BYVAL}} p {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_OFFSET}}) 'Integer(Word) | ||
{{Cl|FUNCTION}} peekd~& ({{Cl|BYVAL}} p {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_OFFSET}}) 'Long(Dword) | {{Cl|FUNCTION}} peekd~& ({{Cl|BYVAL}} p {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_OFFSET}}) 'Long(Dword) | ||
{{Cl|SUB}} pokeb ({{Cl|BYVAL}} p {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_OFFSET}}, {{Cl|BYVAL}} n {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_BYTE}}) | {{Cl|SUB}} pokeb ({{Cl|BYVAL}} p {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_OFFSET}}, {{Cl|BYVAL}} n {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_BYTE}}) | ||
{{Cl|SUB}} pokew ({{Cl|BYVAL}} p {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_OFFSET}}, {{Cl|BYVAL}} n {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|INTEGER}}) | {{Cl|SUB}} pokew ({{Cl|BYVAL}} p {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_OFFSET}}, {{Cl|BYVAL}} n {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|INTEGER}}) | ||
{{Cl|SUB}} poked ({{Cl|BYVAL}} p {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_OFFSET}}, {{Cl|BYVAL}} n {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|LONG}}) | {{Cl|SUB}} poked ({{Cl|BYVAL}} p {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|_OFFSET}}, {{Cl|BYVAL}} n {{Cl|AS}} {{Cl|_UNSIGNED}} {{Cl|LONG}}) | ||
Line 72: | Line 72: | ||
{{Cl|PRINT}} "0x" + {{Cl|HEX$}}(peekd~&(ptr + 9)) | {{Cl|PRINT}} "0x" + {{Cl|HEX$}}(peekd~&(ptr + 9)) | ||
{{Cl|END}} | {{Cl|END}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
<center>'''Note: These functions and statements use a variable name reference pointer in memory instead of [[DEF SEG]].'''</center> | <center>'''Note: These functions and statements use a variable name reference pointer in memory instead of [[DEF SEG]].'''</center> | ||
{{PageSeeAlso}} | |||
* [[DECLARE LIBRARY]] | * [[DECLARE LIBRARY]] | ||
* [[PEEK]], [[POKE]] | * [[PEEK]], [[POKE]] |
Latest revision as of 23:01, 27 February 2024
This header allows you to peek and poke bytes, words, and dwords in QB64. The functions seem to work okay when used with signed variables also. For example, I am able to assign the return of peekw~% to an INTEGER without problem. Regards, Michael Calkins
Create PeekPoke.h text file in the QB64 folder:
/* peek and poke for bytes, words, and dwords in qb64 public domain, sept 2011, michael calkins https://www.tapatalk.com/groups/qbasic/viewtopic.php?p=188921#p188921 */ unsigned char peekb(void * p) {return *(unsigned char *)p;} unsigned short int peekw(void * p) {return *(unsigned short int *)p;} unsigned long int peekd(void * p) {return *(unsigned long int *)p;} void pokeb(void * p, unsigned char n) {*(unsigned char *)p = n;} void pokew(void * p, unsigned short int n) {*(unsigned short int *)p = n;} void poked(void * p, unsigned long int n) {*(unsigned long int *)p = n;} |
PeekPoke.BAS
' peek and poke for bytes, words, and dwords in qb64 ' requires peekpoke.h ' public domain, sept 2011, michael calkins DECLARE CUSTOMTYPE LIBRARY "peekpoke" FUNCTION peekb~%% (BYVAL p AS _UNSIGNED _OFFSET) 'Byte FUNCTION peekw~% (BYVAL p AS _UNSIGNED _OFFSET) 'Integer(Word) FUNCTION peekd~& (BYVAL p AS _UNSIGNED _OFFSET) 'Long(Dword) SUB pokeb (BYVAL p AS _UNSIGNED _OFFSET, BYVAL n AS _UNSIGNED _BYTE) SUB pokew (BYVAL p AS _UNSIGNED _OFFSET, BYVAL n AS _UNSIGNED INTEGER) SUB poked (BYVAL p AS _UNSIGNED _OFFSET, BYVAL n AS _UNSIGNED LONG) END DECLARE ' examples: DIM buffer AS STRING * 16 DIM ptr AS _UNSIGNED _OFFSET buffer = "abcd---- i-ptrs" ptr = _OFFSET(buffer) PRINT buffer pokeb ptr + &HA, &H3 PRINT buffer pokew ptr + 5, &H3FA8 PRINT buffer poked ptr + 4, &HDBB2B1B0 PRINT buffer poked ptr, CVL("QB64") poked ptr + 4, &H2020011A PRINT buffer PRINT "0x" + HEX$(peekb~%%(ptr + 1)) PRINT "0x" + HEX$(peekw~%(ptr + 2)) PRINT "0x" + HEX$(peekd~&(ptr + 9)) END |
See also