Microprocessor & Assembly Language
Visitors: 0 Cache: ... Predicted Questions

Microprocessor & Assembly Language

BGC Trust University Bangladesh | Course: MP 504

Exam Answers from PDF Notes (Microprocessor-2 + Microprocessor-3)

1a Draw the internal architecture of 8086 microprocessor. Write the function of different units 4 marks
πŸ“„ Found in: Page 4-5 of PDF
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      8086 MICROPROCESSOR                        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   BUS INTERFACE UNIT     β”‚        EXECUTION UNIT                β”‚
β”‚         (BIU)            β”‚            (EU)                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚ Address Adder (Ξ£)  β”‚  β”‚  β”‚   Control Circuitry          β”‚   β”‚
β”‚  β”‚ PA = SegΓ—10H+Off   β”‚  β”‚  β”‚   Instruction Decoder        β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                          β”‚                                      β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚ Segment Registers  β”‚  β”‚  β”‚ General Purpose Registers    β”‚   β”‚
β”‚  β”‚ CS, DS, SS, ES     β”‚  β”‚  β”‚ AX, BX, CX, DX              β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚ SP, BP, SI, DI               β”‚   β”‚
β”‚                          β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚                                      β”‚
β”‚  β”‚ IP (Instruction    β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚     Pointer)       β”‚  β”‚  β”‚        ALU (16-bit)          β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β”‚                          β”‚                                      β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚ 6-Byte Instruction β”‚  β”‚  β”‚      Flag Register           β”‚   β”‚
β”‚  β”‚     Queue (FIFO)   β”‚  β”‚  β”‚   (9 active flags)           β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Functions:

UnitFunction
BIUFetches instructions, reads/writes data, generates 20-bit physical addresses, manages 6-byte instruction queue
EUDecodes and executes instructions, performs ALU operations, manages registers and flags
Segment RegistersCS (Code), DS (Data), SS (Stack), ES (Extra) - each 16-bit for segmented memory
Instruction Queue6-byte FIFO pre-fetch buffer enabling pipelining
ALU16-bit Arithmetic Logic Unit for add, subtract, Boolean, shift operations
Flags9 active flags: CF, PF, AF, ZF, SF, OF, TF, IF, DF
1b Calculate string addressing when DF=1 and DF=0 3 marks
πŸ“„ Found in: Page 33 of PDF

Given: DS=7000H, ES=1550H, SI=0050H, DI=0020H

When DF=0 (Increment Mode):

  • Source: ESΓ—10H + SI = 15500H + 0050H = 15550H
  • Destination: DSΓ—10H + DI = 70000H + 0020H = 70020H
  • After: SI=0052H, DI=0022H (incremented by 2)

When DF=1 (Decrement Mode):

  • Source: ESΓ—10H + DI = 15500H + 0020H = 15520H
  • Destination: DSΓ—10H + SI = 70000H + 0050H = 70050H
  • After: SI=004EH, DI=001EH (decremented by 2)
; DF=0 (Increment)
CLD              ; Clear direction flag
LEA SI, SOURCE
LEA DI, DEST
MOVSB            ; SI++, DI++

; DF=1 (Decrement)
STD              ; Set direction flag
LEA SI, SOURCE+1
LEA DI, DEST+1
MOVSB            ; SI--, DI--
1c Define physical address 1 mark
πŸ“„ Found in: Page 17 of PDF

Physical Address is the 20-bit actual address generated by BIU to access memory.

Physical Address = Segment Γ— 10H + Offset

Example: CS=348AH, IP=4214H β†’ PA = 348A0H + 4214H = 38AB4H

1d Explain relative addressing with example 2 marks
πŸ“„ Found in: Page 35 of PDF

Relative Addressing: Operand is signed 8-bit displacement relative to PC/IP.

EA = IP + displacement
; JNC - Jump if No Carry
JNC START    ; If CF=0, jump to START

; JNZ - Jump if Not Zero  
JNZ NEXT     ; If ZF=0, jump to NEXT
2a Stack pointer operation - PUSH and POP 5 marks
πŸ“„ Found in: Pages 19-21 of PDF

Given: Initial SP=40C5H, SS=2022H

After PUSH:

  • SP = SP - 2 = 40C3H
  • [40C3H] = low byte, [40C4H] = high byte

