Computer programs consist of instructions and data. As discussed in Chapter 1, instructions, written in a programming language such as C++ and then translated by the compiler and linker into machine language, give the computer step-by-step directions on what to do. The data is the information that is the subject of the program. For example, if the user of your computer program wants a list of all students with a GPA of 4.0, the data could be a list of all students and their GPAs. The program then would follow instructions to determine and output the list of all students with a GPA of 4.0.
The computer program s instructions and data have to be in the computer s memory for the program to work. This section will explain the different types of computer memory, as well as how and where instructions and data are stored in computer memory.
There are three principal memory locations on your computer.
The central processing unit (CPU)
Random access memory (RAM)
The CPU is the brains of the computer. You may have thought about the CPU when you last considered purchasing a computer, since the CPU s speed often is an important purchase consideration. The faster the CPU s speed, the faster your computer runs.
A hertz, named after Heinrich Hertz, who first detected electromagnetic waves, represents one cycle per second. CPU speed is measured in megahertz (MHz), which represents one million cycles per second, or gigahertz (GHz), which represents 1 billion cycles per second. For example, a CPU that runs at 800 MHz executes 800 million cycles per second. Each computer instruction requires a fixed number of cycles, so the CPU speed determines how many instructions per second the CPU can execute.
The CPU, in addition to coordinating the computer s operations, also has memory, called cache memory . The CPU s cache memory includes a segment called a register . This memory is used to store frequently used instructions and data.
The CPU can access cache memory extremely quickly because it doesn t have far to go; the memory is right on the CPU. However, the amount of available cache memory is quite small; there is only enough room for the most frequently used instructions and data. The remainder of the instructions and data have to be stored somewhere else.
That somewhere else is random access memory, or RAM. You may also have considered RAM when you last purchased a computer, since the more RAM a computer has, the more programs it can run at one time, and the faster it runs.
The CPU can access RAM almost as quickly as cache memory. Additionally, the amount of RAM available to store instructions and data is much larger than the amount of available cache memory.
However, RAM, like cache memory, is temporary. Instructions and data contained in main memory are lost once the computer is powered down. You may have had the unpleasant experience of losing unsaved data when your computer powered off during a power failure, or had to be rebooted.
Additionally, we would want the data to remain intact after the program ended, even if the computer is rebooted or powered off. That is not possible with RAM.
Furthermore, your computer likely has many other programs, for e-mail, Internet, word processing, and so on, that you may not be using right now, but you may want to use in the future. Likewise, your computer also may have other data files, such as term papers, letters , tax spreadsheets, e-mail messages, and so on, that you also may not be using right now, but that you may want to use in the future. Accordingly, we need another memory location, which unlike cache memory or RAM, is persistent ”that is, it will persist even though the computer is rebooted or turned off.
That other, persistent type of computer memory is called, naturally enough, persistent storage . This usually is a hard drive, but also could be, among other devices, a CD-ROM or DVD-ROM, floppy or zip disk, or optical drive. However, no matter what storage device is used, persistent storage is lasting ; instructions and data remain stored even when the computer is powered down. Thus, your computer can be turned off for months, but when it is turned on, the files you previously saved are still there.
Persistent storage, in addition to being lasting, also has a much larger capacity than RAM ”about one hundred to one thousand times larger.
Since persistent storage is lasting and has a very large capacity, it is used to store both programs and data. For example, if you installed Microsoft Word on your computer, the files for this program would be stored on your hard drive. If you then prepared documents using that program, those documents likewise would be saved as files on your hard drive.
While persistent storage has the advantages of being lasting and having a large capacity, a computer program cannot execute instructions located in persistent storage. The instructions must be loaded from persistent storage into RAM. Similarly, a computer program cannot manipulate data located in persistent storage. This data likewise must be loaded from persistent storage into RAM.
While beyond the scope of this chapter, persistent storage also can serve as a backup to RAM, and when serving this purpose is called virtual memory or swap space.
Generally, computer programs use RAM to store instructions and data, so RAM will be our focus in discussing memory. However, much of the discussion of memory also may apply to persistent storage. CPU cache memory is a different subject, discussed more in connection with programming languages, such as assembly language, that are far closer to machine language than is C++.
When someone asks where you live, you may answer 1313 Mockingbird Lane. That is your address.
Addresses are used to locate persons or places. Addresses usually follow a logical pattern. For example, the addresses on one block may be from 1300 to 1399, the next from 1400 to 1499, and so on.
Locations in memory also are identified by address. These addresses often look quite different than the street addresses we re used to, since they usually are expressed as hexadecimal (Base 16) numbers such as 0x8fc1. However, regardless of how the number is written, as shown in Figure 2-1, memory addresses follow the same logical, sequential pattern as do street addresses, one number coming after another.
Hexadecimal Numbers ”We usually use numbers that are decimal, or Base 10, in which each digit is between 0 and 9. By contrast, memory addresses usually are expressed as hexadecimal, or Base 16, in which each digit is between 1 and 15. Since 10, 11, 12, 13, 14, and 15 are not single digits, 10 is expressed as a, 11 as b, 12 as c, 13 as d, 14 as e, and 15 as f. The number 16 in decimal is expressed as 10 in hexadecimal.
Memory address numbers can be large values, and thus may be written more compactly in hexadecimal than in decimal. For example, 1,000,000 in decimal is f4240 in hexadecimal.
Converting between hexadecimal and decimal is explained next in the upcoming section, Converting Between Decimal and Binary or Hexadecimal.
While people live at street addresses, what is stored at each memory address is a byte . Don t worry, I have not misspelled Dracula s favorite pastime.
As discussed in Chapter 1, early computers essentially were a series of switches, 1 representing on, 0 representing off. In computer terminology, a bit is either a 1 or a 0.
However, while a computer may think in bits, it cannot process information as small as a single bit. Eight bits, or one byte, is the smallest unit of information that a computer can process.
Accordingly, each address may store up to one byte of information, represented by a sequence of up to eight ones and zeroes. Thus, just as a street address may be used to locate the persons who live there, a memory address can be used to locate the one byte of information that is stored there. Figure 2-2 shows a sequence of memory addresses, each with a value.
The information stored at a memory address, a series of ones and zeroes, probably has little meaning to most of us. However, to a computer, a sequence of ones and zeroes is quite meaningful.
For example, to my computer, I was born in the year 11110100000. Before you tell me that s impossible , I will tell you I was born in the year 1952. How could I have been born both in the year 11110100000 and in the year 1952?
The numbers with which we usually work are decimal, or base 10. Each number in decimal is represented by a digit between 0 and 9. 1952 is a decimal number.
The sequence of ones and zeroes in a byte also is a number, though it may not look like any number you have ever seen. My birth year, expressed as the number 11110100000, is binary, or base 2. Each number in binary is represented by a digit that is either 0 or 1.
The reason both decimal and binary numbers are involved in computer programming is because both humans and computers are involved. While humans think in decimal numbers, computers think in binary numbers.
You can write computer programs without knowing how to convert between binary and decimal numbers. However, knowing how to do so is not difficult and may help your understanding of what happens behind the scenes. If you are interested, read on!
Converting a number from binary to decimal is simple. Going from right to left, the rightmost binary digit is multiplied by 2 , or 1, the second binary digit from the right is multiplied by 2 1 , or 2, the third binary digit from the right is multiplied by 2 2 , or 4, and so on, through all of the binary digits. The results of each multiplication are added, and the result is the decimal equivalent of the binary number. Table 2-1 shows this calculation for the binary equivalents of the numbers 1 through 5 in decimal.
0 — 2 = 0 — 1 =
1 — 2 = 1 — 1 =
(0 — 2 ) + (1 — 2 1 ) = 0 + 2
(1 — 2 ) + (1 — 2 1 ) = 1 + 2
(0 — 2 ) + (0 — 2 1 ) + (1 — 2 2 ) = 0 + 0 + 4
(1 — 2 ) + (0 — 2 1 ) + (1 — 2 2 ) = 1 + 0 + 4
Converting a number from decimal to binary is almost as easy. Let s use 5 in decimal as an example.
You find the largest power of 2 that can be divided into 5 with a quotient of 1. The answer is 2 2, or 4.
Remember when converting from binary to decimal, the rightmost binary digit is multiplied by 2 , or 1, the second binary digit from the right is multiplied by 2 1 , the third binary digit from the right is multiplied by 2 2 , and so on. Since the exponent is 2, a binary 1 goes into the third binary digit from the right, so the binary number now is 1??, the ? representing each binary digit we still need to calculate.
When you divide 5 by 4, the remainder is 1. You next try to divide 1 by the next lowest power of 2, 2 1 , or 2. The quotient is 0, so a binary 0 goes into the second binary digit from the right. The binary number now is 10?.
When you divide 1 by 2, the remainder is still 1. You next try to divide 1 by the next lowest power of 2, 2 , or 1. The quotient is 1, so a binary 1 goes into the rightmost binary digit. The binary number now is 101, and we re done.
You also can use the same techniques for converting between hexadecimal and decimal. When converting from hexadecimal to decimal, multiply each hexadecimal digit (converting a to 10, b to 11, and so on) by the appropriate power of 16. For example, 5c in hexadecimal is (12 — 16 ) + (5 — 16 1 ), which is 12 + 80 or 92.
Conversely, when converting from decimal to hexadecimal, the highest power of 16 that can be divided into 92 is 16 1 , or 16. The quotient is 5, which goes into the second digit to the right. The remainder is 12, which is c in hexadecimal. This goes into the rightmost digit, resulting in the hexadecimalnumber 5c .