home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 211 / 211.d81 / t.bits < prev    next >
Text File  |  2022-08-26  |  6KB  |  234 lines

  1. u
  2.    BITS: That's My BASIC Program?!
  3.  
  4.            by Scott E. Resh
  5.  
  6.  
  7.     This month we're going to decipher
  8. a BASIC program using an ML monitor.
  9. Prepare to fire-up your favorite
  10. handy-dandy ML monitor, we're on our
  11. way.
  12.  
  13.     On a normal C64 (did you see that
  14. word -- NORMAL?), a BASIC program
  15. starts at $0801. There is a big
  16. difference between LISTing a program
  17. and doing a "memory dump" of the same
  18. program from an ML monitor.
  19.  
  20.     Before we dive into memory dumps,
  21. let me say a word or two about TOKENS.
  22. A token is a 1-byte representation of
  23. a BASIC command or function. Suppose
  24. our BASIC program does a LOT of string
  25. manipulation. For example, suppose the
  26. function, RIGHT$, is used 300 times
  27. (it could happen) in our program. The
  28. word RIGHT$ is six characters long. If
  29. the actual word RIGHT$ appeared in
  30. memory 300 times, it would require
  31. 1,800 bytes! Just a minute though --
  32. let's suppose we replace every
  33. occurrence of RIGHT$ with ONE specific
  34. byte (you guessed it, a TOKEN!). This
  35. would mean only 300 bytes are
  36. necessary instead of 1,800. Now we're
  37. getting somewhere!
  38.  
  39.     Remember, RIGHT$ isn't the only
  40. command/function that has a token. ALL
  41. commands and functions have tokens.
  42. This means that when you do a memory
  43. dump of your program, you won't see
  44. PRINT, REM, GOTO, etc... Instead, you
  45. will see the tokens for those
  46. commands.
  47.  
  48.     On this same side of LOADSTAR
  49. there is a demo program, BITS. (I'm so
  50. creative!) Load this huge program into
  51. BASIC. You can LIST and RUN it, but
  52. please don't modify it, yet...
  53.  
  54.     Notice the address of the SYS
  55. command. What a coincidence!
  56.  
  57.  
  58.     This just happens to be at the END
  59. of the BASIC program. Have you ever
  60. LOADed a BASIC program only to find a
  61. SYS(2063) or SYS2061? Same principle.
  62.  
  63.     Now both your monitor and my
  64. little demo must be in memory. Do a
  65. memory dump of locations $082B
  66. through $083A.
  67.  
  68.     You should see something like
  69. this:
  70.  
  71.  082B: 37 08 78 00 9E 20 20 32
  72.  0833: 31 36 30 00 3D 08 82 00
  73.  
  74. This is equivalent to BASIC's --
  75.  
  76.     120 SYS  2160
  77.  
  78.     What do those bytes mean? Let's
  79. start at $082B. This pair of bytes is
  80. a POINTER to the next line. Here the
  81. next line is at $0837. The next pair
  82. of bytes is the LINE NUMBER. In our
  83. case, it's 120. Lo-byte = 120 (in hex
  84. $78) Hi-byte = 0.
  85.  
  86.  
  87.     The next byte MUST BE a token or
  88. variable assignment (i.e. A=2 or
  89. B$="HI!"). For us the byte is $9E, or
  90. the token for the... take a guess...
  91. the SYS command! The next 2 bytes are
  92. spaces. What always comes after a SYS
  93. command? (I feel like I'm hosting a
  94. game show, "You've won a trip to
  95. Hawaii!") Right again, the ADDRESS for
  96. the SYS command. Wasn't the address
  97. 2160? Let me fetch my trusty little
  98. HEX<->ASCII conversion chart. Hmmm...
  99. $32->"2", $31->"1", $36->"6", (is it
  100. me or is there a pattern emerging
  101. here?) and $30->"0". Aha -- "2160"! We
  102. found the address! (Just call me Scott
  103. "Indy" Resh).
  104.  
  105.  
  106.     The next byte is $00. Every line
  107. of a BASIC programs ALWAYS ends with
  108. a $00. In computerese, that's
  109. "null-terminated". (Remember that if
  110. you ever hope to be a GEOPROGRAMMER)
  111.  
  112.     On to the next byte. I'm curious,
  113. what's the address of the next byte?
  114. Let's see here... 0833, 0834, 0835,
  115. 0836, 0837... that's it! Wait a
  116. minute, I've seen that number before!
  117. (deja-vu?) The FIRST pair of bytes we
  118. looked at pointed to 0837. We traced
  119. our way to the beginning of the next
  120. line.
  121.  
  122.     By the way, try replacing the $9E
  123. at $082F with a $8F or a $99.
  124.  
  125.  
  126.     Fender asked me to say a word or
  127. two about UNNEWing a BASIC program
  128. and the line numbers of a BASIC
  129. program.
  130.  
  131.     UNNEWing is the process of
  132. recovering an accidentally NEWed (NOT
  133. nude) BASIC program. Hopefully, this
  134. technique will save a few gray hairs.
  135. Don't forget, your trustworthy monitor
  136. must be in memory first. Enter a few
  137. lines of BASIC. Now, enter NEW --
  138. that's right, NEW. What NEW does is
  139. POKE two zeroes into $0801 and $0802
  140. and updates three zero page pointers.
  141. This effectively ends your program
  142. before it starts.
  143.  
  144.      Invoke your monitor. Use your
  145. monitor's HUNT command to find the
  146. first occurrence of three zeroes after
  147. $0805. In my case, I enter:
  148.  
  149.   H 0805 9FFF 00 00 00  <RETURN>
  150.  
  151.     Are you asking yourself "Hey
  152. Scott, why $0805? Why not $0801?" I'm
  153. glad you caught that. Both $0801 and
  154. $0802 contain $00. Locations $0803 and
  155. $0804 contain the line number of the
  156. first line of your BASIC program. It's
  157. possible that your first line number
  158. could be 0. To play it safe, we are
  159. going to ignore these four bytes for
  160. now.
  161.  
  162.  
  163.     Write down the first number that
  164. is displayed by your HUNT command.
  165. Add THREE to that number. POKE the
  166. LO-byte into locations $2D, $2F, and
  167. $31. POKE the HI-byte into locations
  168. $2E, $30, and $32.
  169.  
  170.     Time to use the HUNT command
  171. again.  Enter:
  172.  
  173.    H 0805 9FFF 00   <RETURN>
  174.  
  175.     That's right, we want to find the
  176. first occurrence of ONE zero all by
  177. itself. Add ONE to the first number
  178. that was displayed. POKE the LO-byte
  179. of the result into $0801 and the
  180. HI-byte into $0802.
  181.  
  182.     Exit your monitor. You should be
  183. back in BASIC.  LIST your program.
  184. There's your program!
  185.  
  186.  
  187.     Now, about line numbers. There
  188. is a "hackerish" thing you can do to
  189. protect your BASIC program. By
  190. protect, I mean that you can create a
  191. line in your program that keeps your
  192. program from being edited. (Please
  193. note that any program submitted to us
  194. that uses this technique will almost
  195. IMMEDIATELY be rejected.)
  196.  
  197.     Commodore's BASIC 2.0 will not
  198. allow a BASIC line number greater than
  199. 63999. Suppose your BASIC program is
  200. 200 lines long and you want to protect
  201. it. Simple, POKE 2051,0 and POKE
  202. 2052,250. LIST your program. Notice
  203. that your first line is now line
  204. number 64000! Whoa! How'd that happen?
  205.  
  206.     Locations 2051 and 2052 contain
  207. the LO-byte and HI-byte, respectively,
  208. of the first line number of a BASIC
  209. program.
  210.  
  211.      250*256 + 0 = 64000  ($FA00)
  212.  
  213.  
  214.  
  215.     Why don't you try using POKE again
  216. to change the line number from 64000
  217. to 64010?
  218.  
  219.     I'd like to thank Len Thomas for
  220. his thoughtful and considerate
  221. suggestion for this month's BITS
  222. column. ("What do those %&#! numbers
  223. mean!?") We had a lot of fun talking
  224. about hex numbers and such.
  225.  
  226.     If anyone, I mean ANYONE, out
  227. there would like to see something
  228. pertaining to machine language
  229. explained/performed in BITS, write to
  230. us or call.
  231.  
  232.  SER
  233.  
  234.  
  235.