After POP:

  • SP = SP + 2 = 40C5H (restored)
OperationSPMemory
Initial40C5H-
After PUSH40C3H[40C3H]=low, [40C4H]=high
After POP40C5HValues read to register
2b What is wrong with the instructions? 3 marks
πŸ“„ Found in: Pages 28, 31, 34, 37 of PDF
InstructionErrorCorrect
MOV [BX], [DI]Memory-to-memory not allowedMOV AX,[DI] then MOV [BX],AX
MUL AL, 08HMUL takes only one operandMUL 08H
OUT BX, ALPort must be 8-bit or use DXOUT DX, AL
MOV ES, SSSegment-to-segment not allowedMOV AX,SS then MOV ES,AX
INC [BX]Missing operand sizeINC WORD PTR [BX]
IN AH, 08HIN must use AL/AXIN AL, 08H
2c Which registers hold segment and offset addresses? 2 marks
πŸ“„ Found in: Pages 17, 25-26 of PDF
TypeRegistersPurpose
SegmentCS, DS, SS, ESStarting address of memory segments
OffsetIP, SP, BP, SI, DI, BXDisplacement within segment
  • CS + IP = Program execution address
  • DS + SI/DI/BX = Data access address
  • SS + SP/BP = Stack access address
3a Explain the role of queue in processor 2 marks
πŸ“„ Found in: Page 16 of PDF
  • 6-byte FIFO instruction queue in BIU
  • Pre-fetches instructions ahead of execution
  • Enables pipelining - fetch while executing
  • Resets on Jump/Call instructions
  • Removes instructions when EU reads them
3b Evaluate AX register after instructions 5 marks
πŸ“„ Found in: Pages 8-9 of PDF
MOV AH, 55H    ; AH = 55H
MOV AL, 01H    ; AX = 5501H
MOV CX, 01FAH  ; CX = 01FAH
MUL CL         ; AX = 01H Γ— FAH = 00FAH
DIV CH         ; AX = 00FAH Γ· 01H = 00FAH
StepInstructionAX
1MOV AH, 55H55XXH
2MOV AL, 01H5501H
3MUL CL00FAH
4DIV CH00FAH

Final AX = 00FAH

3c Differences between IN and MOV instructions 3 marks
πŸ“„ Found in: Page 34 of PDF
FeatureINMOV
PurposeI/O port transferRegister/memory transfer
OperandsOnly AL/AX + portAny register/memory
ExampleIN AL, 60HMOV AL, [60H]

MOV AX, [BX] - Reads from DS:BX (16-bit)

MOV [BP], DL - Writes to SS:BP (8-bit)

4a Describe the operation of the following instructions 3 marks
πŸ“„ Found in: Pages 19-22, 36 of PDF
InstructionOperationDescription
PUSH AX SP ← SPβˆ’2; [SS:SP] ← AX Decrement SP by 2, then store AX (16-bit) at the new top of stack.
POP F FLAGS ← [SS:SP]; SP ← SP+2 Read word from top of stack into FLAGS register, then increment SP by 2.
HLT Stop CPU Halts the processor until a hardware interrupt (INTR/NMI) or RESET occurs. NOP wastes time; HLT saves power.
DAA Decimal Adjust AL Adjusts AL after BCD addition. If AL&0FH > 9 or AF=1, adds 6 to AL. If AL > 99H or CF=1, adds 60H to AL.
ADD [AX],[BX] Invalid Memory-to-memory ADD is not supported. 8086 allows only one memory operand. Correct: MOV AX,[BX] then ADD [DI],AX
IN BL, 64H Invalid IN instruction only accepts AL or AX as the data register. Correct: IN AL, 64H
4b What is addressing mode? Categorize them 2 marks
πŸ“„ Found in: Page 27 of PDF

Addressing Modes are the different ways the processor determines the effective address (EA) of an operand. The 8086 supports 12 addressing modes in 5 groups:

