1. Home
  2. Explore
  3. Computer Science
  4. CPU and Memory Basics

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

CPU and Memory Basics

How a CPU Communicates with Memory and Devices

Every computing device—whether a personal computer, smartphone, or programmable calculator—depends on a central processing unit, or CPU. The CPU is the part of the system responsible for executing programs. To understand how a computer works, it is useful to follow the path of a single instruction from memory to the CPU and then examine how the CPU communicates with memory and external devices.

The CPU and Program Execution

A program is a sequence of instructions, also called machine code. These instructions tell the CPU what operations to perform, such as adding numbers, moving data, reading memory, or writing data to a device.

When power is applied, the CPU begins looking for executable code in external memory. It reads the program sequentially, much like a person reading a book from beginning to end. The CPU reads memory one byte at a time, interpreting each byte according to the instruction set built into the processor.

As long as the system remains powered, the CPU continues performing this process. It repeatedly:

  1. Obtains an instruction from memory.
  2. Interprets the instruction.
  3. Performs the requested operation.
  4. Moves to the next instruction.

The CPU does not inherently “finish” a program and stop permanently. Instead, it continuously executes instructions. A program may contain a loop, wait for input, or deliberately halt the processor, but the CPU itself is designed to keep operating while power is available.

Memory Cells, Bytes, and Addresses

Computer memory can be viewed as a long sequence, or array, of numerical storage locations. Each location is called a memory cell and stores an integer value.

In the system described here, each cell stores one byte. A byte contains 888 binary digits, or bits, and can represent values from 000 through 255255255:

0≤byte value≤2550 \leq \text{byte value} \leq 2550≤byte value≤255

Each memory cell has a unique number called its address. Addressing begins at zero rather than one. Therefore:

  • The first cell has address 000.
  • The second cell has address 111.
  • The cell numbered 300300300 in ordinary counting has address 299299299.

This zero-based system is common in programming and hardware design. The address is not the value stored in a cell; it is the location used to identify that cell.

A useful analogy is a row of mailboxes. The contents of a mailbox are the stored data, while the number printed on the mailbox is its address. The CPU uses the address to identify which location it wants to access.

An address identifies a memory location; the data stored at that location is a separate value.

ROM and RAM

There are two fundamental types of memory in the system:

Read-Only Memory

Read-only memory, or ROM, contains data that the CPU can read but generally cannot change during normal operation. ROM commonly stores permanent or semi-permanent program code.

ROM is useful for storing code that must be available whenever the computer starts. For example, a computer’s BIOS is stored in ROM. The BIOS is a small program that runs during system startup and helps begin the boot process.

ROM is nonvolatile, meaning that it retains its contents when electrical power is removed.

Random-Access Memory

Random-access memory, or RAM, can be both read and written. It commonly stores:

  • Temporary data created by the CPU.
  • Program code loaded from external storage.
  • Intermediate results produced during calculations.

RAM is volatile. Its contents disappear when power is interrupted. The removable memory modules installed in a computer, often called DIMM sticks, are examples of RAM.

The name “random access” means that the CPU can directly access any memory location using its address. The CPU does not have to read every earlier cell before reaching a later one.

Memory typeCan be read?Can be written?Retains data without power?
ROMYesNormally noYes
RAMYesYesNo

Firmware

A program stored in ROM is often called firmware. Firmware is software closely associated with a particular hardware device. Unlike a general-purpose application, firmware is designed to operate the specific device in which it is installed.

When power is applied, the CPU immediately seeks instructions from the firmware. This allows the device to begin operating before an ordinary operating system or application has been loaded.

Think About It

Why is ROM suitable for startup instructions, while RAM is suitable for temporary calculations? The key difference is whether the information must survive when power is removed.

The Three System Buses

The CPU communicates with memory and peripheral devices through groups of electrical connections called buses. A bus is not necessarily one wire; it is usually a collection of wires that carry related signals.

The three major buses are:

  1. The address bus.
  2. The control bus.
  3. The data bus.

Together, these buses allow the CPU to identify a location, specify an operation, and transfer information.

The Address Bus

The address bus carries the address of the memory cell or device that the CPU wants to access.

Each address line carries one binary digit, either logic 000 or logic 111. If a processor has 161616 address lines, those lines can represent 161616-bit address values.

