- •About the Author
- •Dedication
- •Author’s Acknowledgments
- •Contents at a Glance
- •Table of Contents
- •Introduction
- •Who Should Buy This Book
- •How This Book Is Organized
- •Part I: Programming a Computer
- •Part II: Learning Programming with Liberty BASIC
- •Part III: Advanced Programming with Liberty BASIC
- •Part VI: Internet Programming
- •Part VII: The Part of Tens
- •How to Use This Book
- •Foolish assumptions
- •Icons used in this book
- •Why Learn Computer Programming?
- •How Does a Computer Program Work?
- •What Do I Need to Know to Program a Computer?
- •The joy of assembly language
- •C: The portable assembler
- •High-level programming languages
- •Database programming languages
- •Scripting programming languages
- •The program’s users
- •The target computer
- •Prototyping
- •Choosing a programming language
- •Defining how the program should work
- •The Life Cycle of a Typical Program
- •The development cycle
- •The maintenance cycle
- •The upgrade cycle
- •Writing Programs in an Editor
- •Using a Compiler or an Interpreter
- •Compilers
- •Interpreters
- •P-code: A combination compiler and interpreter
- •So what do I use?
- •Squashing Bugs with a Debugger
- •Writing a Help File
- •Creating an Installation Program
- •Why Learn Liberty BASIC?
- •Liberty BASIC is easy
- •Liberty BASIC runs on Windows
- •You can start using Liberty BASIC today
- •Installing Liberty BASIC
- •Loading Liberty BASIC
- •Your First Liberty BASIC Program
- •Running a Liberty BASIC program
- •Saving a Liberty BASIC program
- •Getting Help Using Liberty BASIC
- •Exiting Liberty BASIC
- •Getting input
- •Displaying output
- •Sending Data to the Printer
- •Storing Data in Variables
- •Creating a variable
- •Assigning a value to a variable
- •Declaring your variables
- •Using Constants
- •Commenting Your Code
- •Using variables
- •Working with precedence
- •Using parentheses
- •Manipulating Strings
- •Declaring variables as strings
- •Smashing strings together
- •Counting the length of a string
- •Playing with UPPERCASE and lowercase
- •Trimming the front and back of a string
- •Inserting spaces
- •Yanking characters out of a string
- •Looking for a string inside another string
- •Using Boolean Expressions
- •Using variables in Boolean expressions
- •Using Boolean operators
- •Exploring IF THEN Statements
- •IF THEN ELSE statements
- •Working with SELECT CASE Statements
- •Checking a range of values
- •Checking a relational operator
- •Boolean expression inside the loop
- •Looping a Fixed Number of Times
- •Counting with different numbers
- •Counting in increments
- •Anatomy of a Computer Bug
- •Syntax Errors
- •Fun with Logic Errors
- •Stepping line by line
- •Tracing through your program
- •Designing a Window
- •Creating a new window
- •Defining the size and location of a window
- •Adding color to a window
- •Putting Controls in a Window
- •Creating a command button
- •Displaying text
- •Creating a check box
- •Creating a radio button
- •Creating text boxes
- •Creating list boxes
- •Creating combo boxes
- •Creating group boxes
- •Storing Stuff in Text Files
- •Creating a new text file
- •Putting stuff in a text file
- •Adding new stuff to an existing text file
- •Retrieving data from a text file
- •Creating a new binary file
- •Saving stuff in a binary file
- •Changing stuff in a binary file
- •Retrieving stuff from a binary file
- •Creating a Graphics Control
- •Using Turtle Graphics
- •Defining line thickness
- •Defining line colors
- •Drawing Circles
- •Drawing Boxes
- •Displaying Text
- •Making Sounds
- •Making a beeping noise
- •Playing WAV files
- •Passing Data by Value or by Reference
- •Using Functions
- •Defining a function
- •Passing data to a function
- •Calling a function
- •Exiting prematurely from a function
- •Using Subroutines
- •Defining a subroutine
- •Passing data to a subroutine
- •Calling a subroutine
- •Exiting prematurely from a subroutine
- •Writing Modular Programs
- •Introducing Structured Programming
- •Sequential instructions
- •Branching instructions
- •Looping instructions
- •Putting structured programming into practice
- •The Problem with Software
- •Ways to Make Programming Easier
- •Breaking Programs into Objects
- •How to use objects
- •How to create an object
- •Creating an object
- •Starting with a Pointer
- •Defining the parts of a linked list
- •Creating a linked list
- •Managing a linked list
- •Making Data Structures with Linked Lists
- •Stacks
- •Queues
- •Trees
- •Graphs
- •Creating a Record
- •Manipulating Data in Records
- •Storing data in a record
- •Retrieving data from a record
- •Using Records with Arrays
- •Making an Array
- •Making a Multidimensional Array
- •Creating Dynamic Arrays
- •Insertion Sort
- •Bubble Sort
- •Shell Sort
- •Quicksort
- •Sorting Algorithms
- •Searching Sequentially
- •Performing a Binary Search
- •Hashing
- •Searching by using a hash function
- •Dealing with collisions
- •Picking a Searching Algorithm
- •Choosing the Right Data Structure
- •Choosing the Right Algorithm
- •Put the condition most likely to be false first
- •Put the condition most likely to be true first
- •Clean out your loops
- •Use the correct data types
- •Using a Faster Language
- •Optimizing Your Compiler
- •Programming Computer Games
- •Creating Computer Animation
- •Making (And Breaking) Encryption
- •Internet Programming
- •Fighting Computer Viruses and Worms
- •Hacking for Hire
- •Participating in an Open-Source Project
- •Niche-Market Programming
- •Teaching Others about Computers
- •Selling Your Own Software
- •Trying Commercial Compilers
- •Windows programming
- •Macintosh and Palm OS programming
- •Linux programming
- •Testing the Shareware and
- •BASIC compilers
- •C/C++ and Java compilers
- •Pascal compilers
- •Using a Proprietary Language
- •HyperCard
- •Revolution
- •PowerBuilder
- •Shopping by Mail Order
- •Getting Your Hands on Source Code
- •Joining a Local User Group
- •Frequenting Usenet Newsgroups
- •Playing Core War
- •Programming a Battling Robot
- •Toying with Lego Mindstorms
- •Index
- •End-User License Agreement
20 |
Part I: Programming a Computer |
Essentially, computers really understand only one language, which consists of zeroes and ones, also known as machine language. A typical program that you could write in machine language might look something like the following example:
0010 1010 0001 1101
0011 1100 1010 1111
0101 0110 1101 0101
1101 1111 0010 1001
Machine language has the following two major drawbacks:
You can easily type a 0 or 1 by mistake, thereby preventing you from giving the computer the correct instructions.
Machine language takes a long time to write (and an even longer time to understand what the language is actually telling the computer to do).
Because of these two huge problems, few people write programs in machine language. To make writing a program easier, programmers quickly invented a simpler programming language known as assembly language.
The joy of assembly language
The whole purpose of assembly language is to enable you to write programs faster and easier than using machine language. So rather than force programmers to write cryptic programs using 0s and 1s, assembly language uses short, easy-to-remember (to programmers, that is) phrases such as JMP, MOV, and ADD, which represent specific machine-language instructions.
Not only does this convention make assembly language source code shorter and easier to write, but it also makes the code easier to read and modify later. A typical assembly language program looks like the following example:
title Nap Program
; This program displays “Take a nap!” on the screen dosseg
.model small
.stack 100h
.data
my_message db ‘Take a nap!’,0dh,0ah,’$’
.code main
Chapter 2: All about Programming Languages |
21 |
int 21h
mov ax,4C00h int 21h main endp
end main
Making programs easy to read and modify is crucial because most programs never work right the first time you use them. And if you want to add new features to a program later, you need to understand how the current program works so that you know how to modify it.
Programmers created assembly language for their convenience only. The computer itself has no idea how to read or use any instructions written in assembly language.
Because computers can’t read assembly language instructions, programmers created special programs that translate assembly language into machine language. These special programs are known as assemblers. If you give your computer a program written in assembly language without an assembler, your computer won’t have the slightest idea how to read assembly language.
So after you write a program in assembly language, you have to feed it to an assembler, which translates your assembly language program into machine code, which your computer can understand.
Assembly language offers the following two distinct advantages over machine language:
Assembly language programs are easier to read than machine language programs.
Assembly language programs are easier to write (and modify) than machine language programs.
Of course, assembly language has the following disadvantages:
Programs that you create by using assembly language run slower and gobble up more space (both physical disk space and memory) than equivalent programs that you may create with machine language.
You can’t easily transfer (or, to use programming lingo, port) a program that you write in assembly language for one computer to another computer.
Writing a program in assembly language can prove extremely tedious, time-consuming, and complicated. That’s why few people bother to write large programs in assembly language.
22 |
Part I: Programming a Computer |
In general, the easier the programming language is to read and write, the slower and larger are the programs it creates. The Holy Grail of computer programming is to create programs that are easy to write, run as fast as possible, and take up as little space as possible.
C: The portable assembler
To combat the drawbacks of assembly language, programmers created a wide variety of different programming languages with names such as COBOL and FORTRAN. (See the following section, “High-level programming languages,” to find out more about the advantages and disadvantages of these types of programming languages.)
But some programmers felt that they needed a language that offers the power to access hardware (as does assembly language) but is easier to read, write, and modify (as are COBOL and FORTRAN). Eventually, they invented a programming language known simply as C.
Programmers based the C programming language on an early programming language by the name of B (although no programming language known as A ever existed).
Programmers wanted to make programming as easy as possible for themselves, so they made the C programming language look more like actual words that people can understand, as the following example demonstrates:
main()
{
printf (“Take a nap!\n”);
}
This C program is equivalent to the assembly language program found in the preceding section of this chapter that displays “Take a nap!” on-screen. Comparing the two, you can see that the C language source code is smaller and easier to read than the equivalent assembly language source code.
By using assembly language, programmers sacrifice readability for speed and size. A program that you write in C runs slower and creates larger program files than does an equivalent assembly language program. That’s because assembly language is closer to the native language of computers (which is machine code) than C. So C programs need to first get translated into assembly language code before finally being converted into machine language code. This two-step process tends to be less efficient than writing an equivalent assembly language program. C source code, however, is much easier to read, write, and modify than assembly language source code (and far easier to read, write, and modify than an equivalent machine-language source code).
Chapter 2: All about Programming Languages |
23 |
The programmers who created the C programming language had the following three main goals:
To create a language that’s easier to read and write than assembly language.
To offer programmers the capability to access all the parts of the computer just as they can by using assembly language.
To provide a small, simple language that you can easily port from one computer to another. Programs that you write in C can run on different computers without massive rewriting, which is the main drawback with assemblyand machine-language programs.
This third goal may look strange, so here’s the rationale behind it: Computers don’t understand C any better than they understand assembly language. (Computers are notorious for not understanding much of anything, which is why programming must be so precise.) If you write an entire program using C, your computer doesn’t have the slightest clue how to read your instructions.
To make a computer read and understand instructions written in C, you must convert your C program into equivalent machine-language instructions. Programmers created special programs, known as compilers, to do this conversion for them. A compiler takes your C program and converts it into machine language, which is like translating a Jules Verne novel from French into English.
As is true of translations between human languages, the simpler the language, the easier is the translation. Translating a children’s book from French into Japanese is much easier than translating a mathematics dissertation from French into Japanese, mainly because a children’s book uses simple words, while a mathematics dissertation uses more complicated words. Similarly, translating C into machine language code is more difficult than translating assembly language into machine language code.
So the only way that you can run a C program on another computer is if someone’s already written a C compiler for that other computer. Because C is a simple language, writing C compilers for different computers is relatively easy, especially if you compare it with the same task for other programming languages, such as Ada or LISP.
Because C compilers are fairly easy to write, you can find C compilers for almost every computer in the world. Theoretically, you can write a C program for the Macintosh, copy it to a computer running Windows XP, recompile it, and run the program with little or no modification.