CONCEPT: A computer’s CPU can only understand instructions that are written in machine language. Because people find it very difficult to write entire programs in machine language, other programming languages have been invented.
• Reading a piece of data from main memory
• Adding two numbers
• Subtracting one number from another number
• Multiplying two numbers
• Dividing one number by another number
• Moving a piece of data from one memory location to another
• Determining whether one value is equal to another value
As you can see from this list, the CPU performs simple operations on pieces of data. The CPU does nothing on its own, however. It has to be told what to do, and that’s the purpose of a program. A program is nothing more than a list of instructions that cause the CPU to perform operations.
Each instruction in a program is a command that tells the CPU to perform a specific oper-ation. Here’s an example of an instruction that might appear in a program:
10110000
To you and me, this is only a series of 0s and 1s. To a CPU, however, this is an instruction to perform an operation.1 It is written in 0s and 1s because CPUs only understand instruc-tions that are written in machine language, and machine language instructions always have an underlying binary structure.
A machine language instruction exists for each operation that a CPU is capable of perform-ing. For example, there is an instruction for adding numbers, there is an instruction for sub-tracting one number from another, and so forth. The entire set of instructions that a CPU can execute is known as the CPU’s instruction set.
N O T E : There are several microprocessor companies today that manufacture CPUs. Some of the more well-known microprocessor companies are Intel, AMD, and Motorola. If you look carefully at your computer, you might find a tag showing a logo for its microprocessor.
Each brand of microprocessor has its own unique instruction set, which is typically understood only by microprocessors of the same brand. For example, Intel micro-processors understand the same instructions, but they do not understand instructions for Motorola microprocessors.
IQ TEST FREE
The machine language instruction that was previously shown is an example of only one instruction. It takes a lot more than one instruction, however, for the computer to do anything meaningful. Because the operations that a CPU knows how to perform are so basic in nature, a meaningful task can be accomplished only if the CPU performs many operations. For example, if you want your computer to calculate the amount of inter-est that you will earn from your savings account this year, the CPU will have to perform a large number of instructions, carried out in the proper sequence. It is not unusual for a program to contain thousands or even millions of machine language instructions.
Programs are usually stored on a secondary storage device such as a disk drive. When you install a program on your computer, the program is typically copied to your computer’s disk drive from a CD-ROM, or perhaps downloaded from a website.
Although a program can be stored on a secondary storage device such as a disk drive, it has to be copied into main memory, or RAM, each time the CPU executes it. For example, suppose you have a word processing program on your computer’s disk. To execute the program you use the mouse to double-click the program’s icon. This causes the program to be copied from the disk into main memory. Then, the computer’s CPU executes the copy of the program that is in main memory. This process is illustrated in Figure 1-16.
1 The example shown is an actual instruction for an Intel microprocessor. It tells the microprocessor to move a value into the CPU.
When a CPU executes the instructions in a program, it is engaged in a process that is known as the fetch-decode-execute cycle. This cycle, which consists of three steps, is repeated for each instruction in the program. The steps are:
1. Fetch A program is a long sequence of machine language instructions. The first step of the cycle is to fetch, or read, the next instruction from memory into the CPU.
2. Decode A machine language instruction is a binary number that represents a com-mand that tells the CPU to perform an operation. In this step the CPU decodes the instruction that was just fetched from memory, to determine which operation it should perform.
3. Execute The last step in the cycle is to execute, or perform, the operation
From Machine Language to Assembly Language
Computers can only execute programs that are written in machine language. As previously mentioned, a program can have thousands or even millions of binary instructions, and writing such a program would be very tedious and time consuming. Programming in machine language would also be very difficult because putting a 0 or a 1 in the wrong place will cause an error.
Although a computer’s CPU only understands machine language, it is impractical for people to write programs in machine language. For this reason, assembly language was created in the early days of computing2 as an alternative to machine language. Instead of using binary num-bers for instructions, assembly language uses short words that are known as mnemonics. For example, in assembly language, the mnemonic add typically means to add numbers, mul typ-ically means to multiply numbers, and mov typically means to move a value to a location in memory. When a programmer uses assembly language to write a program, he or she can write short mnemonics instead of binary numbers.
N O T E : There are many different versions of assembly language. It was mentioned earlier that each brand of CPU has its own machine language instruction set. Each brand of CPU typically has its own assembly language as well.
Assembly language programs cannot be executed by the CPU, however. The CPU only understands machine language, so a special program known as an assembler is used to translate an assembly language program to a machine language program. This process is shown in Figure 1-18. The machine language program that is created by the assembler can then be executed by the CPU.
An assembler translates an assembly language program to a machine language program
Assembly language Machine language
program Program
mov eax, Z 10100001
add eax, 2 10111000
mov Y, eax Assembler
and so forth... 10011110
and so forth...
High-Level Languages
Although assembly language makes it unnecessary to write binary machine language instructions, it is not without difficulties. Assembly language is primarily a direct substitute for machine language, and like machine language, it requires that you know a lot about the CPU. Assembly language also requires that you write a large number of instructions for even the simplest program. Because assembly language is so close in nature to machine lan-guage, it is referred to as a low-level language.
In the 1950s, a new generation of programming languages known as high-level languages began to appear. A high-level language allows you to create powerful and complex programs without knowing how the CPU works, and without writing large numbers of low-level instructions. In addition, most high-level languages use words that are easy to understand. For example, if a programmer were using COBOL (which was one of the early high-level
languages created in the 1950s), he or she would write the following instruction to display the message Hello world on the computer screen:
DISPLAY "Hello world"
Python is a modern, high-level programming language that we will use in this book. In Python you would display the message Hello world with the following instruction:
print 'Hello world'
Doing the same thing in assembly language would require several instructions, and an intimate knowledge of how the CPU interacts with the computer’s output device. As you can see from this example, high-level languages allow programmers to concentrate on the tasks they want to per-form with their programs rather than the details of how the CPU will execute those programs.
Since the 1950s, thousands of high-level languages have been created. Table 1-1 lists several of the more well-known languages.
Language Description
Ada Ada was created in the 1970s, primarily for applications used by the U.S.
Department of Defense. The language is named in honor of Countess Ada
Lovelace, an influential and historic figure in the field of computing.
BASIC Beginners All-purpose Symbolic Instruction Code is a general-purpose language
that was originally designed in the early 1960s to be simple enough for begin-
ners to learn. Today, there are many different versions of BASIC.
FORTRAN FORmula TRANslator was the first high-level programming language. It was
designed in the 1950s for performing complex mathematical calculations.
COBOL Common Business-Oriented Language was created in the 1950s, and was
designed for business applications.
Pascal Pascal was created in 1970, and was originally designed for teaching program-
ming. The language was named in honor of the mathematician, physicist, and
philosopher Blaise Pascal.
C and C++ C and C++ (pronounced “c plus plus”) are powerful, general-purpose lan-
guages developed at Bell Laboratories. The C language was created in 1972
and the C++ language was created in 1983.
C# Pronounced “c sharp.” This language was created by Microsoft around the
year 2000 for developing applications based on the Microsoft .NET platform.
Java Java was created by Sun Microsystems in the early 1990s. It can be used to develop
programs that run on a single computer or over the Internet from a web server.
JavaScript JavaScript, created in the 1990s, can be used in web pages. Despite its name,
JavaScript is not related to Java.
Python Python, the language we use in this book, is a general-purpose language created
in the early 1990s. It has become popular in business and academic applications.
Ruby Ruby is a general-purpose language that was created in the 1990s. It is increas-
ingly becoming a popular language for programs that run on web servers.
Visual Basic Visual Basic (commonly known as VB) is a Microsoft programming language and
software development environment that allows programmers to create Windows-
based applications quickly. VB was originally created in the early 1990s.