GroupModeExampleDescription
1. Register & ImmediateRegisterMOV AX, BXOperand in register
ImmediateMOV AX, 1234HOperand in instruction
2. MemoryDirectMOV AX, [2000H]EA = displacement
Register IndirectMOV AX, [BX]EA = BX/SI/DI
BasedMOV AX, [BX+10H]EA = BX + disp
IndexedMOV AX, [SI+10H]EA = SI/DI + disp
Based-IndexedMOV AX, [BX+SI]EA = BX + SI/DI
Based-Indexed + DispMOV AX, [BX+SI+10H]EA = BX + SI/DI + disp
3. PortDirect PortIN AL, 60H8-bit port address
Indirect PortIN AL, DX16-bit port in DX
4. RelativePC-relativeJMP STARTEA = IP + displacement
5. ImpliedNo operandHLT, NOPOperand implicit in opcode
4c Write an assembly language program to find the sum: 5+10+15+20+...+125 5 marks
πŸ“„ Found in: Page 40 of PDF

Analysis: Arithmetic series with a=5, d=5, last term=125. Number of terms = 125/5 = 25. Loop count = 25βˆ’1 = 24 (18H).

; Sum = 5+10+15+...+125
; Series: a=5, d=5, last=125, n=25, additions=24

        MOV CX, 18H      ; CX = 24 (loop counter)
        MOV AX, 0005H    ; AX = first term (5)
        MOV BX, 000AH    ; BX = second term (10)

NEXT:   ADD AX, BX       ; AX = AX + BX (accumulate sum)
        ADD BX, 0005H    ; BX = next term (+5)
        DEC CX           ; CX = CX - 1
        JNZ NEXT         ; If CX β‰  0, jump to NEXT

; After loop: AX = 1625 (decimal sum)
; Store result
        MOV [RESULT], AX
        HLT

RESULT  DW 0             ; Memory location for result

Interactive Calculator

5a Which control signals are activated during MOV BX,[SI] and MOV [DL],AL? 3 marks
πŸ“„ Found in: Pages 22-24 of PDF

MOV BX, [SI] β€” Memory Read (Data Transfer from Memory to Register)

SignalValueReason
M/IO1Memory operation (not I/O)
RD0 (active)Read from memory
WR1 (inactive)Not a write operation
DT/R0Receive (data coming into CPU)
DEN0 (active)Enable external data bus buffers
ALE1 then 0Address latch enable: high in T1 to latch address, then low for data

MOV [DL], AL β€” I/O Write (Data Transfer from Register to I/O Port)

Note: DL contains the 8-bit port address. This is an I/O write operation.

SignalValueReason
M/IO0I/O operation (not memory)
RD1 (inactive)Not a read operation
WR0 (active)Write to I/O port
DT/R1Transmit (data going out from CPU)
DEN0 (active)Enable external data bus buffers
ALE1 then 0Address latch enable in T1
5b Write short notes on: DEN, NMI, Status Bus, DT/R 4 marks
πŸ“„ Found in: Pages 18, 22-23 of PDF

DEN (Data Bus Enable) β€” Pin 26

DEN is an active-low output signal in minimum mode. When low, it enables external data bus transceivers/buffers. It goes active during data transfer phases of bus cycles and inactive during address phases. This prevents data bus contention when address and data share the same multiplexed bus (AD15-AD0).

NMI (Non-Maskable Interrupt) β€” Pin 17

NMI is a edge-triggered (low-to-high) interrupt input. Unlike INTR, NMI cannot be disabled by clearing IF (Interrupt Flag). It is always serviced. When NMI is activated, the processor completes the current instruction, saves flags (push), clears IF and TF, then fetches the interrupt service routine from vector address 00008H (vector 2). Used for critical events like power failure, memory parity errors.

Status Bus (A19/S6 – A16/S3)

These are multiplexed address/status pins. During T1 of a bus cycle, they carry the upper 4 address bits (A19-A16). During T2-T4, they output status information:

S4S3Segment
00ES (Extra Segment)
01SS (Stack Segment)
10CS (Code Segment) or no segment
11DS (Data Segment)

S5 = IF (Interrupt Flag status), S6 = 0 (always low).

DT/R (Data Transmit/Receive) β€” Pin 27

DT/R is an output signal in minimum mode that controls the direction of external data bus transceivers.

  • DT/R = 1 (Transmit): CPU is writing data to memory or I/O (data flows out from CPU).
  • DT/R = 0 (Receive): CPU is reading data from memory or I/O (data flows into CPU).

This signal must be connected to the direction control pin of external bus transceivers (like 74LS245) to ensure correct data flow direction.