For example, the decimal address 442064420644206 can be converted into a binary number containing 161616 bits. The CPU places those binary bits onto the address lines so that the memory system can determine which location is being selected.

The address bits have different significance:

  • The least significant bit, or LSB, has the smallest place value and is connected to the lowest-numbered address line, such as A0A0A0.
  • The most significant bit, or MSB, has the largest place value and is connected to the highest-numbered address line, such as A15A15A15 in a 161616-line system.

Address lines are numbered starting at zero, just like memory addresses.

Why the Address Bus Is Unidirectional

The address bus is unidirectional. Addresses travel from the CPU to memory or peripheral devices, not in the reverse direction.

The CPU is the device deciding which address it wants to access. Memory chips receive the address and respond to it. Therefore:

  • CPU address pins act as outputs.
  • Memory-chip address pins act as inputs.

Multiple devices can be connected to the same address bus in parallel. However, this creates a problem: every connected device receives the address. A separate mechanism is needed to determine which device should respond. That mechanism is address decoding.

The Control Bus

The control bus carries signals that describe the operation the CPU wants to perform. The two most important memory-operation signals are:

  • READ, often abbreviated as RDRDRD.
  • WRITE, often abbreviated as WRWRWR.

These signals tell memory whether the CPU wants to receive data or send data.

Active-Low Signals

The READ and WRITE signals are active low. This means that a signal is considered active when its electrical level is logic 000, rather than logic 111.

Therefore:

  • A low READ signal means that the CPU is requesting a read.
  • A low WRITE signal means that the CPU is requesting a write.
  • A high signal means that the operation is inactive.

The two operations are mutually exclusive in this system. The CPU does not read and write through the same memory operation at the same time.

Active-low control signals are traditionally used partly because of power-consumption considerations and the electrical behavior of digital circuits.

Example of a Read Operation

Suppose the CPU wants to read the byte stored at address 442064420644206. It performs the following actions:

  1. It places the binary representation of 442064420644206 on the address bus.
  2. It pulls the READ line low.
  3. The selected memory chip recognizes the request.
  4. The memory chip places the stored byte on the data bus.
  5. The CPU receives the byte.

The CPU uses the READ signal even when accessing ROM. Although ROM cannot be written, the CPU does not need to know the physical type of memory in advance. It uses the standard READ operation for both ROM and RAM. The ROM chip uses the signal to determine when it should place its stored data on the data bus.

The Data Bus

The data bus carries the actual values being transferred between the CPU and another device. Unlike the address bus, the data bus is bidirectional:

  • During a read, data travels from memory to the CPU.
  • During a write, data travels from the CPU to memory.

The width of the data bus indicates how many bits can be transferred during one operation. In the example system, the data bus is 888 bits wide. Because one byte is 888 bits, one memory transfer carries one byte.

The width of the data bus is an important part of how a computer is described. An 888-bit computer has an 888-bit data path in the relevant system. A 161616-bit system transfers 161616 bits at a time, while a 323232-bit or 646464-bit system has a wider data path in the relevant architecture.

A wider data bus can transfer more information in one operation, which generally improves the potential performance of the system. Performance also depends on other factors, such as clock speed and processor design.

Example Data Transfer

If the memory cell at address 442064420644206 contains decimal value 112112112, the memory chip places the binary representation of 112112112 on the 888-bit data bus.

During this read:

  • The ROM’s data pins act as outputs.
  • The CPU’s data pins act as inputs.
  • The CPU receives one byte whose value is 112112112.

That byte might represent an instruction rather than ordinary data. If it is an instruction, the CPU interprets it as an opcode.

Instructions and Opcodes

An opcode, short for operation code, is a machine-code value that tells the CPU what operation to perform. The numerical value of an opcode has meaning because the CPU’s instruction set defines what each value represents.

For example, a byte read from ROM might instruct the CPU to write the value 205205205 to the memory cell at address 620886208862088. The byte itself is not inherently meaningful as a decimal number; it becomes meaningful when interpreted according to the processor’s instruction set.

This distinction is important:

  • A byte can be ordinary data.
  • A byte can be an instruction.
  • The CPU determines how to interpret it based on the current execution context and instruction set.

Think About It

