Computer Memory
Binary numbers use only 0 and 1.
Why Everything Becomes Binary
A computer ultimately works with information represented using two possible digits: zero and one. Before reading further, consider a question about what a computer keeps in its memory.
What do you think happens?
Does a computer store only instructions, or only numbers, in its memory?
Reveal answer
Both, and both are represented as binary numbers stored as groups of bits.
The important idea is not that a computer stores only instructions or only numbers. Both instructions and data are represented as binary numbers and stored in memory as groups of bits. This applies both to the instructions a CPU can execute directly and to the data the computer manipulates.
This article follows one instruction from a pattern of zeros and ones into a set of consecutive memory locations, and then shows how the CPU refers to it by address.
Bits, Bytes, and Binary Digits
Binary number: a number written using only the two digits 0 and 1.
Bit: one binary digit, which is either 0 or 1.
Byte: a group of eight bits.
Bits are grouped into bytes, and the byte is the unit that matters for memory. Modern computer memory is organized into locations, with each location holding one byte, or eight bits. The diagram below shows how the eight individual bits of one byte make up that single unit.
Counting With Only Two Digits
Binary uses only two symbols, while decimal uses ten, the digits 0 through 9. Because a binary digit can take only two values, each added bit doubles the number of distinct patterns that can be written. The worked example below makes this concrete.
Listing every pattern of two bits
Using only the digits 0 and 1, list every distinct pattern that two bits can hold.
- 1One bit
A single bit can be 0 or 1, which gives two patterns.
- 2Add a second bit
Each of the two first-bit patterns can be followed by either 0 or 1, producing 00, 01, 10, and 11.
- 3Count the results
Two bits give four distinct patterns, so the number of patterns doubles with each added bit.
Two bits can hold four distinct patterns: 00, 01, 10, and 11.
How Instructions Become Binary Patterns
Machine language is the form of instruction that a computer's CPU can execute directly. A machine language instruction is represented by a sequence of zeros and ones. The particular sequence matters: each particular sequence encodes a particular instruction.
This means the binary pattern is not merely an ordinary piece of data from the CPU's point of view. It can represent an operation for the CPU to execute. Two different sequences of bits encode two different instructions, and the CPU recognizes each sequence as the instruction it stands for.
- 1An instruction is represented by a sequence of bits.
- 2Those bits are grouped into bytes.
- 3The bytes are placed in consecutive memory locations.
- 4When the CPU reads the instruction, it reads the relevant bytes from memory, and the location of the first byte supplies the instruction's memory address.
An instruction and a number are both bit patterns. What distinguishes an instruction is that the CPU is designed to execute the specific pattern as an operation.
Tracing Bytes Through Memory
Now trace a concrete layout. Suppose an instruction takes four bytes, and it is stored in four consecutive locations. Data that the program uses is stored right after it in two more locations. The locations are numbered here only for illustration.
Read the diagram from left to right. The instruction occupies Locations 1 through 4, and the data follows in Locations 5 and 6. Data and instructions generally occupy several consecutive byte-sized memory locations, and neither is treated as a single indivisible location.
Why an Instruction Is Addressed by Its First Byte
When a CPU reads an instruction, it may read four or eight bytes from four or eight memory locations. The rule for addressing does not change because of this. The instruction's address is the address of the first byte among the bytes that make up that instruction.
Mistakes Beginners Make With Memory Layout
Common mistake Treating a multi-byte instruction as if it occupied a single memory location
What it looks likeAssuming a four-byte instruction sits in one slot with a single size of one byte.
Why it failsEach memory location holds one byte, and a complete instruction generally needs several bytes.
Do this insteadThink of the instruction as occupying several consecutive locations, one byte each.
Common mistake Assuming the instruction's address is the address of its last byte
What it looks likeFor an instruction in Locations 1 through 4, concluding that the instruction is addressed as Location 4.
Why it failsThe instruction's address is the address of the first byte that makes up the instruction.
Do this insteadFor an instruction in Locations 1 through 4, the instruction's address is Location 1.
Common mistake Believing that binary patterns in memory are always ordinary numbers
Why it failsA machine language instruction is a binary sequence, and each particular sequence encodes a particular instruction the CPU can execute.
Do this insteadRecognize that the same kind of bit pattern can be data or an instruction, depending on how the CPU uses it.
Practice: Locating an Instruction
A machine language instruction is four bytes long and is stored in consecutive locations 10, 11, 12, and 13. Which address names this instruction, and what do locations 11, 12, and 13 hold?
Hints
- Recall that the instruction's address is the address of its first byte.
- The remaining locations hold the other bytes that make up the same instruction.
Key Takeaways
- A binary number uses only the digits 0 and 1, a bit is one binary digit, and a byte is eight bits.
- Machine language instructions are binary sequences, and each particular sequence encodes a particular instruction the CPU can execute directly.
- Each memory location holds one byte, and data and instructions generally occupy several consecutive locations.
- A multi-byte instruction is addressed by the location of its first byte, even though the CPU reads all of its bytes.