If we are going to learn to talk to a computer, we should learn what it is we are talking to.
Electronic computers can be found almost everywhere, but in general terms, a computer is any entity that can follow orders, more specifically, that can execute instructions. This classification includes humans (who are imperfect computers) as well as pocket calculators, programmable disc players, controllers in automobile engines, thermostats, and airplanes, PDAs, smart mobile phones, and conventional ``personal computers.''
Here, we are concerned with conventional computers. A computer is a kind of of ``electronic accountant.'' So, what is an accountant ? A reductionist description of an accountant is
The filing cabinet analogy just mentioned has generated some standard computer terminology: A file is a collection of instructions or data; files are themselves grouped into folders (also known as directories). Directories are normally kept in secondary storage, their contents copied into primary storage when needed by the processor.
The wiring is attached through an input-output device known as a socket. When computers are connected togerher, this is called a network of computers. The Internet is a famous, worldwide network of computers. (By the way, the World Wide Web is a usage of the internet. The technical definition is that the World Wide Web is an electronic set-of-rules --- a protocol --- for exchange of text, images, and programs in a standardized format.
Here is a diagram that summarizes the components of a computer:
It's based on an electricity game: Say that you take a string of 32 Christmas lights and add lots of extra wires so that you can turn on and off each of the individual lights. Say that you do this for, say, about a dozen strings of lights.
Lay out the strings of lights and plug them in. It would be nice to have a box that is wired to all 12 strings of lights, so that you could push some switches on the box and make the strings light up in various patterns. Pretend that you have done this.
What you have built so far is a central processing unit (CPU). Each string of lights is called a register, and the box that is connected to all the strings is the arithmetic-logic unit (ALU). In a moment, you will see that the pattern of on-and-off lights in a string can represent a number. So, the CPU you built can remember 12 numbers (it has twelve strings), and you can add more wiring to the ALU so that it can ``add,'' ``subtract,'' etc., the ``numbers'' represented by the strings.
Alas, your CPU can remember just 12 numbers ---- just 12 data ! This isn't enough to do real jobs, like printing paychecks or playing computer chess. We need more strings of lights ! It is truly expensive to lay out light strings and wiring in the way we have done for the CPU, so we need another means to hold thousands and thousands of numbers. This is what primary storage is for --- it is a cheap set of Christmas lights that holds thousands of strings of lights.
Now, here is the trick: we don't wire the cheap lights in primary storage directly to the ALU --- we figure out a wiring that can copy the pattern of lights of any string in our cheap lights to one special string in the CPU. And then we use this one wiring (it is called a system bus) to copy from primary storage into the special register (called the data register) in the CPU. (From there, the light-pattern can be copied to any of the other registers.)
This is the basic idea behind computer architecture --- it is patterns of on-off lights in strings of lights, where some of the strings (the ``registers'') are wired to an ``arithmetic box'' (ALU) in the CPU, and lots of cheap strings of lights are placed in primary storage, where their on-off patterns can be copied, one string at a time as needed, in and out of the CPU.
Disk storage and CD storage work similarly, where a disk uses north-south magnetization for on-off, and a CD uses bumps and holes on its plastic surface for on-off.
If a computer can execute instructions, what kind of instructions can it execute? This depends---a conventional notebook computer of course cannot execute the instructions written in a cookbook, nor can it follow the instructions for driving a car from Chicago to Manhattan. (But there are special-purpose computers that can attempt the latter.)
The CPU inside a typical computer performs a range of arithmetic-like instructions --- addition and subtraction and text copying. To do these tasks, numbers and text must be coded in sequences of 1's and 0's (these are ``on'' and ``off'' !), called bits, so that they can be represented in the registers in the CPU. The technique of writing numbers, text, and instructions in 1-and-0 (on-and-off) patterns is called binary coding.
One of the miracles of
number theory is that all of arithmetic can be performed on sequences
of 1s and 0s---this is called binary arithmetic.
Numbers like 7 and 19 are written as the
binary numerals 111 and 10011, respectively.
Binary arithmetic operations
are easy to formalize and they are wired into the CPU's ALU.
See Figure 2 for examples.
FIGURE 2: an example of binary arithmetic================================
How to write a binary numeral (it's a sequence of powers of 2):
7 = 4 + 2 + 1 = 1 x 22 + 1 x 21 + 1 x 20
= the binary numeral 111
19 = 16 + 2 + 1 = 1 x 24 + 0 x 23 + 0 x 22
+ 1 x 21 + 1 x 20
= the binary numeral 10011
In this way, each quantity (base-10 number) can be converted to a unique
pattern of 1s and 0s. An index register in a computer
holds 32 digits, e.g., 19 looks like this inside a
Addition of binary numbers is simpler than usual---there are fewer cases:
0 + 0 = 0
0 + 1 = 1 + 0 = 1
1 + 1 = 10 (that is, 0 and a ``carry'' of 1)
When we add two multi-digit binary numbers, we must manage the carry values.
Example: 19 + 7
carry: 1 1 1 1
19: 1 0 0 1 1
+ 7: + 1 1 1
26: 1 1 0 1 0
When the ALU does this addition, it copies 19 into one register, and 7
into another register. The ALU uses its wiring to see the patterns and
copy the pattern that is the sum of 19 and 7 into a third register.
Binary numbers are handled in units of eight bits at a time; such a unit is called a byte. (An example byte is 00011010.) Of course, large numbers can not be entirely encoded within one single byte, so computers represent a number as a sequence of four bytes (32 bits), called a computer word. It is also possible to use binary numerals to stand for letters. There are standard codings, such as the ASCII and Unicode codings, that encode each keyboard symbol into binary form. (For example, the coding of the keyboard letter, a, is 97, that is, 1100001.)
By using binary codings, a computer can compute with letters as well as numbers. Images can also be coded with binary codings: An image is a grid of ``points'' (dots), where each point is colored by a color represented by three numbers: a binary number that represents the point's red content, a binary number that represents its green content, and a binary number that represents its blue content. Sounds can also be coded by a sequence of binary numbers.
If you like, think again about strings of Christmas lights and a box wired to the strings. How do you tell the box what to do ? Perhaps you move some switches on the box to make something happen. But if your light strings will be used for 12 hours a day, every day, to light the display windows at Macy's, you will not want to be there yourself to press the switches!
No doubt, you will want to build yet another box of wires that connect to the ALU so that when you start the other box, it tells the ALU what to do. The new box is a ``computer program,'' and indeed, the computers built in the 1940s had such programs wired directly to the ALU.
But here is a better idea: We can use patterns of light to stand for instructions for pushing the ALU's switches, and we can store the patterns in the primary memory! Then, we attach a control unit box to the ALU that tells the ALU to do the following over and over:
The extra box attached to the ALU is called the control unit of the CPU. Steps 1-2-3 listed above are called the fetch-decode-execute instruction cycle, and the patterns of lights in the primary storage are called a computer program. Here is a picture of the CPU:
Notice that a stored program is a sequence of instructions, and each instruction is a pattern written as a binary number. Must we write binary numbers to tell the computer what to do ? In the 1940's this was indeed the case, but soon people developed translator programs, called compilers and interpreters, which convert instructions humans write into the sequence of instructions coded as binary numbers.
One of the first widely used translator programs was FORTRAN (''FORmula TRANslator''); it converted algebra equations into binary-coded instructions. Now, there are many different translator programs, each of which knows how to convert instructions in a particular programming language to binary-coded instructions. (Examples are C, C++, Java, PERL, Prolog, Scheme, ....)
In this course, we will learn how to write programs in the Python language, which are converted by the Python interpreter into binary-coded instructions that are stored in primary storage and are read and executed by the CPU.
Real computer programs can be huge, and a typical computer can keep only a few programs in primary storage, so most computer programs are saved on the internal disk. When a human wants the computer's CPU to execute a program, it gives a signal to the computer, which copies the program from disk to primary storage, and starts executing the instructions, by using the fetch-decode-execute instruction cycle.
But how does the human tell the computer to start a program ?
When a computer is first started, its CPU looks on the computer's disk for a program to copy into primary storage to execute; the program it finds is the operating system. An operating system is the computer's ``controller program''; by displaying information on the display and by receiving messages from mouse and keyboard, the operating system helps the computer's user start programs. Often, a user's request to start a program is little more than a mouse movement and a click, or it might be the typing of text within a command window (or command-prompt window or MS-DOS window).
Prior to the 1980s, an operating system used the computer's display as one large command window. Programs were started by typing within the window, and the program would read its input data from the window and would display its output within the same window. All input and output were text---words and numbers.
Modern operating systems
create multiple windows on the display, where the windows might be
or windows created by executing programs.
shows a display that holds three distinct windows:
FIGURE 3: a multi-window display=======================================
The picture shows
a command window, a window created by a word-processing program,
and a window created by a Web-browsing program.
These windows were created with the behind-the-scenes help of
the operating system.
The user interacts with the window by moving the mouse into it, typing,
clicking, or reading. Icons appear
along the left side of the display.
The operating system program is always resting in primary storage, and the CPU does the instructions within the operating system when it is not executing a program that the user has started. The operating system also ``protects'' the computer from harmful programs that a human might start. The details about how this is done must wait for a later lecture.
The previous graphic showed a command window. To create a command window, press Start (in the lower left corner of the display) and use the mouse to select the Programs menu and then the Accessories menu and then the CommandWindow menu item. This creates a new command window, and when you move the computer mouse into the window and click, you have activated the keyboard so that the words you type into the window will be read by the operating system.
You talk to the operating system in a special language, called command language. We will learn command language, bit by bit, as we need it, but here are some examples of simple instructions typed in commmand language:
|dir||list the files on the computer disk|
|mkdir FOLDERNAME||Make a new folder (directory) named FOLDERNAME at the current position on the disk|
|cd FOLDERNAME||open the folder, FOLDERNAME, so that you can list the files (so, next type dir and see...)|
|type FILENAME||show the text held within the file, FILENAME|
|copy FILENAME FILENAME2||Make a copy of FILENAME and name it FILENAME2|
|move FILENAME FILENAME2||Move file FILENAME and rename it FILENAME2|
|del FILENAME||remove FILENAME from the disk|
|notepad FILENAME||start the notepad text editor, which creates a new window for editing FILENAME|
|python||start the Python interpreter, which lets us talk directly to the computer, in the language of Python|
There are many more commands to learn. Perhaps you already know how to use menu items and icons, along with mouse clicks and drags, to list files, open folders, delete files, etc. But the menu items, icons, clicks, and drags are really just ``sign language'' to the operating system. The commands listed above truly let us ``talk'' to the computer.