Concepts / Computer Memory

Computer Memory

Binary numbers use only 0 and 1.

  • Programming

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?

  • Only instructions

  • Only numbers

  • Both, and both are represented as binary numbers stored as groups of bits

Reveal answer
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

Definition

Binary number: a number written using only the two digits 0 and 1.

Definition

Bit: one binary digit, which is either 0 or 1.

Definition

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.

Byteone memory locationBit 10 or 1Bit 20 or 1Bit 30 or 1Bit 40 or 1Bit 50 or 1Bit 60 or 1Bit 70 or 1Bit 80 or 1containscontainscontainscontainscontainscontainscontainscontains
How do individual bits (0 or 1) combine to form a byte, and how many bits does each byte contain?

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.

Worked example

Listing every pattern of two bits

Using only the digits 0 and 1, list every distinct pattern that two bits can hold.

  1. 1
    One bit

    A single bit can be 0 or 1, which gives two patterns.

  2. 2
    Add a second bit

    Each of the two first-bit patterns can be followed by either 0 or 1, producing 00, 01, 10, and 11.

  3. 3
    Count the results

    Two bits give four distinct patterns, so the number of patterns doubles with each added bit.

Result

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.

  1. 1
    An instruction is represented by a sequence of bits.
  2. 2
    Those bits are grouped into bytes.
  3. 3
    The bytes are placed in consecutive memory locations.
  4. 4
    When 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.
Key point

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.

Location 1instruction byte 1Location 2instruction byte 2Location 3instruction byte 3Location 4instruction byte 4Location 5data byte 1Location 6data byte 2followed byfollowed byfollowed byfollowed byfollowed by
How are a program's instructions and data laid out in consecutive memory locations, and which location comes after which?

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.

Location 1first byte, names theinstructionLocation 2second byte of instructionLocation 3third byte of instructionLocation 4fourth byte of instructioncontinuescontinuescontinues
When an instruction spans several bytes, which byte's address names the whole instruction, and what do the remaining bytes' addresses correspond to?

Mistakes Beginners Make With Memory Layout

  • Common mistake

    Treating a multi-byte instruction as if it occupied a single memory location

    What it looks like

    Assuming a four-byte instruction sits in one slot with a single size of one byte.

    Why it fails

    Each memory location holds one byte, and a complete instruction generally needs several bytes.

    Do this instead

    Think 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 like

    For an instruction in Locations 1 through 4, concluding that the instruction is addressed as Location 4.

    Why it fails

    The instruction's address is the address of the first byte that makes up the instruction.

    Do this instead

    For 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 fails

    A machine language instruction is a binary sequence, and each particular sequence encodes a particular instruction the CPU can execute.

    Do this instead

    Recognize that the same kind of bit pattern can be data or an instruction, depending on how the CPU uses it.

Practice: Locating an Instruction

EASY

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.