5c Which register is called Accumulator? Write the special purposes of CX register 3 marks
πŸ“„ Found in: Pages 8-9 of PDF

Accumulator β€” AX Register

AX (16-bit) is called the accumulator. It is split into AH (high byte) and AL (low byte). Special uses:

  • I/O Operations: IN and OUT instructions must use AL (8-bit) or AX (16-bit) as the data register.
  • MUL/DIV: MUL multiplies AL (or AX) by operand, result in AX (or DX:AX). DIV divides AX (or DX:AX) by operand.
  • String I/O: LODS loads from [DS:SI] into AL/AX. STOS stores AL/AX to [ES:DI]. SCAS compares AL/AX with [ES:DI].
  • BCD Adjustments: DAA and DAS adjust AL after BCD addition/subtraction.
  • Sign Extend: CBW extends AL to AX (sign). CWD extends AX to DX:AX.

CX Register β€” Count Register

CX (16-bit) is split into CH (high byte) and CL (low byte). Special uses:

  • LOOP Instruction: CX is automatically decremented by 1. Loop jumps to label if CX β‰  0.
  • LOOPZ/LOOPNZ: Conditional loop using CX as counter with zero-flag check.
  • REP Prefix: Repeats string instruction CX times (REP MOVSB, REP LODSB, etc.).
  • Shift/Rotate Count: CL register specifies the count for shift and rotate instructions (SHR, ROL, etc.).
  • String Scan: REPNE SCASB scans memory CX times comparing with AL.
6a Determine the port addresses of 8254 2 marks
πŸ“„ Found in: Pages 24-25 of PDF

The 8254 uses A1 and A0 (connected to system address lines) to select between its internal registers. CS (chip select) is typically decoded from higher address lines (e.g., A15 inverted).

Address Selection Table (A1, A0):

A1A0Selected RegisterPort Address (if CS=8000H–8003H)
00Counter 08000H
01Counter 18001H
10Counter 28002H
11Control Register8003H

If CS is decoded from A15 (inverted, active low), then:

  • A15 = 0 for 8254 selected (address range 0000H–7FFFH)
  • A14-A2 are decoded to select the specific 8254 in a multi-device system
  • A1 and A0 select the internal register
6b Write a subroutine to initialize Counter 1 in Mode 4 with count 65000, and read the count on the fly 4 marks
πŸ“„ Found in: Pages 25-30 of PDF

Control Word Calculation:

Counter 1, Mode 4, Binary count, LSB+MSB write:

SC1 SC0 | RW1 RW0 | M2 M1 M0 | BCD = 01 | 11 | 100 | 0 = 78H

Count Value:

65000 decimal = FE60H (16-bit binary)

LSB = 60H, MSB = FEH

Subroutine:

; Initialize Counter 1 in Mode 4, Count = 65000
; Assumes: Control Register = 8003H, Counter 1 = 8001H

INIT_CTR1 PROC NEAR
    ; Step 1: Write Control Word
    MOV AL, 78H         ; 01 11 100 0 = Counter1, LSB+MSB, Mode4, Binary
    OUT 8003H, AL       ; Write to Control Register

    ; Step 2: Load count LSB first
    MOV AL, 60H         ; Low byte of FE60H
    OUT 8001H, AL       ; Write to Counter 1

    ; Step 3: Load count MSB
    MOV AL, FEH         ; High byte of FE60H
    OUT 8001H, AL       ; Write to Counter 1
    RET
INIT_CTR1 ENDP

; Read Count on the Fly (without stopping counter)
READ_CTR1 PROC NEAR
    ; Step 1: Write Latch Command for Counter 1
    MOV AL, 40H         ; 01 00 0000 = Latch Counter 1
    OUT 8003H, AL       ; Write to Control Register

    ; Step 2: Read LSB first
    IN AL, 8001H        ; Read LSB of latched count
    MOV BL, AL          ; Store in BL

    ; Step 3: Read MSB
    IN AL, 8001H        ; Read MSB of latched count
    MOV BH, AL          ; Store in BH
    ; BX now contains the latched count value
    RET
READ_CTR1 ENDP