Why can the same binary pattern represent either data or an instruction? The bits themselves do not carry labels. Their meaning comes from how the CPU is instructed to interpret them.

Connecting Multiple Memory Chips

Suppose a system contains both a ROM chip and a RAM chip. Both chips can be connected in parallel to the address and data buses.

This arrangement is efficient because both chips receive the same address signals. However, it creates a serious problem during reads. If both chips respond at the same time, both may attempt to drive different values onto the data bus. This produces a bus collision, in which the devices electrically interfere with one another.

The solution is address decoding.

Address Decoding

Address decoding is the process of examining address bits and generating a device-selection signal for a particular address range. It is implemented with logic gates or decoder circuits.

The term “decoding” here does not mean decrypting secret information. It means recognizing a particular pattern of binary values.

For example, a decoder might activate a device only when certain address lines have a particular combination of zeros and ones. Each memory device is assigned a range of addresses, and the decoder activates that device only when the CPU requests an address within that range.

A computer’s address space can be compared to a book divided into chapters. Each chapter occupies a fixed range of pages. In the same way, each memory chip occupies a fixed range of addresses.

Dividing a 161616-Bit Address Space

A CPU with 161616 address lines can represent:

216=65,5362^{16} = 65{,}536216=65,536

different addresses.

Because each address identifies one byte, the total addressable memory is 65,53665{,}53665,536 bytes, or 646464 kilobytes.

The valid addresses range from:

0 through 65,5350 \text{ through } 65{,}5350 through 65,535

The final address is 65,53565{,}53565,535 rather than 65,53665{,}53665,536 because counting begins at zero.

If the address space is divided equally between ROM and RAM:

  • ROM occupies addresses 000 through 32,76732{,}76732,767.
  • RAM occupies addresses 32,76832{,}76832,768 through 65,53565{,}53565,535.

Each device receives:

32,768=32 kilobytes32{,}768 = 32 \text{ kilobytes}32,768=32 kilobytes

The ROM is placed at the lower end because the CPU typically begins execution at address 000 after reset. The first firmware instruction must therefore be located at the beginning of the ROM address range.

Hexadecimal Representation

Binary is the actual electrical language of digital hardware, but long binary strings are difficult for humans to read. Programmers therefore commonly use the hexadecimal number system.

Hexadecimal uses 161616 symbols:

0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

The letters represent values from 101010 through 151515.

Hexadecimal is convenient because one hexadecimal digit corresponds exactly to four binary bits:

BinaryHexadecimal
000000000000000
000100010001111
001000100010222
…\dots……\dots…
111111111111FFF

This means:

  • 444 bits require 111 hexadecimal digit.
  • 888 bits require 222 hexadecimal digits.
  • 161616 bits require 444 hexadecimal digits.

A group of 444 bits is called a nibble. A group of 888 bits is a byte. A group of 161616 bits or more is commonly called a word, depending on the architecture.

The 161616-bit address ranges become easier to express in hexadecimal:

  • ROM: 000000000000 through 7FFF7FFF7FFF.
  • RAM: 800080008000 through FFFFFFFFFFFF.

Hexadecimal numbers must be pronounced digit by digit. For example, 800080008000 in hexadecimal is not read as “eight thousand,” because it represents a different numerical value from decimal 8,0008{,}0008,000.

Common notation includes:

  • C-language style: 0x80000x80000x8000.
  • Assembly-language style: 8000H8000H8000H.

Both forms indicate that the number is hexadecimal.

Using the Most Significant Address Bit for Decoding

The boundary between ROM and RAM in this example is especially convenient because it can be identified by the most significant address bit, A15A15A15.

For addresses below 0x80000x80000x8000:

A15=0A15 = 0A15=0

For addresses from 0x80000x80000x8000 through 0xFFFF0xFFFF0xFFFF:

A15=1A15 = 1A15=1

Therefore:

  • ROM can be selected when A15A15A15 is low.
  • RAM can be selected when A15A15A15 is high.

A simple inverter can generate the required complementary signal. One chip receives the address bit directly, while the other receives the inverted version.

Chip Select and Output Enable

Memory chips have a control input called Chip Select, or CS. It may also be called Chip Enable, or CE. In this system, CS or CE is active low.

