Liberty Basic is develeopped by Carl Gundel
Original Newsletter compiled by Alyce Watson and Brosco
Translation to HTML: Raymond Roumeas

  The Liberty Basic Newsletter - Issue #29 - MAR 99

"Genius is 1% inspiration and 99% perspiration." - Thomas Alva Edison


In This Issue:

The third part in Stev Harney's series,

"There's more than one way to skin a cat." Thanks Stev!

Coming Soon:

We have another guest author! Bob Oakes has provided a demo/tutorial on creating DOS batchfiles. Watch for it here soon!


"There's more than one way to skin a cat." (Part 3)

Part 1 covered the big, general aspects of skinning cats.

Part 2 covered specific aspects of skinning cats and started getting into the details.

In this part we continue from where we left off and get further into the details of having more than one way to accomplish things.

We'll start by ripping away the fur and fussiness of illogical thinking and look at the guts underneath - the building blocks of structured programming, the cornerstones of contention and the fundamentals of communication - that make up what we call computer languages.

At first our minds will reject this type of thinking because we haven't analyzed the way we think in terms of logic. As humans, we tend - more or less - to have feelings about things or make wishy-washy judgements based upon so called "wisdom" passed down to us through previous generations of computer illiterates.

Some people like spinach but never play baseball, while others play the game at the drop of a hat but can't stand eating twigs and leaves. Personal choices we made at an early age that imprinted certain feelings deep within our cortex. After living with these feelings for so many years they seem natural to us and are very hard to let go of.

I know it's difficult, but we need to make changes (see rule 2 in part 1) in our thinking. We need to learn how 'computers' do things. We need to learn how logic works and why computers use this logic to accomplish the tasks we ask of them.

Computers are great number crunchers, and they can handle several types of numerical-systems. They can use these various systems to work on different types-of-numbers, such as; Floating point (fractions), signed (+ or -) or unsigned numbers, and whole numbers (integers).

For the purpose of this article, we'll only be dealing with whole unsigned byte-sized decimal integers, binary notation and boolean conditions. For more detailed information on numerical systems, see my next series of articles titled; "Numbers, numbers, numbers, the fusion of confusion".

The binary numbering system isn't a subject you will be able to pick up and be done with in a couple of days or a long weekend. And you probably won't learn much about computer logic by dabbling at it Willie-nilli. This will take a concentrated effort on your part to derive any of the real benefits.

As in baseball, you don't need to understand ALL the rules to play the game, but the more rules you understand, the better you can play. You will need to reread this article several times and study other related materials that are available.

It's not as scary as you might think, but in order to do this properly, we need to have the right attitude and be willing to practice what we are learning more than once (see rule 3 in part 1).

Don't be shy, jump in and get your feet wet. Be brave and toss the kitty into the crystal-clear binary pool of boolean logic. Meeoooowwwww. Splash!

3 2 1

10987654321098765432109876543210 : Binary digit positions


0 = 1 bit

0000 = 4 bits = 1 nibble

00000000 = 8 bits = 1 byte

0000000000000000 = 16 bits = 1 word

00000000000000000000000000000000 = 32 bits = 1 long

I typed these bit-positions as zeros for clarity, but usually these bit- sets are filled with what look like random 1's and 0's. I assure you that they are NOT random unless you are using the random function, each group is holding a valid number used by something in your system. Here is an example of a typical byte you'd find: 01101001 :which equates to 105 in decimal.

These obscure patterns seem strange at first because we have nothing to compare them to and it's a bit hard to get a handle on them because they are so small. Very small - one bit to be exact. Everything you enter into your program is eventually converted into this binary machine-language format because it is the only thing your computer understands. There are millions of these ones and zeros zooming around in the CPU, disk drive, memory chips and splattered all over your screen.

Starting at the shallow end of the pool, we begin picking them up one at a time and stacking them together in larger groups and giving names to each group to make them easier to handle. 4 bits is a nibble. 8 bits is a byte. 16 bits is a word and 32 bits is a long word. There were also systems that used 6 bits, 12 bits or 24 bits, but you never see them around anymore.

NOTE: Binary numbers are based on powers of 2, not powers of 10 like decimal numbers. In fact, the binary numbering system is called "Base 2". In other words, each digit to the left has a value of exactly twice the value of the digit to its right instead of the way we count in decimal where each digit to the left has a value of 10 times the digit on its right. Hmmm, maybe reading it spelled out with words is confusing, lets break that down into a couple of small diagrams and look at it again.

 

DECIMAL (base 10):

1000 100 010 001 : decimal digit values

1 0 5 : 105 decimal (decimal format)

Add these up;

100 or 1 100's
+ 0 or 0 10's
+ 5 or 5 1's
---
= 105

 

BINARY (base 2):

128 064 032 016 008 004 002 001 : binary digit values

0 1 1 0 1 0 0 1 : 105 decimal (binary format)

Now add up the bit values of any bits that are set (1)

0 or 0 128's
+ 64 or 1 64's
+ 32 or 1 32's
+ 0 or 0 16's
+ 8 or 1 8's
+ 0 or 0 4's
+ 0 or 0 2's
+ 1 or 1 1
---
= 105

Is this making sense yet? Try another one;

128 064 032 016 008 004 002 001 : bit values for 1 byte

1 0 0 0 0 0 0 1 : 129 decimal (binary format)

Add the bit values of any bits that are true (1)

128
+ 1
---
= 129

Binary notation is just another way to view or look at numbers. It's not hard, just time consuming, error prone, and not very user friendly. Load up the attached program I sent in with this article and fiddle around with it to get yourself accustomed to reading binary numbers.

As you can see, each bit can hold only one value at a time, either a 0 or a 1. All decisions within your CPU are based on the condition of these bits.

For the rest of this article, we'll only be using one of these bits. The two symbols it can hold (1 or 0) represent the two conditions or states this bit can be in. Think of it as a miniature switch that can be either ON or OFF.

These are the two cornerstones I mentioned before. It can't get much simpler. This is something a computer can handle very well.

These are called BOOLEAN logic tests, BOOLEAN conditions or just BOOLEANS. I think Boolean was the name of the guy that first realized all decisions could be broken down to this tiny one bit level.

Some languages have internal constants set up and the words 'true' and 'false' are considered keywords and cannot be used for anything else.

In other languages (Liberty Basic for one) you could actually lie to your computer and tell it just the opposite - that false = 1 and true = 0 and it would be just as happy. The reason for this is because your computer doesn't care! All it's concerned with is having a unique symbol for each of the conditions.

How you use the symbols is up to you.

Here's an example: Copy and paste these lines into LB and try it.

false=0 : true=1 : x=0 : if x=false then print "False" else print "True"
false=1 : true=0 : x=0 : if x=false then print "False" else print "True"

Howling madly into the wind of knowledge, the kitty screams in agony as we begin to practice these different ways. Yeeeeeeeeooooooooooooowwwwwww.

Run this little pill and then stop and think about what it is doing.

x = 5 : while x : print x : x = x - 1 : wend

What happened here is that as long as the value of x was above 0, it kept printing the result. As soon as x became 0, it lost is value and the loop terminated. You could do the same by starting with a negative value and adding 1 each time instead of subtracting - try it.

Now see what happens when you move the 'print x' after the x = x - 1

x = 5 : while x : x = x - 1 : print x : wend

In most computer languages, any variable that is holding a value other than zero is considered to be true because it HAS value.

Now try this one, then change the value of x and try it again.

x = 29 : if x then print "True" else print "False"

All three were asking the same thing: if x has ANY value other than zero then return a result of true.

Notice the words true and false have changed positions? Also notice that there isn't anything between the x and the word then. It's a short-hand method of coding. This may sound trivial, but back when I started, saving a few bytes here and there by leaving off "a space, an equal sign (=), a greater-than (>) or less-than (<) character and a value to compare to", could mean the difference between a program fitting into memory or not being able to load because it was to big. It was very important to whittle things down to its smallest form of pure logic. This type of test won't work in cases where you need to know the specific value of something, but if the actual value is unimportant, why add the extra time to type it and force the processor into making longer comparisons. It just wastes time and space.

See how we can play around with logic? These are just SOME of the ways we skin cats - and there are plenty more.

Now that the mud we kicked up at the bottom of the boolean pool has settled, everything should be much clearer. You should understand that computers do things by switching current on or off and it's time to move on. It's time to study how we can communicate with our computers using these tiny logic switches in structured programming.

"Hey", you say, "I thought you told us there was more than one way to skin cats, but this looks like there are only two ways, maybe three, what gives?"

You're right AND you're wrong. There are ways to compound (leverage) the effect of these booleans to your advantage - keep reading.

When we need to know the condition of something that has happened in a program we create a logic-test. We ask a question and receive an answer. There are all sorts of questions to ask and many ways of asking From these answers, we can make logical decisions and do something in the code.

If we need to pause for a second to let the video board display something, we could set up a timer test and wait until we get a specific answer before going on to the next part of the program with something like this;

temp = val(right$(time$(),2)) + 1 ' set up a temp value
if temp > 59 then temp = 1 ' overflow correction
while val(right$(time$(),2)) < temp : wend ' conditional test

This code will keep checking the internal clock and comparing it to the temp value we set until that second has passed, and when that happens, the loop terminates, and the instruction pointer moves to the next line of code. In other words, as long as the question returns a truthful answer (TRUE, the time now is less than your test value), it will continue checking. As soon as the clock advances far enough and the time is greater than your temp value, you will get back an answer of false (FALSE, time is not less than your test value).

Easy huh? If you can learn to break everything down to this level of true or false testing you will be well on your way to understanding what makes a computer tick and how you can use the knowledge to make your program do what you want.

bad = 0 : good = 1
kitty = good
If kitty = bad then put kitty outside
if kitty = good then treat kitty to catnip

We could also ask the opposite;

if kitty = NOT good then put kitty outside

The keyword NOT inverts the binary value after the boolean-test and THEN gives you the result.

Okay, as promised in part 2 we'll look deeper into the multi-line if, then, else boolean testing and see what we can learn. This is how we leverage the power of our one bit switch. Here goes. The cat is out of the water now and it's time to go to work. Here kitty, kitty, nice kitty. Meow?

First we set up a few color values

black = 0
blue = 1
green = 2
cyan = 3
red = 4
purple = 5
 
x = blue

We'll use the variable named x to hold the color value and k will hold the ASCII value of the pressed key. Suppose we need to change the color of something on screen depending on which key a user pushed. The user may press any key, so we need to test first and then make a change.

First is a simple, one-line example (easy to type, easy to read, and very fast for the computer to execute).

' if user presses R then set color to red, otherwise set color to green
if k = 82 then x = red else x = green

Now a multi-line statement (more typing, just as easy to read).

' if user presses R then set color to red, otherwise set color to green
if k = 82 then
x = red
else
x = green
end if

Although these structures look different, the end result is the same because logically they are equivalent. You can use these multi-line statements in places where you need to have many lines of code for each condition.

NOTE: The next piece of code was written as an example of doing that, but you shouldn't try to run it because there are no target lines for the goto statements.

if k = 82 then
x = red
print "the color is set to red"
t = val(time$())
backcolor = black
for z = 1 to 3 : beep : next z ' a pretend error message
notice "Error";any$
y = 1 : z = 2
' we can also include OTHER logic tests WITHIN this type of structure
if today = Friday then ' it's payday
bankbalance = bankbalance + paycheck
end if
if today = Saturday then
goto [computerstore]
end if
if today = Sunday then
goto [sundayschool]
end if
' single line tests may also be included
if today = Monday then goto [work]
else
x = green
print "the color is set to green"
t = val(time$())
backcolor = white
y = 3 : z = 4
notice "Okay";any$
' we could write hundreds of lines of code in each section.
if temprature < 75 then heat = heat + 1
end if

All three examples are testing the same thing. If the user does press the R key, the color is set to red, otherwise the color is set to green. In the third example I have included several other boolean tests to show you how to add versitility into your programs.

Agreed, this is a very simple test, but when you get the hang of it, you'll soon be getting very creative and doing all sorts of conditional testing, both simple and complex, and single and multi-line.

All right, if you're still with me, you can play around with this little;

' Syntactically correct bug
x = 1
if x = 1 then
y = 5
else if x = 2 then
y = 10
end if
print y
input a$
end

Copy this little snippet into LB and try it, then change the value of x to 2 and run it again, then, set x to 3 or more and see what happens! Can you figure out what's wrong?

It happened to me when one of our cats walked across the keyboard while I was getting a cup of coffee.

<GRIN> Damn cats, we should skin them all.


BRANCHING:

You may also jump to another part of your program depending on conditions like these. Branching means to move from where you are now to the [target] line in the program. Cats do this all the time while climbing trees. If a bird is on branch 2, kitty jumps to branch 2. It just means to go to another part of the program and start executing the code on that line. If the bird flitters away to branch 5, the cat will usually follow by jumping to branch 5 and execute the bird there.

if k = 82 goto [line1] else goto [line2]

or

if k = 82 then
more code here
goto [line1]
else
more code here
goto [line2]
end if

What this does is test to see if the user pressed the R key and if that is true the program jumps (branches) to the label [line1] and if not, the program branches to [line2]

Sometimes the bird gets away completely and we need a default statement to catch this and take care of things.

That's what these else statements are for. They hold other statements to use if the test above it fails. Lets see how this would be written in code. In this case, the cat is afraid of heights, anything above branch 3 is unacceptable.

NOTE: The word kitty in parenthesis is just to help you see what is going on and would NOT be part of your code.

if bird < = branch3 then
(kitty) goto [thistree]
else
(kitty) goto [anothertree]
end if

Okay, what this says is; If the bird is on or below branch 3, kitty will climb this tree. If the bird is above branch 3, kitty will go to another tree and see if there are any birds. We use these branching statements to move around in a program. At critical points we make a logical choice and take a fork in the road. We decide to go left or right, or in this case, goto another line and work on other problems or handle other tasks.

Ahhh, the lights behind your eyes are blinking now! Can you see how useful this could be in a game program

... you could have your cat jumping all over the forest - and probably catching a few birds (bonus points) for lunch.

By now you're thinking that all this binary stuff is completely new, computer-age thinking - sort of like magic or something - but, the truth lies elsewhere. We live in a world of duality. Good vs bad, up vs down, in vs out, left vs right, on vs off - everything can be refined to its smallest level and converted into a true or false test and - here's the real kicker. Nature worked out all this low level digital stuff millions of years ago - you were just not aware of it until now.

If you don't believe me, take another look at the information available to us about DNA (DeoxyriboNucleic Acid) or the Atomic structure.

Take out your magnifying glass, look at things REAL CLOSE, and finally ...

Don't let the cat get your tongue, speak up, ask questions and give me some feedback on any part of this article. Let me know if you think they are too long or to short or if you need more or fewer details.

And if you're so inclined, give me another subject to tackle, wrestle to the ground, take apart and explain or you. There are many aspects of programming we haven't touched on yet - stuff like; the difference between goto and gosub, validating user input, white space, file systems, telecommunication software, sorting data, working with arrays (both single and multi- dimensional), or any of a hundred others that I haven't mentioned.

Until then, Stev

PS: The kitty in this story is safe and sound after its adventure It has eaten its dinner and is now napping peacefully at my feet with one eye open looking at me while licking the funny little bits from its fur.


Newsletter compiled and edited by: Brosco and Alyce.

Comments, requests or corrections: Hit 'REPLY' now!