Author Topic: question about PEEK and POKE - hello need to know what this is doing  (Read 6908 times)

0 Members and 1 Guest are viewing this topic.

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Hello

I know nothing of peek and poke could some one tell me what this is doing

Badger

Code: QB64: [Select]
  1. FOR il = 1 TO 59
  2.     POKE 5000 + il, il
  3. FOR il = 1 TO 59
  4.     PRINT PEEK(5000 + il);
  5.  
« Last Edit: October 26, 2020, 05:05:28 pm by odin »

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: hello need to know what this is doing
« Reply #1 on: October 26, 2020, 04:27:59 pm »
Hello

This is the code the first post is from .. looking over it again i know what it does but i need to know how and what the peek and poke statements are doing.

Badger

Code: QB64: [Select]
  1. REM A Powerball drawing program by Martin VandenHurk.
  2. a$ = TIME$
  3. PRINT "Drawing..."
  4. FOR l = 1 TO 59
  5.     POKE 5000 + l, l
  6. FOR l = 1 TO 59
  7.     PRINT PEEK(5000 + l);
  8. PRINT "Now drawing powerball numbers..."
  9. ct = 1
  10. here:
  11. x = INT(RND * 59) + 1
  12. IF PEEK(5000 + x) <> 0 THEN r(ct) = x: POKE 5000 + x, 0: GOTO there
  13. GOTO here
  14. there:
  15.  
  16. ct = ct + 1
  17.  
  18. IF ct = 6 THEN GOTO done ELSE GOTO here
  19. done:
  20. PRINT "Here are your 5 Powerball numbers:"
  21. FOR ct = 1 TO 5
  22.     PRINT r(ct);
  23. PRINT "The red Powerball number is:";
  24. PRINT INT(RND * 35) + 1
  25. PRINT "time started  : "; a$
  26. PRINT "time completed: "; TIME$
  27.  
  28.  
  29.  

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: hello need to know what this is doing
« Reply #2 on: October 26, 2020, 04:28:12 pm »
Well, I don't know the addresses it is referring to but PEEK is supposed to read a memory address on the PC and show the value stored there. POKE is used to write a value to a memory address. The old ways don't work too well on modern QB64 (since they overflow a lot) and I have a library here https://www.qb64.org/forum/index.php?topic=3134.msg124049#msg124049 that is able to do the task on Windows 64 and 32 bit.
« Last Edit: October 26, 2020, 04:34:58 pm by SpriggsySpriggs »
Shuwatch!

Offline Kernelpanic

  • Newbie
  • Posts: 94
    • View Profile
Re: hello need to know what this is doing
« Reply #3 on: October 26, 2020, 04:44:49 pm »
Poke is a Adress in RAM:  Poke 5000, 33
Means, Adress: 5000, Value: 33
Peek is:  x = Peek(Adress)

Code: QB64: [Select]
  1.  
  2. POKE 5000, 33
  3.  
  4. x = PEEK(5000)
  5.  

It is like pointer in C.
« Last Edit: October 26, 2020, 04:46:17 pm by Kernelpanic »
Mark Twain
"Als wir das Ziel endgültig aus den Augen verloren hatten, verdoppelten wir unsere Anstrengungen."
„Having lost sight of our goals, we redoubled our efforts.“

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: hello need to know what this is doing
« Reply #4 on: October 26, 2020, 04:50:18 pm »
SpriggsySpriggs


what do these const values mean if i may ask ...

Badger

I dont mean to ask for a lesson but that is what i might be asking LOL



