Digital Logic Foundations
The gates that build everything from adders to processors
Open interactive version (quiz + challenge)Real-world analogy
What is it?
Digital logic is the foundation of all digital electronics. Using simple logic gates (AND, OR, NOT, XOR) that make binary decisions, engineers build adders for arithmetic, flip-flops for memory, and control circuits for instruction decoding. Every microprocessor, including the Intel 8086, is constructed entirely from these basic logic elements organized into an Arithmetic Logic Unit (ALU), registers, and a control unit synchronized by a clock signal.
Real-world relevance
When your 8086-based computer executes ADD AX, BX, the instruction decoder activates the ALU's addition circuit -- a chain of 16 full adders. Each adder is built from AND, OR, and XOR gates. The carry ripples from bit 0 to bit 15, and the final result is captured in AX's flip-flops on the next clock edge. Status flags (Zero, Carry, Overflow, Sign) are set by additional gate circuits monitoring the result. This entire operation takes about 3 clock cycles -- 600 nanoseconds at 5 MHz.
Key points
- AND, OR, NOT: The Building Blocks — Every digital circuit is built from three fundamental gates. AND outputs 1 only when all inputs are 1. OR outputs 1 when at least one input is 1. NOT (inverter) flips the bit. From these three, you can build any logic function. NAND and NOR are 'universal gates' -- you can build any other gate using only NAND or only NOR.
- XOR: The Difference Detector — XOR (exclusive OR) outputs 1 when inputs differ. It is essential for comparisons, parity checking, and encryption. XOR-ing a value with itself always gives zero -- this is the fastest way to clear a register on the 8086. XOR is also used in checksums and simple ciphers.
- Truth Tables — A truth table lists every possible input combination and the resulting output. For 2 inputs, there are 4 rows. For 3 inputs, 8 rows. Truth tables are the specification from which circuits are designed. Engineers write the truth table first, then derive the circuit.
- Half Adder and Full Adder — A half adder adds two single bits and produces a Sum and a Carry. Sum = A XOR B, Carry = A AND B. A full adder adds three bits (A, B, and Carry-in) and produces Sum and Carry-out. Chain 16 full adders together and you have the 16-bit adder inside the 8086's ALU.
- The ALU: Arithmetic Logic Unit — The ALU is the heart of the microprocessor. It combines adders, logic gates, and multiplexers to perform operations selected by the instruction decoder. The 8086 ALU handles ADD, SUB, AND, OR, XOR, NOT, shift, rotate, and compare -- all using the same circuit with different control signals.
- Flip-Flops and State — Logic gates alone are stateless -- they forget. Flip-flops (bistable circuits) store one bit of memory. An SR flip-flop uses two cross-coupled NAND gates. A D flip-flop captures input on a clock edge. Registers in the 8086 are arrays of D flip-flops: AX is sixteen D flip-flops clocked together.
- Clock and Synchronization — The clock signal is a square wave that synchronizes all flip-flops. On each rising edge, flip-flops capture new data and logic gates settle to new outputs. The 8086 runs at 5-10 MHz. Each instruction takes multiple clock cycles: MOV takes 2 cycles, ADD takes 3, MUL takes up to 133 cycles.
- From Gates to Processors — A processor is built in layers: gates form adders and multiplexers, adders form the ALU, flip-flops form registers, control logic decodes instructions, and buses connect everything. The 8086 has roughly 29,000 transistors arranged into these functional blocks -- all from the same AND, OR, NOT building blocks.
Code example
; 8086 Assembly: Using bitwise logic to implement a 4-bit adder manually
; This demonstrates how ADD works internally using gates
.MODEL SMALL
.STACK 100h
.CODE
MAIN PROC
; We will add two 4-bit values using only AND, XOR, and SHL
; A = 0101 (5), B = 0011 (3), Expected: 1000 (8)
MOV AL, 05h ; A = 0101
MOV BL, 03h ; B = 0011
MOV CL, 00h ; Carry = 0
ADD_LOOP:
MOV DL, AL ; Save A
XOR AL, BL ; Sum without carry (half add)
AND DL, BL ; Generate carry bits
SHL DL, 1 ; Shift carry to next position
MOV BL, DL ; BL = new carry to propagate
CMP BL, 00h ; Any carries left?
JNE ADD_LOOP ; If yes, keep propagating
; AL now contains the result: 08h (5 + 3 = 8)
MOV AH, 4Ch
INT 21h
MAIN ENDP
END MAINLine-by-line walkthrough
- 1. MOV AL, 05h / MOV BL, 03h -- We load our two 4-bit numbers: A=5 (0101) and B=3 (0011). These are the values we want to add using only logic operations.
- 2. MOV DL, AL -- Save a copy of A in DL because XOR will overwrite AL. We need the original A for the carry calculation.
- 3. XOR AL, BL -- XOR produces the 'sum without carry' for each bit position. 0101 XOR 0011 = 0110. This is exactly what a half adder's Sum output does for each bit.
- 4. AND DL, BL -- AND finds positions where both bits are 1, which is where carries are generated. 0101 AND 0011 = 0001. Carry is generated at bit position 0.
- 5. SHL DL, 1 -- Shift the carry bits left by one position because a carry affects the next higher bit. 0001 becomes 0010.
- 6. MOV BL, DL / CMP BL, 00h / JNE ADD_LOOP -- We propagate carries by looping. The carry bits become the new B value. When no more carries remain (BL=0), the addition is complete.
- 7. After the loop: First iteration gives AL=0110, carry=0010. Second iteration: 0110 XOR 0010 = 0100, carry = 0010 AND 0110 shifted = 0100. Third iteration: 0100 XOR 0100 = 0000... wait, let me trace again -- actually AL=1000 = 8. The ripple carry algorithm terminates when all carries are resolved.
Spot the bug
MOV AL, 0Fh
MOV BL, 0Fh
AND AL, BL
JZ RESULT_ZERO
; Code continues expecting AL might be zero...Need a hint?
Show answer
Explain like I'm 5
Fun fact
Hands-on challenge
More resources
- Logic Gates - All About Circuits (All About Circuits)
- How Computers Add Numbers (YouTube)
- Digital Logic Simulator (Logic.ly)
- 8086 Flag Register (GeeksforGeeks)