Explanation:

  • Latch Command (40H): Freezes the current count in an internal latch so it can be read without affecting the counting process.
  • Read Order: Always read LSB first, then MSB (matching the write order set in the control word).
  • GATE Pin: Must be high (logic 1) for the counter to count in Mode 4.
6c Write a main program to display seconds using the subroutine from Q6b 4 marks
πŸ“„ Found in: Pages 30-35 of PDF

Timing Calculation:

Assume CLK = 3.5 MHz (typical for 8086 system):

Clock period = 1 / 3.5 MHz = 0.2857 ΞΌs
Counter period = 65000 Γ— 0.2857 ΞΌs = 18.57 ms per call
Calls per second = 1000 ms / 18.57 ms β‰ˆ 54 calls

Main Program:

; Main Program: Display seconds on 7-segment or counter
; Counter 1 in Mode 4, Count = 65000
; CLK = 3.5 MHz, ~18.57ms per countdown

COUNTS_PER_SEC  EQU 54      ; 54 calls β‰ˆ 1 second
DISP_COUNT      DB 0        ; Seconds counter (0-59)

MAIN:
    MOV AX, 0000H
    MOV DS, AX              ; Initialize DS

    ; Initialize Counter 1
    CALL INIT_CTR1          ; From Q6b subroutine

SECOND_LOOP:
    MOV CH, COUNTS_PER_SEC  ; CH = 54 (calls per second)

WAIT_SEC:
    ; Wait for Counter 1 countdown to complete
    CALL READ_CTR1          ; Read current count
    CMP BX, 0000H           ; Check if counter reached 0
    JNZ WAIT_SEC            ; Keep polling

    ; Counter finished one cycle, reinitialize
    CALL INIT_CTR1

    DEC CH                  ; One more 18.57ms passed
    JNZ WAIT_SEC            ; Continue until 54 calls

    ; 1 second elapsed
    INC DISP_COUNT          ; Increment seconds display
    CMP DISP_COUNT, 60      ; Check if 60 seconds
    JL DISPLAY_SEC

    MOV DISP_COUNT, 0       ; Reset to 0 (optional: increment minutes)

DISPLAY_SEC:
    MOV AL, DISP_COUNT
    ; Output to display port (e.g., 7-segment decoder)
    OUT 80H, AL             ; Write to display port

    JMP SECOND_LOOP         ; Repeat forever

Alternative: Using Mode 2 (Rate Generator) for continuous seconds:

; Cascade: Counter 1 (Mode 2, count 50000) drives Counter 2 (Mode 2, count 40)
; Total: 50000 Γ— 40 = 2,000,000 counts = 1 second at 2MHz

; Counter 1 Control Word: 01 11 010 0 = 74H
; Counter 2 Control Word: 10 11 010 0 = 94H

INIT_SECONDS PROC NEAR
    ; Counter 1: Mode 2, Count = 50000
    MOV AL, 74H
    OUT 8003H, AL
    MOV AL, 50H             ; LSB of 50000 (C350H)
    OUT 8001H, AL
    MOV AL, C3H             ; MSB
    OUT 8001H, AL

    ; Counter 2: Mode 2, Count = 40
    MOV AL, 94H
    OUT 8003H, AL
    MOV AL, 28H             ; 40 = 28H
    OUT 8002H, AL
    MOV AL, 00H
    OUT 8002H, AL

    RET