Code: QB64: [Select]
  1.  
  2. CONST PROCESS_VM_READ = &H0010
  3. CONST PROCESS_QUERY_INFORMATION = &H0400
  4. CONST PROCESS_VM_WRITE = &H0020
  5. CONST PROCESS_VM_OPERATION = &H0008
  6. CONST STANDARD_RIGHTS_REQUIRED = &H000F0000
  7. CONST SYNCHRONIZE = &H00100000
  8. CONST PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED OR SYNCHRONIZE OR &HFFFF
  9.  
  10. CONST TH32CS_INHERIT = &H80000000
  11. CONST TH32CS_SNAPHEAPLIST = &H00000001
  12. CONST TH32CS_SNAPMODULE = &H00000008
  13. CONST TH32CS_SNAPMODULE32 = &H00000010
  14. CONST TH32CS_SNAPPROCESS = &H00000002
  15. CONST TH32CS_SNAPTHREAD = &H00000004
  16. CONST TH32CS_SNAPALL = TH32CS_SNAPHEAPLIST OR TH32CS_SNAPMODULE OR TH32CS_SNAPPROCESS OR TH32CS_SNAPTHREAD
  17.  

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: hello need to know what this is doing
« Reply #5 on: October 26, 2020, 05:02:08 pm »
Hello

I think i know what the lottery program is doing with the peeks and pokes

he first pokes the value 1 to 50. In address 5000 1 to 59

then generates a random number by x= int(rnd*59)+1
then in the next line he compares the value of x to that value by using peek to make sure there is no zero or duplicates in his number

Correct ????

Badger

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: hello need to know what this is doing
« Reply #6 on: October 26, 2020, 05:07:46 pm »
what do these const values mean if i may ask ...

@badger The const values are just what is required for running the specific commands so you don't have to memorize those hexadecimal numbers. They correspond to the declarations from the WinAPI. You don't really need to know what they do since they're only used by the functions I made. However, the first block is used for gaining rights to a process. The second block is used for gaining rights for finding the process.

he first pokes the value 1 to 50. In address 5000 1 to 59

Yes, it looks like he is poking those addresses but I was saying that I don't know what those addresses refer to. I was hesitant to run it on my PC since I wasn't sure what it was going to do. I know it is supposed to generate PowerBall numbers. I ran it on Linux 64 bit as well as Windows 10 in 32 and 64 bit. It worked. Everything seems straightforward and you seem to understand what is happening.
« Last Edit: October 26, 2020, 05:10:04 pm by SpriggsySpriggs »
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #7 on: October 27, 2020, 01:37:18 am »
@badger Here is some code I made using the WinAPI code that I mentioned and you've looked at. This does the Powerball draw using that library :) No guarantee that it is exactly the same result or that it will be stable. I got numbers (sometimes) when I ran it. Don't take this as me telling you to use this instead. Your code you posted at first works better for that sort of thing. I was trying to see if I could do it as well. My library is better suited for the stuff like in the example from my post about Peeping Tom. It's good for editing another program's memory or doing peeks and pokes in your program.

