home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Loadstar 211
/
211.d81
/
t.bits
< prev
next >
Wrap
Text File
|
2022-08-26
|
6KB
|
234 lines
u
BITS: That's My BASIC Program?!
by Scott E. Resh
This month we're going to decipher
a BASIC program using an ML monitor.
Prepare to fire-up your favorite
handy-dandy ML monitor, we're on our
way.
On a normal C64 (did you see that
word -- NORMAL?), a BASIC program
starts at $0801. There is a big
difference between LISTing a program
and doing a "memory dump" of the same
program from an ML monitor.
Before we dive into memory dumps,
let me say a word or two about TOKENS.
A token is a 1-byte representation of
a BASIC command or function. Suppose
our BASIC program does a LOT of string
manipulation. For example, suppose the
function, RIGHT$, is used 300 times
(it could happen) in our program. The
word RIGHT$ is six characters long. If
the actual word RIGHT$ appeared in
memory 300 times, it would require
1,800 bytes! Just a minute though --
let's suppose we replace every
occurrence of RIGHT$ with ONE specific
byte (you guessed it, a TOKEN!). This
would mean only 300 bytes are
necessary instead of 1,800. Now we're
getting somewhere!
Remember, RIGHT$ isn't the only
command/function that has a token. ALL
commands and functions have tokens.
This means that when you do a memory
dump of your program, you won't see
PRINT, REM, GOTO, etc... Instead, you
will see the tokens for those
commands.
On this same side of LOADSTAR
there is a demo program, BITS. (I'm so
creative!) Load this huge program into
BASIC. You can LIST and RUN it, but
please don't modify it, yet...
Notice the address of the SYS
command. What a coincidence!
This just happens to be at the END
of the BASIC program. Have you ever
LOADed a BASIC program only to find a
SYS(2063) or SYS2061? Same principle.
Now both your monitor and my
little demo must be in memory. Do a
memory dump of locations $082B
through $083A.
You should see something like
this:
082B: 37 08 78 00 9E 20 20 32
0833: 31 36 30 00 3D 08 82 00
This is equivalent to BASIC's --
120 SYS 2160
What do those bytes mean? Let's
start at $082B. This pair of bytes is
a POINTER to the next line. Here the
next line is at $0837. The next pair
of bytes is the LINE NUMBER. In our
case, it's 120. Lo-byte = 120 (in hex
$78) Hi-byte = 0.
The next byte MUST BE a token or
variable assignment (i.e. A=2 or
B$="HI!"). For us the byte is $9E, or
the token for the... take a guess...
the SYS command! The next 2 bytes are
spaces. What always comes after a SYS
command? (I feel like I'm hosting a
game show, "You've won a trip to
Hawaii!") Right again, the ADDRESS for
the SYS command. Wasn't the address
2160? Let me fetch my trusty little
HEX<->ASCII conversion chart. Hmmm...
$32->"2", $31->"1", $36->"6", (is it
me or is there a pattern emerging
here?) and $30->"0". Aha -- "2160"! We
found the address! (Just call me Scott
"Indy" Resh).
The next byte is $00. Every line
of a BASIC programs ALWAYS ends with
a $00. In computerese, that's
"null-terminated". (Remember that if
you ever hope to be a GEOPROGRAMMER)
On to the next byte. I'm curious,
what's the address of the next byte?
Let's see here... 0833, 0834, 0835,
0836, 0837... that's it! Wait a
minute, I've seen that number before!
(deja-vu?) The FIRST pair of bytes we
looked at pointed to 0837. We traced
our way to the beginning of the next
line.
By the way, try replacing the $9E
at $082F with a $8F or a $99.
Fender asked me to say a word or
two about UNNEWing a BASIC program
and the line numbers of a BASIC
program.
UNNEWing is the process of
recovering an accidentally NEWed (NOT
nude) BASIC program. Hopefully, this
technique will save a few gray hairs.
Don't forget, your trustworthy monitor
must be in memory first. Enter a few
lines of BASIC. Now, enter NEW --
that's right, NEW. What NEW does is
POKE two zeroes into $0801 and $0802
and updates three zero page pointers.
This effectively ends your program
before it starts.
Invoke your monitor. Use your
monitor's HUNT command to find the
first occurrence of three zeroes after
$0805. In my case, I enter:
H 0805 9FFF 00 00 00 <RETURN>
Are you asking yourself "Hey
Scott, why $0805? Why not $0801?" I'm
glad you caught that. Both $0801 and
$0802 contain $00. Locations $0803 and
$0804 contain the line number of the
first line of your BASIC program. It's
possible that your first line number
could be 0. To play it safe, we are
going to ignore these four bytes for
now.
Write down the first number that
is displayed by your HUNT command.
Add THREE to that number. POKE the
LO-byte into locations $2D, $2F, and
$31. POKE the HI-byte into locations
$2E, $30, and $32.
Time to use the HUNT command
again. Enter:
H 0805 9FFF 00 <RETURN>
That's right, we want to find the
first occurrence of ONE zero all by
itself. Add ONE to the first number
that was displayed. POKE the LO-byte
of the result into $0801 and the
HI-byte into $0802.
Exit your monitor. You should be
back in BASIC. LIST your program.
There's your program!
Now, about line numbers. There
is a "hackerish" thing you can do to
protect your BASIC program. By
protect, I mean that you can create a
line in your program that keeps your
program from being edited. (Please
note that any program submitted to us
that uses this technique will almost
IMMEDIATELY be rejected.)
Commodore's BASIC 2.0 will not
allow a BASIC line number greater than
63999. Suppose your BASIC program is
200 lines long and you want to protect
it. Simple, POKE 2051,0 and POKE
2052,250. LIST your program. Notice
that your first line is now line
number 64000! Whoa! How'd that happen?
Locations 2051 and 2052 contain
the LO-byte and HI-byte, respectively,
of the first line number of a BASIC
program.
250*256 + 0 = 64000 ($FA00)
Why don't you try using POKE again
to change the line number from 64000
to 64010?
I'd like to thank Len Thomas for
his thoughtful and considerate
suggestion for this month's BITS
column. ("What do those %&#! numbers
mean!?") We had a lot of fun talking
about hex numbers and such.
If anyone, I mean ANYONE, out
there would like to see something
pertaining to machine language
explained/performed in BITS, write to
us or call.
SER