A memory chip responds only when its chip-select input is active. If it is not selected, its output pins enter a high-impedance state, often written as the Z-state.

High impedance means that the output behaves as though it is electrically disconnected from the bus. This prevents inactive devices from interfering with the device that is currently responding.

These outputs are also called tri-state outputs because they can represent:

  1. Logic 000.
  2. Logic 111.
  3. High impedance, or disconnected.

Memory chips also use an Output Enable, or OE, signal. For ROM, CS and OE have closely related roles because ROM only sends data outward. For RAM:

  • Active OE allows the RAM to place data on the data bus during a read.
  • A separate write-enable signal allows the RAM to store incoming data.

The simplified wiring is therefore:

  • The ROM chip is selected for the lower address range.
  • The RAM chip is selected for the upper address range.
  • READ connects to the output-enable inputs.
  • WRITE connects to RAM’s write-enable input.
  • ROM does not use WRITE because it cannot be written during normal operation.

Reset and the First Instruction

The CPU contains a program counter, or PC, that holds the address of the next instruction to execute.

When the CPU receives a RESET signal, the program counter is set to zero:

PC=0PC = 0PC=0

The CPU then places address 000 on the address bus and activates READ. Because address 000 belongs to the ROM range, the ROM chip is selected and supplies the first firmware instruction.

The RAM chip remains inactive because its chip-select signal is not active. Its outputs stay in the high-impedance state, so RAM does not interfere with ROM’s data.

This simple sequence explains why startup ROM is usually located at the beginning of the address space: reset directs the CPU to address 000, where the initial firmware must be available.

A Complete RAM Write and Read

Consider a CPU instruction that writes the value 0xAA0xAA0xAA to address 0xB0000xB0000xB000.

The value 0xAA0xAA0xAA is decimal 170170170 and binary:

0xAA=1010101020xAA = 10101010_20xAA=101010102​

The CPU performs the write in several stages:

  1. It places address 0xB0000xB0000xB000 on the address bus.
  2. Because 0xB0000xB0000xB000 is above 0x80000x80000x8000, the highest address bit A15A15A15 is high.
  3. The decoder activates the RAM chip.
  4. The CPU places 0xAA0xAA0xAA on the data bus.
  5. The CPU pulls WRITE low.
  6. RAM stores 0xAA0xAA0xAA in address 0xB0000xB0000xB000.

The patterns 0xAA0xAA0xAA and 0x550x550x55 are often used in memory testing because their alternating binary patterns make it easier to detect certain wiring or storage errors.

To read the value back, the CPU:

  1. Places 0xB0000xB0000xB000 on the address bus.
  2. Activates READ by pulling the READ line low.
  3. Causes the RAM chip to place the stored value on the data bus.
  4. Switches its data lines to input mode.
  5. Receives 0xAA0xAA0xAA.

The CPU does not need to understand that one physical chip is ROM and another is RAM. It simply requests an address and an operation. The decoding circuitry ensures that the correct physical device responds.

Think About It

What would happen if both ROM and RAM were active during the same read? Both chips could drive the data bus simultaneously, creating a bus collision. Chip selection prevents this conflict.

Memory-Mapped Video RAM

Address decoding can assign address ranges not only to ordinary RAM and ROM but also to special-purpose devices.

One example is video RAM, or VRAM. VRAM is memory whose contents affect what appears on a display. Writing a value to a VRAM location changes the corresponding screen information.

Typically, VRAM addresses correspond to screen positions:

  • Lower addresses represent positions nearer the upper-left corner.
  • Increasing addresses move across the screen and then downward.
  • Higher addresses generally represent positions nearer the lower-right corner.

The exact arrangement can vary, but the main idea is that a memory write can produce a visible change on the display. This is an example of memory-mapped hardware: the CPU communicates with a device by reading or writing addresses assigned to that device.

Input-Output Ports

A computer must communicate with more than memory. It also needs to interact with devices such as:

  • Keyboards.
  • Mice.
  • Gamepads.
  • Displays.
  • Storage controllers.
  • Other expansion hardware.

These devices communicate through input-output ports, often called I/O ports.

The CPU uses read and write operations for ports just as it does for memory. However, ports occupy a separate address space and are accessed using separate machine-language instructions.