INIT_SECONDS ENDP
7a Draw the block diagram of 8254 and list its applications 3 marks
πŸ“„ Found in: Pages 21-22 of PDF
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         8254 TIMING COUNTER                         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                                                     β”‚
β”‚   D7-D0 ──>β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                     β”‚
β”‚  (Data Bus) β”‚   Data Bus      β”‚                                     β”‚
β”‚             β”‚   Buffer        β”‚                                     β”‚
β”‚             β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                     β”‚
β”‚                      β”‚ Internal Bus                                  β”‚
β”‚   RD ──>  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  <── WR                            β”‚
β”‚   CS ──>  β”‚  Read/Write Logic  β”‚  <── A0, A1                        β”‚
β”‚            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                     β”‚
β”‚                      β”‚                                               β”‚
β”‚            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                     β”‚
β”‚            β”‚ Control Word      β”‚                                     β”‚
β”‚            β”‚ Register          β”‚                                     β”‚
β”‚            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                     β”‚
β”‚                      β”‚                                               β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                           β”‚
β”‚   β”‚                  β”‚                  β”‚                           β”‚
β”‚   β–Ό                  β–Ό                  β–Ό                           β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”          β”Œβ”€β”€β”€β”€β”€β”€β”                          β”‚
β”‚ β”‚CTR 0 β”‚        β”‚CTR 1 β”‚          β”‚CTR 2 β”‚                          β”‚
β”‚ β”‚      β”‚        β”‚      β”‚          β”‚      β”‚                          β”‚
β”‚ β”‚CLK0  β”‚        β”‚CLK1  β”‚          β”‚CLK2  β”‚                          β”‚
β”‚ β”‚GATE0 β”‚        β”‚GATE1 β”‚          β”‚GATE2 β”‚                          β”‚
β”‚ β”‚OUT0  β”‚        β”‚OUT1  β”‚          β”‚OUT2  β”‚                          β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”€β”€β”˜                          β”‚
β”‚                                                                     β”‚
β”‚   CLK  ──> 16-bit Counter ──> OUT (3 independent channels)         β”‚
β”‚   GATE ──> Gate Control (starts/stops counting)                    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Block Diagram Components:

ComponentDescription
Data Bus Buffer8-bit bidirectional, connects to system data bus D7-D0
Read/Write LogicRD, WR, CS, A0, A1 control register access
Control Word RegisterStores the control word written by CPU for each counter
Counter 0, 1, 2Three independent 16-bit down-counters with CLK, GATE, OUT pins

Applications of 8254:

  • Real-Time Clock: Generate periodic interrupts for timekeeping (e.g., 100 Hz for OS tick).
  • Event Counter: Count external events using CLK input (Mode 0).
  • Digital One-Shot: Generate a single pulse of programmable width (Mode 1).
  • Square Wave Generator: Generate precise frequency square waves (Mode 3).
  • Rate Generator: Generate periodic pulses at specified intervals (Mode 2).
  • Complex Waveform Generator: Combine multiple modes for audio/signal generation.
  • DRAM Refresh: Generate DMA request signals for periodic DRAM refresh.
  • Baud Rate Generator: Provide clock for serial communication.

Key Features (8254 vs 8253):

Feature82548253
Max FrequencyDC-8 MHz (8254-2: DC-10 MHz)DC-2 MHz
Read-BackYes (status + count)No
Packages24-pin DIP24-pin DIP
CountingBinary or BCDBinary or BCD
7b Explain read-back command and count on the fly in 8254 3 marks
πŸ“„ Found in: Pages 29-30 of PDF

Read-Back Command (8254 only, not 8253)

The read-back command allows the CPU to read both the count value and status of multiple counters simultaneously using a single command byte.

Read-Back Command Format:

D7  D6  D5  D4  D3  D2  D1  D0
 1   1   COUNT STATUS CNT2 CNT1 CNT0  0
 β”‚   β”‚    β”‚     β”‚      β”‚    β”‚    └── Select Counter 0
 β”‚   β”‚    β”‚     β”‚      β”‚    └─────── Select Counter 1
 β”‚   β”‚    β”‚     β”‚      └─────────── Select Counter 2
 β”‚   β”‚    β”‚     └───────────────── Read status register
 β”‚   β”‚    └─────────────────────── Read count value
 β”‚   └────────────────────────── Must be 1 for read-back
 └────────────────────────────── Must be 11 for read-back
  • COUNT bit (D4): If 1, latches the count of selected counters for reading.
  • STATUS bit (D3): If 1, latches the status register of selected counters.
  • CNT2/CNT1/CNT0: Select which counters to read back (multiple can be selected).

Status Register Format:

D7  D6  D5  D4  D3  D2  D1  D0
OUTPUT NULLCOUNT RW1 RW0 M2 M1 M0 BCD
   β”‚      β”‚       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  └─── BCD/Binary
   β”‚      └──── If 1, count not yet loaded
   └─────────── Current OUT pin state

Count on the Fly (Reading While Counting)

To read the current count without stopping the counter (GATE must remain high):

  1. Method 1 β€” Latch Command: Write counter latch command (SC1 SC0 0 0 X X X X) to freeze count. Then read LSB then MSB from counter port.
  2. Method 2 β€” Read-Back Command: Write read-back command with COUNT=1. This latches count and optionally status. Then read status (if latched) then count (LSB then MSB).
; Method 1: Latch Command for Counter 0
MOV AL, 00H          ; 00 00 0000 = Latch Counter 0
OUT CTRL_REG, AL      ; Write latch command
IN AL, CTR0_PORT      ; Read LSB
MOV BL, AL
IN AL, CTR0_PORT      ; Read MSB
MOV BH, AL            ; BX = latched count

; Method 2: Read-Back Command for Counter 1
MOV AL, 0C6H          ; 11 11 0 1 1 0 0 = Read-back Counter1
OUT CTRL_REG, AL      ; Count + Status
IN AL, CTR1_PORT      ; Read status first
IN AL, CTR1_PORT      ; Read LSB of count
IN AL, CTR1_PORT      ; Read MSB of count
7c Explain Rate Generator and Software Triggered Strobe modes of 8254 4 marks
πŸ“„ Found in: Pages 31-34 of PDF

Mode 2: Rate Generator

Generates a continuous series of 1-clock-wide negative pulses at a programmable rate.

PropertyBehavior
OUT OutputHigh for (Nβˆ’1) clock periods, low for 1 clock period
Auto-ReloadCounter automatically reloads the count value after reaching 0
GATE RequirementGATE must be high for counting; GATE=0 stops counter
TriggerSoftware (loading count starts counting)
N=1Illegal (undefined behavior)
ApplicationsPeriodic interrupts, baud rate generation, clock dividers
Mode 2 Waveform (N=5):

CLK:  β”Œβ”€β”€β”  β”Œβ”€β”€β”  β”Œβ”€β”€β”  β”Œβ”€β”€β”  β”Œβ”€β”€β”  β”Œβ”€β”€β”  β”Œβ”€β”€β”  β”Œβ”€β”€β”  β”Œβ”€β”€β”
      β””β”€β”€β”˜  β””β”€β”€β”˜  β””β”€β”€β”˜  β””β”€β”€β”˜  β””β”€β”€β”˜  β””β”€β”€β”˜  β””β”€β”€β”˜  β””β”€β”€β”˜  β””β”€β”€β”˜

OUT:  ──────────────────────────────┐     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
      ↑ N-1=4 cycles high          β””β”€β”€β”€β”€β”€β”˜ 1 cycle low
                                  (auto-reload, repeats)

Mode 4: Software Triggered Strobe

Generates a single negative pulse (1 clock wide) after a software-triggered countdown. Unlike Mode 2, it does NOT auto-reload.

PropertyBehavior
OUT OutputHigh for N clock periods, low for 1 clock period at end
Auto-ReloadNO β€” must reload count for each new pulse
GATE RequirementGATE must be high; GATE=0 halts counter
TriggerSoftware (loading count starts counting)
ApplicationsProgrammable delay, single-shot pulses, strobe signals
Mode 4 Waveform (N=5):

CLK:  β”Œβ”€β”€β”  β”Œβ”€β”€β”  β”Œβ”€β”€β”  β”Œβ”€β”€β”  β”Œβ”€β”€β”  β”Œβ”€β”€β”  β”Œβ”€β”€β”
      β””β”€β”€β”˜  β””β”€β”€β”˜  β””β”€β”€β”˜  β””β”€β”€β”˜  β””β”€β”€β”˜  β””β”€β”€β”˜  β””β”€β”€β”˜

OUT:  ──────────────────────────────┐     ─────────────────
      ↑ N=5 cycles high            └───── 1 cycle low
      (one-shot, stops β€” must reload)

Key Differences:

FeatureMode 2 (Rate Generator)Mode 4 (SW Triggered Strobe)
OutputContinuous pulsesSingle pulse per load
Auto-ReloadYesNo
TriggerSoftwareSoftware
GATE ControlHigh to count, Low to stopHigh to count, Low to stop
Pulse Width1 CLK (always)1 CLK (always)
High PeriodNβˆ’1 CLKN CLK
Use CasePeriodic interrupts, clocksDelays, one-shot events

Mode 5: Hardware Triggered Strobe (for comparison)

Similar to Mode 4, but counting is triggered by a rising edge on the GATE pin (hardware trigger) instead of software loading. Useful when precise timing relative to an external event is needed.