Lesson 4 of 48 beginner

Digital Logic Foundations

The gates that build everything from adders to processors

Open interactive version (quiz + challenge)

Real-world analogy

Logic gates are like voting booths with very strict rules. An AND gate only says 'yes' if ALL voters say yes. An OR gate says 'yes' if ANY voter says yes. A NOT gate is a contrarian -- it always says the opposite. By chaining millions of these simple voters together, you can build a machine that does math, stores memory, and runs programs. The entire 8086 processor is just millions of these tiny yes/no decision makers working in concert.

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

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 MAIN

Line-by-line walkthrough

  1. 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. 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. 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. 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. 5. SHL DL, 1 -- Shift the carry bits left by one position because a carry affects the next higher bit. 0001 becomes 0010.
  6. 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. 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?
Think about what 0Fh AND 0Fh actually produces. When does AND result in zero?
Show answer
0Fh AND 0Fh = 0Fh (any value AND-ed with itself returns itself, not zero). The JZ will never trigger here. If the intent was to check if two values are equal, the correct instruction is XOR AL, BL (which gives zero when both values are the same) or CMP AL, BL followed by JZ.

Explain like I'm 5

Imagine you have three magic stamps. The AND stamp only marks a paper if you press BOTH buttons. The OR stamp marks a paper if you press EITHER button. The NOT stamp flips whatever you give it -- give it a smiley face, it stamps a frown. Now imagine you have millions of these tiny stamps all connected together, stamping faster than you can blink. That is how a computer chip does math and makes decisions!

Fun fact

The NAND gate is called a 'universal gate' because you can build ANY other logic gate using only NAND gates. AND = NAND followed by NAND(inverter). OR = NAND the inverted inputs. NOT = NAND with both inputs tied together. In fact, early computers like the Apollo Guidance Computer were built using only NOR gates -- about 5,600 of them, wired by hand by women workers at a Raytheon factory.

Hands-on challenge

Using only 8086 bitwise instructions (AND, OR, XOR, NOT, SHL, SHR), write a program that multiplies AL by 2 without using MUL or ADD. Then extend it to multiply by 10 (hint: multiply by 8 and multiply by 2, then combine the results). Verify your answer by checking AX after execution.

More resources

Open interactive version (quiz + challenge) ← Back to course: Microprocessor A–Z