The same physical address bus can be used for both memory addresses and port addresses. The system distinguishes the two kinds of requests with additional control-bus signals:

  • IOREQ, meaning input-output request.
  • MEMREQ, meaning memory request.

These signals are traditionally active low. A memory device should respond only when MEMREQ is active, while an I/O device should respond only when IOREQ is active.

Without this distinction, a memory location and an I/O port with the same numerical address could both react to the same CPU operation.

Building a One-Bit Output Port

A simple output port can be used to control an LED. The port can be implemented with a D-type flip-flop.

A D-type flip-flop stores one input bit when it receives an appropriate clock signal. Its stored output remains stable until another clock event updates it.

In the example:

  • The flip-flop’s data input is connected to the least significant data-bus line, D0D0D0.
  • The flip-flop is assigned port address 0xFF0xFF0xFF.
  • The flip-flop’s clock input is generated by decoding the appropriate control signals.
  • The stored bit controls the LED.

The port address 0xFF0xFF0xFF is binary:

0xFF=1111111120xFF = 11111111_20xFF=111111112​

An 888-input AND gate can detect whether all address lines A0A0A0 through A7A7A7 are high. The circuit must also detect:

  • WRITE is active.
  • IOREQ is active.

These conditions ensure that the flip-flop responds only to a write to I/O port 0xFF0xFF0xFF, not to a write to a memory location with the same numeric address.

Turning the LED On

To turn the LED on, the CPU performs an instruction equivalent to writing the value 111 to port 0xFF0xFF0xFF:

  1. The CPU places 0xFF0xFF0xFF on the address bus.
  2. It places logic 111 on data line D0D0D0.
  3. It activates WRITE by pulling WRITE low.
  4. It activates IOREQ by pulling IOREQ low.
  5. The decoder generates the flip-flop’s clock signal.
  6. The flip-flop stores the 111.
  7. The LED illuminates.

Turning the LED Off

To turn the LED off, the CPU performs the same operation but writes 000 to the port:

  1. The address remains 0xFF0xFF0xFF.
  2. The CPU places logic 000 on D0D0D0.
  3. WRITE and IOREQ are activated.
  4. The flip-flop stores 000.
  5. The LED turns off.

This example demonstrates how a processor can control physical hardware using ordinary binary writes. The CPU does not need a special “LED wire.” It writes to a decoded address, and the hardware interprets that write as a control command.

Expansion Buses and Device Decoding

The same principle applies to expansion systems. Multiple devices can share a communication bus, but each device must recognize only the addresses and control signals assigned to it.

Older systems such as ISA and PCI used parallel connections in which expansion slots shared bus connections. Each expansion card contained decoding circuitry that determined when the card should respond.

Configuring an older ISA sound card often involved selecting an address and an interrupt line. Selecting the address effectively configured the card’s decoder so that the CPU could communicate with it at the chosen location.

Beginning with PCI Express, the architecture moved away from this fully shared parallel-bus arrangement. Each motherboard slot has independent communication lines associated with the installed expansion device. This reduces the need for all devices to share the same physical communication paths.

Comprehensive Review

The operation of a simple computer can now be understood as a coordinated sequence:

  1. The CPU begins execution after reset, typically with the program counter set to address 000.
  2. The CPU places an address on the unidirectional address bus.
  3. Address-decoding circuitry determines which memory chip or device owns that address.
  4. The CPU uses the control bus to indicate a read or write operation.
  5. During a read, the selected device places data on the bidirectional data bus.
  6. During a write, the CPU places data on the data bus and the selected device stores or acts on it.
  7. Inactive devices place their outputs in the high-impedance state so they do not interfere.
  8. ROM provides persistent startup code, while RAM stores temporary information.
  9. I/O devices use decoded ports and control signals such as IOREQ so that they remain distinct from memory.
  10. Peripheral hardware can respond to writes by storing values in flip-flops or by performing physical actions such as illuminating an LED.

The central idea is that a CPU communicates with the outside world through organized signals: the address bus identifies the destination, the control bus identifies the operation, and the data bus carries the information. Address decoding selects the correct device, while tri-state outputs ensure that only the selected device drives the shared data bus. Together, these mechanisms form the foundation of how processors communicate with memory and peripherals in computer systems.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent