Copyright © 2005 David Schmidt

Chapter 0B:
Computer hardware and operating systems


0B.1 What is a Computer?
0B.2 How does a computer compute?
0B.3 What Can a Computer Compute?
0B.4 Stored programs
0B.5 Operating systems
0B.6 Talking to the operating system with a command window


If we are going to learn to talk to a computer, we should learn what it is we are talking to.


0B.1 What is a Computer?

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

  1. a pencil-with-intelligence (the accountant is holding the pencil, giving it intelligence!) calculating on a slip of paper
  2. the calculations from the paper are copied into a notebook
  3. notebooks are saved in a filing cabinet
If you open a computer, you will find a similar hierarchy: These items attach to the computer:

Here is a diagram that summarizes the components of a computer:


0B.2 How does a computer compute?

You should study computer engineering to learn the details, but here is a story that gives you a general idea.

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.


0B.3 What Can a Computer Compute?

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):

Examples:
  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
register:  000000000000000000000010011


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.

ENDFIGURE=================================================================

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.

Exercises

  1. If a computer is indeed any entity that can follow orders, then give examples from real life of ``computers.''
  2. List all the input-output devices that are attached to the computer you will use for your programming exercises.
  3. Hand-held calculators are computers. What are the input-output devices for a calculator?
  4. Based on the patterns in Figure 2, write the binary numerals for 0; 5; 18; 19; 20; 71.
  5. Define binary numeral multiplication and use it to multiply 5 by 3.


0B.4 Stored programs

Now that we have a CPU, which holds an ALU and some registers, and now that we have primary storage, which holds lots of binary numerals that can be copied into registers and added, how do we tell the ALU to copy some numbers and do some additions?

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:

  1. reach into primary storage and get the pattern of lights that stands for the next instruction to do
  2. once the pattern is copied into a register, figure out if the pattern stands for ``add'' or ``subtract'' or ``copy a data pattern from storage to another register'' or ``copy a pattern from a register to storage''
  3. do what the instruction says
Indeed, this is how modern computers work !

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 ?


0B.5 Operating systems

The operating system helps a human talk to a computer.

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 command windows or windows created by executing programs. Figure 3 shows a display that holds three distinct windows:

FIGURE 3: a multi-window display=======================================



ENDFIGURE=================================================
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.

Exercise

Use a computer to start a program, like a game or a word processor. List all the windows that are created by the program, list the ways you give input information to the program, and list the ways the program displays output.


0B.6 Talking to the operating system with a command window

When you click on an icon or a menu item, you are ``talking'' to the operating system. This form of ``conversation'' is a bit like tapping another person on the shoulder. But we can have better conversations with another person by talking in words. And indeed, one can also talk to the operating system in words by typing the words into a command window (command-line window, MS-DOS window).

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.