Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 4 — Modes of Transfer: Programmed I/O, Interrupt-Driven I/O and DMA

Lesson 41 of 49 in the free Computer Organization and Architecture notes on Siksha Sarovar, written by Rohit Jangra.

Three Modes of Transfer

Data between memory and an I/O device can be transferred in three ways, differing in who supervises the transfer and what the CPU does meanwhile.

1. Programmed I/O

   The CPU executes a program that:
      1. reads the device STATUS register,
      2. tests the ready/flag bit,
      3. loops back if not ready  (this loop is called POLLING or BUSY WAITING),
      4. transfers one word when ready,
      5. repeats for the next word.
   Typical loop:

   LOOP:  IN    STATUS        ; read the status register
          AND   #01H          ; mask the "ready" bit
          BZ    LOOP          ; not ready -> poll again
          IN    DATA          ; ready -> read the data byte
          STA   BUFFER        ; store it in memory
          INC   POINTER
          DEC   COUNT
          BNZ   LOOP          ; more bytes to go

The flag mechanism

   1. The interface sets its FLAG bit when the device has data ready.
   2. The CPU keeps reading the status register until it sees flag = 1.
   3. The CPU reads the data register — reading it CLEARS the flag.
   4. The interface can now accept the next item from the device.

The cost

   Q: A keyboard delivers 10 characters per second. The CPU runs at
      1 GHz and the polling loop takes 5 instructions (5 ns).

      Time available per character = 100 ms
      Time actually needed         = 5 ns
      CPU utilisation for useful work = 5ns / 100ms = 0.000005 %

      The CPU spends 99.999995% of its time in the polling loop
      doing NOTHING.
Verdict: programmed I/O is simple and needs no extra hardware, but it wastes the CPU almost entirely. It is used only in small embedded systems, in bootloaders, and where the device is nearly as fast as the CPU.

2. Interrupt-Driven I/O

   Instead of the CPU asking "are you ready?", the DEVICE says
   "I am ready" by asserting an INTERRUPT REQUEST line.

   The CPU:
      - runs other programs while the device is busy,
      - is interrupted when the device becomes ready,
      - suspends the current program, saves its state,
      - executes an INTERRUPT SERVICE ROUTINE (ISR) that transfers the word,
      - restores the saved state and resumes the interrupted program.

The interrupt cycle

   1. Device asserts the interrupt request line.
   2. CPU finishes the CURRENT INSTRUCTION (interrupts are checked at
      instruction boundaries, never mid-instruction).
   3. CPU saves PC and the status flags (usually on the stack).
   4. CPU disables further interrupts (IEN <- 0).
   5. CPU determines WHICH device interrupted (polling, daisy chain
      or vectored interrupt).
   6. CPU branches to the ISR address.
   7. ISR transfers the data and clears the device flag.
   8. ISR ends with a RETURN FROM INTERRUPT that restores PC and flags
      and re-enables interrupts.

Identifying the interrupting device

MethodHow it worksSpeed
Software pollingThe ISR reads each device's status in priority orderSlow (n reads)
Daisy chainingHardware priority chain; the winner places its vector on the busFast
Parallel priorityA priority encoder + mask register produces the vectorFastest
Vectored interruptThe device supplies its own ISR address directlyFastest

Types of interrupt

   EXTERNAL   : from I/O devices, timers, power failure
   INTERNAL   (traps/exceptions): divide by zero, invalid opcode,
                stack overflow, page fault — caused by the program itself
   SOFTWARE   : deliberately caused by an instruction (INT n, SVC, syscall)
                — the standard mechanism for system calls

   MASKABLE   : can be disabled by the CPU (most device interrupts)
   NON-MASKABLE (NMI): cannot be disabled — power failure, hardware fault

3. Direct Memory Access (DMA)

   In BOTH previous modes the data passes THROUGH the CPU:

      Device -> CPU register -> Memory        (two bus transfers per word)

   DMA removes the CPU from the path entirely:

      Device -> Memory                        (one bus transfer per word)

   A DMA CONTROLLER takes over the buses and performs the transfer.

The next lesson covers DMA in full. In this comparison, note only the essentials:

   1. CPU programs the DMA controller: start address, word count, direction.
   2. CPU resumes other work.
   3. DMA controller requests the bus (BUS REQUEST / HOLD).
   4. CPU relinquishes the bus (BUS GRANT / HLDA) and floats its bus drivers.
   5. DMA controller transfers the whole block directly to/from memory.
   6. DMA controller interrupts the CPU when the block is complete.

4. Complete Comparison

BasisProgrammed I/OInterrupt-driven I/ODMA
Who initiatesCPU (polls)Device (interrupts)Device, via the DMA controller
CPU involvementTotal — every word, plus waitingPer word, but no waitingOnly at start and end of the block
CPU idle timeEnormous (busy waiting)NoneNone
Data pathDevice → CPU → MemoryDevice → CPU → MemoryDevice → Memory
Transfers per word2 bus cycles2 bus cycles1 bus cycle
SpeedSlowestMediumFastest
Extra hardwareNoneInterrupt logicDMA controller
Overhead per wordPolling loopContext save/restoreNone
Best forVery slow or very simple devicesModerate-rate devices; unpredictable timingHigh-speed block devices (disk, network, graphics)
ExampleReading a switchKeyboard, mouse, UARTDisk, SSD, NIC, sound card

5. Which Mode to Choose

   Device is slower than the CPU by a huge factor,
   and transfers are single bytes at unpredictable times
        ->  INTERRUPT-DRIVEN I/O

   Device transfers LARGE BLOCKS at high speed
        ->  DMA

   Device is simple, the system is tiny, or you are in a boot loader
   where interrupts are not yet set up
        ->  PROGRAMMED I/O

6. Worked Comparison Numerical

   A disk transfers 4 KB blocks at 4 MB/s. The CPU runs at 1 GHz.
   An interrupt costs 500 cycles of overhead; a programmed-I/O
   transfer of one word costs 20 cycles.

   PROGRAMMED I/O (4-byte words):
      Words per block = 4096 / 4 = 1024
      Cycles = 1024 x 20 = 20,480 cycles per block, PLUS the CPU
      must poll for the entire transfer duration (1 ms) = 1,000,000 cycles.
      -> essentially 100% CPU usage.

   INTERRUPT-DRIVEN (one interrupt per word):
      1024 interrupts x 500 cycles = 512,000 cycles per block = 51% CPU.

   DMA (one interrupt per block):
      1 interrupt x 500 cycles = 500 cycles per block = 0.05% CPU.

This is why every modern high-speed device uses DMA. The next lesson looks at how the CPU sorts out which interrupt to service first when several arrive at once.