Sunday, August 4, 2013

Petit Computer Journal #2


Petit Computer Journal #2

Hello World!

It is a long-standing tradition among computer programmers to write a "Hello World" program when first learning a new computer language. It encompasses a fully functioning program. It tells the user that the computer is alive and kicking. So, here I will present the program. But since I like to do more than usual, I will first present the version of the program that is very popular in the old days. You can usually find it on display computers. Here it is:

10 PRINT "HELLO WORLD"
20 GOTO 10

That's it! The computer will display the words "HELLO WORLD" over and over again. What's with the numbers in the beginning of the line? They are called line numbers. Petit Computer Smile Basic does not use line numbers. No, really. Those line numbers you see in Petit Computer editor is totally unlike the line numbers you see here. Petit Computer line numbers are reference numbers. Good for telling you where your errors lie. Did you mistype PRINT as PRONTO? Error!

So, if Smile Basic does not use line numbers, how do we tell it to GOTO line 10? Well, first of all, I hope you know what GOTO does. Here's a bit of background:

Computer programs are made of different instructions. You see two of them here: PRINT, which displays the text, and GOTO which causes the instruction to go to the line number specified (in this case 10).

The computer uses something like a "Program Counter" - PC for short. All it does is specify which instruction is to be executed (or run). You can see this in action using TRACE command. Petit Computer does not have TRACE. Anyway, you have lines, and line numbers. The instructions is executed one after the other. GOTO will cause the instruction to go to the line specified. Petit Computer does not use line numbers. It uses label. You create a label by typing character '@' in front of the name. So the Hello World program goes like this:

@A :'Start of the program
PRINT "Hello World"
GOTO @A


A Word about @label

This isn't mentioned in the Help manual, but the program ignores everything after the space in label. So, theoretically, you can type this:

@A Start of the program

But I like to put in colon (command separator) and tick (comment) just in case the creator changed their mind and decide to allow something like this:

@A :PRINT "Hello World":GOTO @A

By the way, a question mark (?) is a shorthand for PRINT. So you can type ?"Hello World" instead of PRINT "HELLO WORLD"


What computers are made of?

By now, you are no doubt mesmerized by the powers of computer. The machine is capable of wonderful things. I know I'm bucking the convention here, but the computer is actually stupid. As in Forrest Gump stupid. No. Even worse than that! What are computers made of? A bunch of numbers. Really.

You see, a computer memory is a contiguous section of numbers. If you see the advertisement that a computer has 64KB. You know that a computer have about 64,000 numbers. Actually 2 to the power of 16 or 65536. Somewhere among those numbers are entries for touchscreen, buttons, various pixels on screen, and other things. But really, it's all just a bunch of numbers. Bits and bytes.

Fortunately, you don't have to know them. I usually tell people that there are two different things you can use: Text and Numbers.

Numbers are basically digits: 1,4,1234,3.5,3.46,3.121428. There is a difference between Integer and Real (Float), but not that much. Petit Computer uses 32 bit Fixed notation that is not too accurate. Be careful of round of error.

Strings are basically text. Strings are defined by enclosing text in quotation marks: "This is a text", "This is also a text", "That is a chair with a panda on it." 

You can assign numbers and text to variables (named references):
A=42 :'number.
A$="Hello World" :'string.
Notice $ mark at the end of variable name!


What about bits?

Ah, yes. Bits and bytes. For user convenience, we don't deal with bits and bytes, mostly. There are times when we will have to, usually when we're interfacing with hardware. You can do binary digits by using "&b" and hexadecimal digits by using "&h". Try this:

PRINT &B1000 :'Prints 8
PRINT &B0100 :'Prints 4
PRINT &B0001 :'Prints 1
PRINT &B1100 :'Prints 12

PRINT &HACE :'Prints 2766
PRINT &HC :'Prints 12
PRINT &HC4 :'Prints 196
PRINT &HEAD :' Prints 3757


A quick sample program?

So, ready for a quick sample program? Didn't we just have one? Hello World is a program, isn't it? Oh, you want something that computes. Ok, how about this?

?2+2

The computer should print "4" underneath that statement. If not, try doing it in direct mode. Push the orange |>RUN button on the lower screen. Got it? There you go. A full functioning program!

Haha, joking aside, that is the original purpose of the computer: To compute numbers. But I can give you something else. This one is more substantial. You type this in /EDIT mode as it will not fit in one line in direct mode. Make sure that this is the only line. Type NEW in direct mode to erase any program in memory. Then enter edit mode and type this all in one line.

INPUT "NUMBER";N:FOR A=2 TO N-1:IF N%A==0 THEN ?"NOT PRIME" ELSE NEXT:?"PRIME"

Now enter direct mode. Type RUN. Here are some sample runs:

NUMBER?
267
NOT PRIME

NUMBER?
13457
PRIME

The interesting thing is, this line will also work in old style BASIC that allows IF-THEN-ELSE structure. The commands are as follow:
1. INPUT "NUMBER";N  'The program prints out the text "NUMBER", append question mark, and waits for user input. You type in a number. The computer copies the number to variable N
2. FOR A=2 TO N-1  'This is a loop. It assigns to variable A some numbers, starting from 2 to a number 1 less than you typed. It then repeats the command between this and the keyword "NEXT"
3. IF N%A==0 THEN 'This checks whether or not N is evenly divisible by A. 
4. ?"NOT PRIME" 'Prints out "NOT PRIME" if TRUE
5. ELSE 'A keyword that means the comparison is FALSE
6. NEXT 'This is the NEXT in FOR-NEXT LOOP. Increment the value of A by one and repeat the process.
7. ?"PRIME" 'This prints the word "PRIME". It is displayed when A has reached the value N-1 (as specified in the loop declaration)

Quite a program, eh? You have 7 steps all put into one line! I know that the loop may terminate after the square root of N, but I like to keep things simple.

Here's another program. Use it to practice inputting the program into the computer:
PRINT"FACTORS"
FOR A=2 TO N
@F :'LABEL FOR WHILE LOOP
IF N%A==0 THEN ?A:N=N/A:GOTO @F
NEXT

You type this underneath the PRIME 1-liner program. It uses the N from the INPUT "NUMBER";N instruction above. There is a little trick here that isn't mentioned in the help file. You can use multiple lines in IF-THEN by using colon. So, the statements ?A:N=N/A:GOTO @F will only be executed if N is evenly divisible by A. This isn't true for old BASIC, so don't be surprised if you need some modification in order to run it in old BASIC.

What have we learned?

You know:
Text and Numbers
Labels and GOTO
Direct (RUN) mode
Edit mode
PRINT and INPUT
RUN the program (executing the program).
Walk through the code (reading the code in editor)
The computer is stupid, stupid, stupid!

Try this:
What is the largest number that can be typed after the prompt? 
Shorthand for REM(REMARK)?

Edit:
You can also do this:


PRINT"FACTORS"
INPUT "NUMBER";N
FOR A=2 TO N
IF N%A==0 THEN ?A:N=N/A:A=A-1
NEXT

If works by adjusting the value of A one step backward, effectively making it the same value as before when it gets incremented by the loop.

No comments:

Post a Comment