Code: QB64: [Select]
  1. $IF 64BIT THEN
  2.     TYPE PROCESSENTRY32
  3.         dwSize AS LONG
  4.         cntUsage AS LONG
  5.         th32ProcessID AS _INTEGER64
  6.         th32DefaultHeapID AS _UNSIGNED _INTEGER64
  7.         th32ModuleID AS LONG
  8.         cntThreads AS LONG
  9.         th32ParentProcessID AS LONG
  10.         pcPriClassBase AS LONG
  11.         dwFlags AS LONG
  12.         szExeFile AS STRING * 260
  13.     END TYPE
  14.     TYPE PROCESSENTRY32
  15.     dwSize AS LONG
  16.     cntUsage AS LONG
  17.     th32ProcessID AS LONG
  18.     th32DefaultHeapID AS _UNSIGNED LONG
  19.     th32ModuleID AS LONG
  20.     cntThreads AS LONG
  21.     th32ParentProcessID AS LONG
  22.     pcPriClassBase AS LONG
  23.     dwFlags AS LONG
  24.     szExeFile AS STRING * 260
  25.     END TYPE
  26.  
  27. CONST PROCESS_VM_READ = &H0010
  28. CONST PROCESS_QUERY_INFORMATION = &H0400
  29. CONST PROCESS_VM_WRITE = &H0020
  30. CONST PROCESS_VM_OPERATION = &H0008
  31. CONST STANDARD_RIGHTS_REQUIRED = &H000F0000
  32. CONST SYNCHRONIZE = &H00100000
  33. CONST PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED OR SYNCHRONIZE OR &HFFFF
  34.  
  35. CONST TH32CS_INHERIT = &H80000000
  36. CONST TH32CS_SNAPHEAPLIST = &H00000001
  37. CONST TH32CS_SNAPMODULE = &H00000008
  38. CONST TH32CS_SNAPMODULE32 = &H00000010
  39. CONST TH32CS_SNAPPROCESS = &H00000002
  40. CONST TH32CS_SNAPTHREAD = &H00000004
  41. CONST TH32CS_SNAPALL = TH32CS_SNAPHEAPLIST OR TH32CS_SNAPMODULE OR TH32CS_SNAPPROCESS OR TH32CS_SNAPTHREAD
  42.  
  43. CONST TRUE = -1
  44. CONST FALSE = 0
  45.  
  46.     FUNCTION CreateToolhelp32Snapshot& (BYVAL dwFlags AS LONG, BYVAL th32ProcessID AS LONG)
  47.     FUNCTION Process32First%% (BYVAL hSnapshot AS LONG, BYVAL lppe AS _OFFSET)
  48.     FUNCTION Process32Next%% (BYVAL hSnapshot AS LONG, BYVAL lppe AS _OFFSET)
  49.     FUNCTION OpenProcess& (BYVAL dwDesiredAccess AS LONG, BYVAL bInheritHandle AS _BYTE, BYVAL dwProcessId AS LONG)
  50.     FUNCTION ReadProcessMemory%% (BYVAL hProcess AS LONG, BYVAL lpBaseAddress AS _OFFSET, BYVAL lpBuffer AS _OFFSET, BYVAL nSize AS LONG, BYVAL lpNumberOfBytesRead AS _OFFSET)
  51.     FUNCTION WriteProcessMemory%% (BYVAL hProcess AS LONG, BYVAL lpBaseAddress AS _OFFSET, BYVAL lpBuffer AS _OFFSET, BYVAL nSize AS LONG, BYVAL lpNumberOfBytesWritten AS _OFFSET)
  52.     FUNCTION CloseHandle%% (BYVAL hObject AS LONG)
  53.  
  54.     FUNCTION strlen& (BYVAL ptr AS _UNSIGNED _OFFSET)
  55.  
  56. exe = MID$(COMMAND$(0), _INSTRREV(COMMAND$(0), "\") + 1)
  57.  
  58. a = TIME$
  59. PRINT "Drawing..."
  60. DIM pokval AS INTEGER
  61. pokval = 5000
  62. FOR l = 1 TO 59
  63.     pok = pokeint(exe, _OFFSET(pokval) + l, l)
  64. FOR l = 1 TO 59
  65.     pok = peekint(exe, _OFFSET(pokval) + l)
  66. PRINT "Now drawing powerball numbers..."
  67. ct = 1
  68.     x = INT(RND * 59) + 1
  69.     IF peekint(exe, _OFFSET(pokval) + x) <> 0 THEN
  70.         REDIM _PRESERVE r(ct) AS INTEGER
  71.         r(ct) = x
  72.     END IF
  73.     pok = pokeint(exe, _OFFSET(pokval) + x, 0)
  74.     ct = ct + 1
  75.  
  76.     IF ct = 6 THEN EXIT DO
  77.  
  78. PRINT "Here are your 5 Powerball numbers:"
  79. FOR ct = 1 TO 5
  80.     PRINT r(ct);
  81. PRINT "The red Powerball number is:";
  82. PRINT INT(RND * 35) + 1
  83. PRINT "time started  : "; a
  84. PRINT "time completed: "; TIME$
 [ You are not allowed to view this attachment ]  
« Last Edit: October 27, 2020, 01:44:52 am by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #8 on: October 27, 2020, 10:36:13 am »
Why PEEK and POKE for random numbers?

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #9 on: October 27, 2020, 02:09:34 pm »
Why PEEK and POKE for random numbers?

It's a very obfuscating way to simply make certain that the values don't appear twice in the same drawing.  After looking over the original code, I went in and made what I thought was some simple Steve Edits(tm)  to bring the code up to more modern programming practices:

Code: QB64: [Select]
  1. REM A Powerball drawing program by Martin VandenHurk.
  2. REM Which Steve finds to be a very goofy way to do Powerball numbers...
  3. REM So this is the modified version, which doesn't use GOTO, or obsolete PEEK and POKE commands.
  4.  
  5.  
  6.  
  7. a$ = TIME$
  8. PRINT "Drawing..."
  9.  
  10. 'FOR l = 1 TO 59
  11. 'POKE 5000 + l, l
  12. 'NEXT
  13. b$ = "11111111111111111111111111111111111111111111111111111111111" 'Steve addition of a 59 length string of all 1's.
  14.  
  15. 'SLEEP 3                             'Why a sleep statement here?  If we're going to time program execution, we don't need it to slow things down
  16. 'FOR l = 1 TO 59                     'For that much, why are we even bothering with these PRINT statements at all?  The CLS below erases them from the screen too fast for us to even register them with our human eyesight...
  17. '    PRINT PEEK(5000 + l);
  18. '    PRINT l;                        'Even if we do print our values to the screen, WTH do we need to PEEK the numbers from 1 to 59?  We just put them in memory with POKE in perfect order.  Why not just print them and be done with it?
  19. 'NEXT
  20. 'PRINT "Now drawing powerball numbers..."
  21. 'ct = 1
  22. 'here:
  23. FOR ct = 1 TO 6 '                      We want 6 numbers , so a FOR loop makes sense here, rather than a GOTO label
  24.     DO '                               A simple DO loop, to get an unique X. If we draw the same number twice, simply redraw until it's not an used number
  25.         x = INT(RND * 59) + 1
  26.         'IF PEEK(5000 + x) <> 0 THEN r(ct) = x: POKE 5000 + x, 0: GOTO there
  27.         'GOTO here
  28.     LOOP UNTIL MID$(b$, x, 1) = "1" '  End of the Steve added DO loop
  29.     r(ct) = x: MID$(b$, x, 1) = "0" '  Basically the same as the code after the THEN which I commented out above, but using a simple string rather than PEEK and POKE to check for used numbers.
  30. 'there: '                              All needlessly complicated GOTO labels and junk, which simply obfuscates the code.  These were all commented out and removed by Steve
  31.  
  32. '   ct = ct + 1
  33.  
  34. '  IF ct = 6 THEN GOTO done ELSE GOTO here
  35. ' done:
  36. 'CLS '                                 No need for a CLS here, or else there's no need for the printing above, as it'll simply dash off the screen too quick to read.
  37. PRINT "Here are your 5 Powerball numbers:"
  38. FOR ct = 1 TO 5
  39.     PRINT r(ct);
  40. PRINT "The red Powerball number is:";
  41. PRINT INT(RND * 35) + 1
  42. PRINT "time started  : "; a$
  43. PRINT "time completed: "; TIME$
  44.  



And, to compare the two, without all the comments in the way, here's the original again:

Code: QB64: [Select]
  1. REM A Powerball drawing program by Martin VandenHurk.
  2. a$ = TIME$
  3. PRINT "Drawing..."
  4. FOR l = 1 TO 59
  5.     POKE 5000 + l, l
  6. FOR l = 1 TO 59
  7.     PRINT PEEK(5000 + l);
  8. PRINT "Now drawing powerball numbers..."
  9. ct = 1
  10. here:
  11. x = INT(RND * 59) + 1
  12. IF PEEK(5000 + x) <> 0 THEN r(ct) = x: POKE 5000 + x, 0: GOTO there
  13. GOTO here
  14. there:
  15.  
  16. ct = ct + 1
  17.  
  18. IF ct = 6 THEN GOTO done ELSE GOTO here
  19. done:
  20. PRINT "Here are your 5 Powerball numbers:"
  21. FOR ct = 1 TO 5
  22.     PRINT r(ct);
  23. PRINT "The red Powerball number is:";
  24. PRINT INT(RND * 35) + 1
  25. PRINT "time started  : "; a$
  26. PRINT "time completed: "; TIME$

And here's the altered version, without the extra junk left in it:

Code: QB64: [Select]
  1. REM A Powerball drawing program by Steve.
  2. a$ = TIME$
  3. PRINT "Drawing..."
  4. b$ = "11111111111111111111111111111111111111111111111111111111111"
  5. FOR ct = 1 TO 5
  6.     DO
  7.         x = INT(RND * 59) + 1
  8.     LOOP UNTIL MID$(b$, x, 1) = "1"
  9.     r(ct) = x: MID$(b$, x, 1) = "0"
  10. PRINT "Here are your 5 Powerball numbers:"
  11. FOR ct = 1 TO 5
  12.     PRINT r(ct);
  13. PRINT "The red Powerball number is:";
  14. PRINT INT(RND * 35) + 1
  15. PRINT "time started  : "; a$
  16. PRINT "time completed: "; TIME$

From a 34 line program with PEEKs and POKEs, labels and GOTOs, to a simple little 21 line program.  This goes from something which has new (or modern) coders scratching their heads and saying, "What the heck does this do?", (such as in the original post), to becoming something simple enough for anyone to basically glance at and figure out what we're doing.

A lot of these old commands are in the language just for backwards compatibility reasons.  They're not something which a respectable, decent modern-day programmer needs in their toolbox.  PEEK and POKE are two commands best simply forgotten about, unless you're having to maintain/convert legacy code from the 90's.  And GOTO.... GOTO is still a need tool for the modern programmer, but it shouldn't be the first tool of choice, when simple FOR-NEXT, DO-LOOP, and WHILE-WEND statements can replace it easily in most cases and make the code much more readable and easier to figure out.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #10 on: October 27, 2020, 05:58:04 pm »
21 lines what are you doing playing around like that?  ;-))
Code: QB64: [Select]
  1. _TITLE "Powerball 1-69 redball 1 to 26" 'b+ 2020-10-27
  2. ' ref: https://www.google.com/search?client=opera&q=powerball+number+range&sourceid=opera&ie=UTF-8&oe=UTF-8
  3. t# = TIMER(.01)
  4. DEFINT A-Z
  5. DIM pb(1 TO 69)
  6. FOR i = 1 TO 69
  7.     pb(i) = i
  8. FOR i = 69 TO 2 STEP -1
  9.     SWAP pb(i), pb(INT(RND * i) + 1)
  10. FOR i = 20 TO 24
  11.     PRINT pb(i);
  12. PRINT "redball:"; INT(RND * 26) + 1; "   time:"; TIMER(.01) - t#
  13.  
« Last Edit: October 27, 2020, 06:29:37 pm by bplus »

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #11 on: October 27, 2020, 06:22:36 pm »
Hello

I did not mean to start a competition rotf but it was fun to watch. I was going to rewrite it using arrays and such on the same principal. If you dont need to peek and poke it is a good idea to not. but i just wanted a little more understanding of what he was doing. both programs are really cute.

Badger

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #12 on: October 27, 2020, 06:30:21 pm »
ha yeah I am still fixing mine, found a glaring error.

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #13 on: October 27, 2020, 06:36:38 pm »
Hey

Repost the code if you would ok .. want to see if i can pick out the differences

Badger

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: question about PEEK and POKE - hello need to know what this is doing
« Reply #14 on: October 27, 2020, 06:50:04 pm »
SpriggsySprigs

i copied your code but get illegal string number conversion on this code. If i rem it out it just does it on the next instance.
 pok = pokeint(exe, _OFFSET(pokval) + l, l)

Badger