0% found this document useful (0 votes)
30 views30 pages

Unit 3-4 Mpi Codes and Extra Coding Related Content

Uploaded by

DIKSHANT SHARMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views30 pages

Unit 3-4 Mpi Codes and Extra Coding Related Content

Uploaded by

DIKSHANT SHARMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

8086 Programs

Question 1: Evaluate the Expression Z=A+BZ = A + B

Write an assembly program to calculate the sum of two 8-bit numbers, AA and BB, and store the
result in ZZ.

Solution:

DATA SEGMENT
A DB 25H ; First number
B DB 17H ; Second number
Z DB ? ; Result
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX ; Initialize data segment register

MOV AL, A ; Load A into AL


ADD AL, B ; Add B to AL
MOV Z, AL ; Store result in Z

MOV AH, 4CH ; Exit to DOS


INT 21H
CODE ENDS
END START

Question 2: Evaluate the Expression Z=A−BZ = A - B

Write an assembly program to calculate the subtraction of BB from AA.

Solution:

DATA SEGMENT
A DB 3AH ; First number
B DB 14H ; Second number
Z DB ? ; Result
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX ; Initialize data segment register

MOV AL, A ; Load A into AL


SUB AL, B ; Subtract B from AL
MOV Z, AL ; Store result in Z
MOV AH, 4CH ; Exit to DOS
INT 21H
CODE ENDS
END START

Question 3: Evaluate the Expression Z=A×BZ = A \times B

Write an assembly program to calculate the product of two 8-bit numbers, AA and BB.

Solution:

DATA SEGMENT
A DB 05H ; First number
B DB 03H ; Second number
Z DW ? ; Result (product may exceed 8 bits, so use word)
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX ; Initialize data segment register

MOV AL, A ; Load A into AL


MOV BL, B ; Load B into BL
MUL BL ; Multiply AL by BL, result in AX
MOV Z, AX ; Store result in Z

MOV AH, 4CH ; Exit to DOS


INT 21H
CODE ENDS
END START

Question 4: Evaluate the Expression Z=A/BZ = A / B

Write an assembly program to divide AA by BB and store the quotient in ZZ and the remainder
in RR.

Solution:

DATA SEGMENT
A DB 16H ; Dividend
B DB 05H ; Divisor
Z DB ? ; Quotient
R DB ? ; Remainder
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX ; Initialize data segment register
MOV AL, A ; Load A into AL
MOV BL, B ; Load B into BL
XOR AH, AH ; Clear AH for division
DIV BL ; Divide AL by BL, quotient in AL, remainder in AH
MOV Z, AL ; Store quotient in Z
MOV R, AH ; Store remainder in R

MOV AH, 4CH ; Exit to DOS


INT 21H
CODE ENDS
END START

Question 5: Evaluate the Expression Z=(A+B)×CZ = (A + B) \times C

Write an assembly program to compute ZZ where Z=(A+B)×CZ = (A + B) \times C.

Solution:

DATA SEGMENT
A DB 04H ; First number
B DB 02H ; Second number
C DB 03H ; Multiplier
Z DW ? ; Result (product may exceed 8 bits)
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX ; Initialize data segment register

MOV AL, A ; Load A into AL


ADD AL, B ; Add B to AL
MOV BL, C ; Load C into BL
MUL BL ; Multiply AL by BL, result in AX
MOV Z, AX ; Store result in Z

MOV AH, 4CH ; Exit to DOS


INT 21H
CODE ENDS
END START

These programs demonstrate basic arithmetic operations and use of 8086 registers like AX, AL,
AH, and BL. The MUL and DIV instructions are used for multiplication and division.

Question 1: Reverse a String


Write an 8086 program to reverse a given string.

Solution:

asm
Copy code
DATA SEGMENT
STR DB 'HELLO$', 0 ; Input string (terminated with $)
RESULT DB 6 DUP ('$') ; Space for the reversed string
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX ; Initialize data segment register

LEA SI, STR ; Load address of input string


LEA DI, RESULT ; Load address of result string

; Find the length of the string


MOV CX, 0
FIND_LENGTH:
MOV AL, [SI] ; Load character
CMP AL, '$' ; Check for string terminator
JE REVERSE ; If terminator found, start reversing
INC SI ; Move to the next character
INC CX ; Increment length counter
JMP FIND_LENGTH

REVERSE:
DEC SI ; SI points to last valid character
REVERSE_LOOP:
MOV AL, [SI] ; Load character from the end
MOV [DI], AL ; Store in result
DEC SI ; Move backward in input string
INC DI ; Move forward in result
LOOP REVERSE_LOOP ; Repeat for all characters

MOV [DI], '$' ; Add string terminator to result

; Exit to DOS
MOV AH, 4CH
INT 21H
CODE ENDS
END START

Question 2: Concatenate Two Strings

Write an 8086 program to concatenate two strings.

Solution:

asm
Copy code
DATA SEGMENT
STR1 DB 'HELLO$', 0 ; First string
STR2 DB ' WORLD$', 0 ; Second string
RESULT DB 20 DUP ('$') ; Space for the concatenated string
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX ; Initialize data segment register

LEA SI, STR1 ; Address of first string


LEA DI, RESULT ; Address of result string

; Copy STR1 to RESULT


COPY_STR1:
MOV AL, [SI] ; Load character from STR1
CMP AL, '$' ; Check for string terminator
JE COPY_STR2 ; If terminator found, move to STR2
MOV [DI], AL ; Copy to result
INC SI ; Move to next character in STR1
INC DI ; Move to next character in RESULT
JMP COPY_STR1

COPY_STR2:
LEA SI, STR2 ; Address of second string
COPY_STR2_LOOP:
MOV AL, [SI] ; Load character from STR2
CMP AL, '$' ; Check for string terminator
JE END_PROGRAM ; If terminator found, end program
MOV [DI], AL ; Copy to result
INC SI ; Move to next character in STR2
INC DI ; Move to next character in RESULT
JMP COPY_STR2_LOOP

END_PROGRAM:
MOV [DI], '$' ; Add string terminator to result

; Exit to DOS
MOV AH, 4CH
INT 21H
CODE ENDS
END START

Question 3: Compare Two Strings

Write an 8086 program to compare two strings and check if they are equal.

Solution:

asm
Copy code
DATA SEGMENT
STR1 DB 'HELLO$', 0 ; First string
STR2 DB 'HELLO$', 0 ; Second string
RESULT DB ? ; 1 if equal, 0 if not
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX ; Initialize data segment register

LEA SI, STR1 ; Address of first string


LEA DI, STR2 ; Address of second string
MOV RESULT, 1 ; Assume strings are equal

COMPARE_LOOP:
MOV AL, [SI] ; Load character from STR1
MOV BL, [DI] ; Load character from STR2
CMP AL, BL ; Compare characters
JNE NOT_EQUAL ; If not equal, set result to 0
CMP AL, '$' ; Check for string terminator
JE END_COMPARE ; If terminator, end comparison
INC SI ; Move to next character in STR1
INC DI ; Move to next character in STR2
JMP COMPARE_LOOP

NOT_EQUAL:
MOV RESULT, 0 ; Set result to not equal

END_COMPARE:
; Exit to DOS
MOV AH, 4CH
INT 21H
CODE ENDS
END START

Question 4: Count the Length of a String

Write an 8086 program to find the length of a string.

Solution:

asm
Copy code
DATA SEGMENT
STR DB 'HELLO$', 0 ; Input string (terminated with $)
LENGTH DB ? ; Store length
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX ; Initialize data segment register

LEA SI, STR ; Address of input string


MOV CX, 0 ; Initialize counter

COUNT_LOOP:
MOV AL, [SI] ; Load character
CMP AL, '$' ; Check for string terminator
JE STORE_LENGTH ; If terminator found, store length
INC CX ; Increment counter
INC SI ; Move to next character
JMP COUNT_LOOP

STORE_LENGTH:
MOV LENGTH, CX ; Store length in LENGTH

; Exit to DOS
MOV AH, 4CH
INT 21H
CODE ENDS
END START

Question 5: Find a Character in a String

Write an 8086 program to search for a specific character in a string and return its position.

Solution:

asm
Copy code
DATA SEGMENT
STR DB 'HELLO$', 0 ; Input string
CHAR DB 'L', 0 ; Character to find
POSITION DB -1 ; Store position (-1 if not found)
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX ; Initialize data segment register

LEA SI, STR ; Address of input string


MOV AL, CHAR ; Load character to find
MOV CX, 0 ; Initialize position counter

FIND_CHAR:
MOV BL, [SI] ; Load character from string
CMP BL, '$' ; Check for string terminator
JE NOT_FOUND ; If terminator, character not found
CMP AL, BL ; Compare characters
JE STORE_POSITION ; If match, store position
INC SI ; Move to next character
INC CX ; Increment position counter
JMP FIND_CHAR

STORE_POSITION:
MOV POSITION, CX ; Store position in POSITION
JMP END_PROGRAM

NOT_FOUND:
MOV POSITION, -1 ; Set position to -1

END_PROGRAM:
; Exit to DOS
MOV AH, 4CH
INT 21H
CODE ENDS
END START

Question 6: Copy a String

Write a program to copy a string from source memory to destination memory.

Solution:

asm
Copy code
DATA SEGMENT
SRC DB 'HELLO$', 0 ; Source string with '$' as a delimiter
DEST DB 20 DUP('$') ; Destination string space
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX
MOV ES, AX ; Set ES to the same data segment

LEA SI, SRC ; Load address of source string into SI


LEA DI, DEST ; Load address of destination string into DI

COPY_LOOP:
MOV AL, [SI] ; Load current character from source
MOV [DI], AL ; Store it in destination
INC SI ; Increment source index
INC DI ; Increment destination index
CMP AL, '$' ; Check for end of string delimiter
JNE COPY_LOOP ; Repeat if not end

MOV AH, 4CH ; Exit to DOS


INT 21H
CODE ENDS
END START

Question 7: Find the Length of a String

Write a program to calculate the length of a string stored in memory (excluding the delimiter $).
Solution:

asm
Copy code
DATA SEGMENT
STR DB 'HELLO$', 0 ; Input string
LENGTH DB 0 ; Variable to store length
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX

LEA SI, STR ; Load address of string into SI


MOV CX, 0 ; Initialize length counter

LENGTH_LOOP:
MOV AL, [SI] ; Load current character
CMP AL, '$' ; Check for delimiter
JE END_LENGTH ; Exit if end of string
INC CX ; Increment counter
INC SI ; Increment source index
JMP LENGTH_LOOP ; Repeat loop

END_LENGTH:
MOV LENGTH, CL ; Store result in LENGTH

MOV AH, 4CH ; Exit to DOS


INT 21H
CODE ENDS
END START

Question 8: Reverse a String

Write a program to reverse a string stored in memory.

Solution:

asm
Copy code
DATA SEGMENT
STR DB 'HELLO$', 0 ; Input string
REV DB 20 DUP('$') ; Space for reversed string
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX
MOV ES, AX ; Set ES to the same data segment

LEA SI, STR ; Load address of input string into SI


LEA DI, REV ; Load address of reversed string into DI

; Find the length of the string


MOV CX, 0 ; Initialize length counter
FIND_LENGTH:
MOV AL, [SI]
CMP AL, '$'
JE FOUND_LENGTH
INC CX
INC SI
JMP FIND_LENGTH

FOUND_LENGTH:
DEC SI ; Move SI to the last character
DEC CX ; Exclude '$'

; Reverse the string


REVERSE_LOOP:
MOV AL, [SI] ; Load current character
MOV [DI], AL ; Store it in the reversed string
DEC SI ; Decrement source index
INC DI ; Increment destination index
LOOP REVERSE_LOOP

MOV [DI], '$' ; Add string delimiter

MOV AH, 4CH ; Exit to DOS


INT 21H
CODE ENDS
END START

Question 9: Compare Two Strings

Write a program to compare two strings and determine if they are equal.

Solution:

asm
Copy code
DATA SEGMENT
STR1 DB 'HELLO$', 0 ; First string
STR2 DB 'HELLO$', 0 ; Second string
RESULT DB 0 ; Result: 1 for equal, 0 for not equal
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX

LEA SI, STR1 ; Load address of first string


LEA DI, STR2 ; Load address of second string

COMPARE_LOOP:
MOV AL, [SI] ; Load current character from STR1
MOV BL, [DI] ; Load current character from STR2
CMP AL, BL ; Compare characters
JNE NOT_EQUAL ; If not equal, jump
CMP AL, '$' ; Check for end of string
JE EQUAL ; If end of string, strings are equal
INC SI ; Increment source index
INC DI ; Increment destination index
JMP COMPARE_LOOP ; Repeat loop

NOT_EQUAL:
MOV RESULT, 0 ; Set result to 0 (not equal)
JMP END_PROGRAM

EQUAL:
MOV RESULT, 1 ; Set result to 1 (equal)

END_PROGRAM:
MOV AH, 4CH ; Exit to DOS
INT 21H
CODE ENDS
END START

Question 10: Convert a String to Uppercase

Write a program to convert a string to uppercase.

Solution:

asm
Copy code
DATA SEGMENT
STR DB 'hello$', 0 ; Input string
UPPER DB 20 DUP('$') ; Space for uppercase string
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX
MOV ES, AX ; Set ES to the same data segment

LEA SI, STR ; Load address of input string


LEA DI, UPPER ; Load address of uppercase string

UPPERCASE_LOOP:
MOV AL, [SI] ; Load current character
CMP AL, '$' ; Check for end of string
JE END_UPPERCASE ; Exit if end of string
CMP AL, 'a' ; Check if character is lowercase
JL NOT_LOWERCASE
CMP AL, 'z'
JG NOT_LOWERCASE
SUB AL, 20H ; Convert to uppercase
NOT_LOWERCASE:
MOV [DI], AL ; Store character in uppercase string
INC SI ; Increment source index
INC DI ; Increment destination index
JMP UPPERCASE_LOOP ; Repeat loop

END_UPPERCASE:
MOV [DI], '$' ; Add string delimiter

MOV AH, 4CH ; Exit to DOS


INT 21H
CODE ENDS
END START

Interfacing microprocessors like the 8086 with RAM and ROM involves connecting memory
chips to the microprocessor to ensure data can be read from and written to memory in an
organized and efficient manner. Here’s a detailed guide:

Key Components

1. Microprocessor (e.g., 8086): The central processing unit that communicates with memory to
execute instructions and store data.
2. RAM: Volatile memory used for temporary data storage during program execution.
3. ROM: Non-volatile memory used to store programs or data that do not change (e.g., boot
programs).
4. Address Decoder: A circuit that selects the appropriate memory device based on the address
lines from the microprocessor.
5. Control Signals:
o RD (Read): Indicates a read operation.
o WR (Write): Indicates a write operation.
o ALE (Address Latch Enable): Used to latch the lower byte of the address in multiplexed
address/data buses.
6. Data Bus: Transfers data between the microprocessor and memory.
7. Address Bus: Carries the memory address to access.
8. Chip Select (CS): Enables a specific memory chip for communication.

Memory Map

To interface memory, a memory map is required, defining address ranges allocated to RAM and
ROM. For example:

 ROM: 0000H - 7FFFH (first half of the address space).


 RAM: 8000H - FFFFH (second half of the address space).
Interfacing RAM and ROM with 8086

1. Connecting Address and Data Lines

 8086 Address Bus (A0-A19): Directly connect to the memory chips.


o Lower address lines (A0-A19) select the location within a chip.
o Address decoder handles higher address lines to select RAM or ROM.
 8086 Data Bus (D0-D15): Connect to the data pins of RAM/ROM for data transfer.

2. Using Address Decoders

 An address decoder (e.g., 74LS138) uses higher address lines (A15-A19) to generate Chip Select
(CS) signals for RAM and ROM.
 Example:
o If A19=0A19 = 0: ROM is selected.
o If A19=1A19 = 1: RAM is selected.

3. Control Signals

 ROM: Only the RD signal is connected, as ROM is read-only.


 RAM: Both RD and WR signals are connected to allow read and write operations.

4. Multiplexed Address/Data Bus (AD0-AD15)

 The lower 16 address lines (A0-A15) in the 8086 are multiplexed with data lines.
 Use a latch (e.g., 74LS373) controlled by the ALE signal to demultiplex the address and data
buses.

5. Control Timing

Timing signals must align with the microprocessor clock to ensure proper data transfer:

 READY: Used to insert wait states if memory is slower than the processor.
 DEN (Data Enable): Activates memory or I/O devices.

Circuit Example

Connections

1. ROM:
o Address lines (A0-A14 for 32KB ROM).
o Data lines (D0-D7 for 8-bit data).
o Connect CS to output from the address decoder.
2. RAM:
o Address lines (A0-A14 for 32KB RAM).
o Data lines (D0-D7 for 8-bit data).
o CS connected to another output of the address decoder.
o RD and WR signals connected for read/write operations.

Address Decoder Example (74LS138)

 Inputs: A15-A17 (higher address lines).


 Outputs: Select signals for ROM or RAM.

Latch (74LS373)

 Multiplexed address/data lines (AD0-AD15) are latched using ALE.

Sample Memory Map

Address Range Device Chip Select Condition

0000H-7FFFH ROM A15 = 0

8000H-FFFFH RAM A15 = 1

Assembly Code Example

Here’s an example of reading from ROM and writing to RAM:

; Assume data in ROM at address 0000H


MOV AX, 0000H ; Load starting address of ROM
MOV DS, AX ; Set data segment to ROM
MOV AL, [BX] ; Read data from ROM at offset BX

; Store data in RAM at address 8000H


MOV AX, 8000H ; Load starting address of RAM
MOV DS, AX ; Set data segment to RAM
MOV [BX], AL ; Write data to RAM at offset BX

Debugging Tips

1. Ensure proper timing alignment between microprocessor and memory.


2. Check CS signals with an oscilloscope to verify memory selection.
3. Confirm that latching of address/data lines works correctly.

This setup forms the foundation for interfacing memory with a microprocessor and can be
adapted for larger or more complex memory systems.

Interfacing LEDs with a microprocessor (e.g., 8086) involves controlling LEDs through the
microprocessor's output ports. Here's how to design and implement LED interfacing:

Components Needed

1. Microprocessor (e.g., 8086)


2. LEDs: Light Emitting Diodes
3. Resistors: Typically 330 Ω330 \, \Omega to 1 kΩ1 \, k\Omega to limit current
4. Output Port (e.g., 8255 PPI - Programmable Peripheral Interface)

Basic Concepts

 LEDs are diodes that emit light when current flows through them. They are usually
connected with a resistor to limit current and prevent damage.
 LEDs can be connected in:
o Active High Configuration: LED turns ON when the output is HIGH (logic 1).
o Active Low Configuration: LED turns ON when the output is LOW (logic 0).

Interfacing LEDs Directly to Output Port

1. Connection:
o Connect the cathode (negative) of the LED to the port pin (e.g., P0 of 8255).
o Connect the anode (positive) of the LED to the power supply (+5 V+5 \, V)
through a resistor.
o Alternatively, for an active low configuration, connect the anode to the port pin
and the cathode to ground through a resistor.
2. Control Signals:
o To turn the LED ON, send logic HIGH or LOW depending on the configuration.
o To turn the LED OFF, send the opposite logic.

Using 8255 PPI for Interfacing

The 8255 Programmable Peripheral Interface is commonly used for I/O port interfacing.
Connection Diagram

 8255 Ports: Connect LEDs to one of the ports (e.g., Port A).
 Control Word Register: Configure the port as an output port.
 Resistors: Connect series resistors with each LED.

Assembly Code for LED Interfacing

Turning ON and OFF LEDs

; Assume LEDs connected to Port A of 8255


MOV AL, 80H ; Control word: Configure Port A as output
OUT 03H, AL ; Send control word to 8255 control register (address
03H)

MOV AL, 0FFH ; Turn ON all LEDs (active low configuration)


OUT 00H, AL ; Send data to Port A (address 00H)

MOV AL, 00H ; Turn OFF all LEDs (active low configuration)
OUT 00H, AL ; Send data to Port A

Flashing LEDs

To make the LEDs blink at a specific interval:

1. Delay Subroutine: Add a loop to introduce delay.


2. Code Example:

; Blinking LEDs connected to Port A


MOV AL, 80H ; Configure Port A as output
OUT 03H, AL ; Send control word to control register

START:
MOV AL, 0FFH ; Turn ON all LEDs
OUT 00H, AL ; Send data to Port A
CALL DELAY ; Call delay subroutine

MOV AL, 00H ; Turn OFF all LEDs


OUT 00H, AL ; Send data to Port A
CALL DELAY ; Call delay subroutine

JMP START ; Repeat forever

DELAY:
MOV CX, 0FFFFH ; Load delay count
WAIT:
LOOP WAIT ; Decrement CX until zero
RET
Circuit Design

1. Active High Configuration:


o Anode: Connect to +5 V+5 \, V through a 330 Ω330 \, \Omega resistor.
o Cathode: Connect to the port pin.
2. Active Low Configuration:
o Anode: Connect to the port pin through a resistor.
o Cathode: Connect to GNDGND.

Debugging Tips

1. Check Current: Ensure the resistor value limits current within the LED's specifications
(typically 10-20 mA).
2. Test Connections: Verify the wiring between the LED, resistor, and port pin.
3. Port Initialization: Ensure the port is configured correctly as an output port.
4. Use Multimeter: Check the voltage across the LED to confirm proper operation.

Practical Applications

 Indicator Lights: Show the status of microprocessor operations.


 Pattern Display: Use multiple LEDs to display patterns or sequences.
 Debugging: Indicate program execution stages or errors.

This setup allows effective control and monitoring of LEDs through a microprocessor.

Interfacing an LCD (Liquid Crystal Display) with a microprocessor like the 8086 involves
connecting the LCD to the microprocessor's data and control lines, ensuring proper timing and
data transfer to display text or graphical information. Here's a comprehensive guide:

LCD Basics

1. Types of LCDs:
o Character LCDs (e.g., 16x2, 20x4): Display text characters in rows and columns.
o Graphical LCDs (e.g., 128x64): Display pixels for custom graphics.
2. Common LCD Controller:
o Character LCDs often use the Hitachi HD44780 controller.
o These LCDs operate in 4-bit or 8-bit mode for data transfer.
3. Key Signals:
o RS (Register Select): Chooses between Command (RS=0) or Data (RS=1).
o R/W (Read/Write): Selects Read (R/W=1) or Write (R/W=0) mode.
o E (Enable): A high-to-low pulse triggers data read/write.
o D0-D7: Data pins for sending commands or data.
o VCC: Power supply (typically 5V).
o VEE: Contrast adjustment (connected to a potentiometer).

Connections for 8086

1. Data Bus:
o Connect LCD data pins (D0-D7 for 8-bit mode or D4-D7 for 4-bit mode) to the
lower 8 bits of the microprocessor's data bus.
o For 4-bit mode, data is sent in two nibbles (higher nibble first).
2. Control Signals:
o RS, R/W, and E are connected to microprocessor I/O ports for control.
3. Address Decoder:
o Use an address decoder (e.g., 74LS138) to generate the CS (Chip Select) signal
for the LCD.
4. Power and Contrast:
o Connect VCC and GND for power.
o Use a 10kΩ potentiometer to adjust the contrast through the VEE pin.

Command and Data Instructions

To control the LCD, you send commands and data:

1. Commands:
o Clear Display: 0x01
o Return Home: 0x02
o Entry Mode Set: 0x06 (Increment cursor).
o Display ON/OFF: 0x0C (Display ON, Cursor OFF).
o Function Set: 0x38 (8-bit mode, 2 lines, 5x7 font).
2. Data:
o ASCII values of characters to display.

Timing Requirements

1. Enable Pulse Width: Minimum 450ns.


2. Command Execution Time: Typically 37μs, but Clear Display and Return Home can
take up to 1.52ms.
3. Inter-instruction Delay: Ensure a delay after each command or data transfer.

Steps to Interface LCD with 8086

1. Initialize the LCD:


o Send initialization commands to configure the LCD in 4-bit or 8-bit mode.
2. Send Commands:
o Use the RS signal to distinguish commands from data.
3. Send Data:
o Write ASCII characters to display text.

Circuit Diagram

Connections:

 LCD D0-D7 → 8086 Data Bus D0-D7.


 LCD RS, R/W, E → I/O pins of the microprocessor (via address decoding if needed).
 LCD VEE → Potentiometer (contrast adjustment).
 LCD VCC, GND → Power supply.

Assembly Code Example

Functionality: Display "HELLO" on a 16x2 LCD.

DATA SEGMENT
MSG DB 'HELLO', 0 ; String to display
DATA ENDS

CODE SEGMENT
START:
MOV AX, DATA ; Load data segment
MOV DS, AX

; Initialize LCD
CALL LCD_INIT

; Display string
LEA SI, MSG ; Load address of string
DISPLAY_LOOP:
LODSB ; Load next character in AL
CMP AL, 0 ; Check for null terminator
JE END_DISPLAY ; Exit if end of string
CALL LCD_WRITE_DATA ; Write character to LCD
JMP DISPLAY_LOOP ; Repeat

END_DISPLAY:
MOV AH, 4CH ; Exit to DOS
INT 21H

; Subroutine: Initialize LCD


LCD_INIT PROC
MOV AL, 38H ; Function set: 8-bit, 2-line, 5x7 font
CALL LCD_WRITE_CMD
MOV AL, 0CH ; Display ON, Cursor OFF
CALL LCD_WRITE_CMD
MOV AL, 06H ; Entry mode set: Increment cursor
CALL LCD_WRITE_CMD
MOV AL, 01H ; Clear display
CALL LCD_WRITE_CMD
RET
LCD_INIT ENDP

; Subroutine: Write Command to LCD


LCD_WRITE_CMD PROC
MOV DX, LCD_CMD_PORT ; Command port address
OUT DX, AL ; Send command
CALL LCD_ENABLE_PULSE
RET
LCD_WRITE_CMD ENDP

; Subroutine: Write Data to LCD


LCD_WRITE_DATA PROC
MOV DX, LCD_DATA_PORT ; Data port address
OUT DX, AL ; Send data
CALL LCD_ENABLE_PULSE
RET
LCD_WRITE_DATA ENDP

; Subroutine: Generate Enable Pulse


LCD_ENABLE_PULSE PROC
; Generate high-to-low pulse on Enable pin
; Add delay here if required
RET
LCD_ENABLE_PULSE ENDP

CODE ENDS
END START

Tips

1. Delay Management:
o Ensure proper delays between commands and data writes to meet the LCD timing
requirements.
2. Address Mapping:
o Use proper memory mapping for control and data registers.
3. Debugging:
o Verify signal levels on RS, R/W, and E pins with an oscilloscope.
o Check contrast adjustment for proper visibility.

This code can be extended to support additional functionalities like cursor positioning, scrolling,
and custom character design.

Interfacing an Analog-to-Digital Converter (ADC) with a microprocessor like the 8086 involves
converting analog signals (from sensors or other sources) into digital data that the
microprocessor can process. Below is a detailed explanation of how to interface ADCs with a
microprocessor.

Key Components

1. ADC (Analog-to-Digital Converter):


o Converts analog input signals into digital outputs.
o Examples: ADC0804 (8-bit), ADC0809 (8-bit, 8-channel).
2. Microprocessor (8086):
o Processes the digital data provided by the ADC.
3. Control Signals:
o Start of Conversion (SOC): Initiates the conversion process.
o End of Conversion (EOC): Indicates the conversion is complete.
o Output Enable (OE): Enables the ADC's digital output for reading.
4. Multiplexer (Optional):
o If the ADC has multiple input channels (e.g., ADC0809), a multiplexer selects the desired
analog input.
5. Data Bus:
o Transfers the digital output from the ADC to the microprocessor.

Steps to Interface ADC with Microprocessor

1. ADC Initialization

 Configure the ADC pins, including SOC, EOC, and OE.


 If the ADC has a multiplexer (like ADC0809), set the channel selection bits.

2. Start the Conversion

 The microprocessor sends a signal (pulse) on the SOC pin to start the conversion.

3. Wait for Conversion


 The ADC asserts the EOC pin to indicate that the conversion is complete.
 The microprocessor can poll the EOC signal or use an interrupt to detect conversion completion.

4. Read Digital Data

 After the conversion, the microprocessor enables the OE pin to read the digital output from the
ADC.
 The digital data is read via the data bus (D0-D7 for an 8-bit ADC).

Interfacing Circuit Example

Using ADC0804

 ADC0804 is a commonly used 8-bit ADC with a single channel.

Connections:

1. Analog Input (Vin): Connect the analog signal (e.g., from a sensor) to the Vin pin.
2. Control Pins:
o SOC: Connected to a microprocessor output pin to start the conversion.
o EOC: Connected to a microprocessor input pin to monitor the conversion status.
o OE: Connected to a microprocessor output pin to enable reading data.
3. Data Bus (D0-D7): Connected to the microprocessor's data bus.

Sample Assembly Code for Interfacing ADC0804

Algorithm:

1. Set SOC to start the conversion.


2. Wait for EOC to go high (polling).
3. Enable OE to read the digital output.
4. Store the result in memory.

; Assume ADC connected to port addresses


; SOC at port 0x20, EOC at port 0x21, Data at port 0x22

MOV DX, 20H ; Port address for SOC


MOV AL, 01H ; Start conversion (set SOC high)
OUT DX, AL ; Send pulse to start conversion

WAIT_EOC:
MOV DX, 21H ; Port address for EOC
IN AL, DX ; Read EOC status
TEST AL, 01H ; Check if EOC is high
JZ WAIT_EOC ; Wait until EOC is high

MOV DX, 22H ; Port address for data


IN AL, DX ; Read ADC data
MOV [RESULT], AL ; Store the result in memory

HLT ; Halt execution

Interfacing with ADC0809 (Multi-Channel ADC)

Key Features:

 8 analog input channels.


 Requires a multiplexer address for channel selection.

Steps:

1. Configure the channel selection using address pins A, B, and C.


2. Send a pulse on SOC to start conversion.
3. Wait for EOC to go high.
4. Enable OE and read the data.

Channel Selection Table for ADC0809

Channel A (Pin 23) B (Pin 24) C (Pin 25)

IN0 0 0 0

IN1 1 0 0

IN2 0 1 0

IN3 1 1 0

IN4 0 0 1

IN5 1 0 1

IN6 0 1 1

IN7 1 1 1

Sample Code for ADC0809


; Assume SOC at port 0x30, EOC at port 0x31, Data at port 0x32, and Channel
Select at 0x33

MOV DX, 33H ; Port for channel select


MOV AL, 05H ; Select channel IN5 (binary 101)
OUT DX, AL ; Send channel selection

MOV DX, 30H ; Port address for SOC


MOV AL, 01H ; Start conversion
OUT DX, AL ; Send SOC pulse

WAIT_EOC:
MOV DX, 31H ; Port address for EOC
IN AL, DX ; Read EOC status
TEST AL, 01H ; Check if EOC is high
JZ WAIT_EOC ; Wait until EOC is high

MOV DX, 32H ; Port address for data


IN AL, DX ; Read ADC data
MOV [RESULT], AL ; Store the result in memory

HLT ; Halt execution

Debugging Tips

1. Ensure proper clock signal and reference voltage for the ADC.
2. Check SOC and EOC signals using an oscilloscope.
3. Verify the timing requirements of the ADC (e.g., conversion time).
4. Ensure proper pull-up/pull-down resistors for open-drain signals (if required).

This setup allows efficient communication between the microprocessor and ADC for various
applications like sensor interfacing, data acquisition systems, and embedded control systems.

Interfacing a microprocessor like the 8086 with a Digital-to-Analog Converter (DAC) is


common in applications requiring analog signal generation, such as audio synthesis, waveform
generation, or motor control. Below is a detailed explanation and example of how to interface a
DAC with a microprocessor.

Key Concepts of DAC Interfacing

1. Digital-to-Analog Conversion:
o A DAC converts a digital binary value provided by the microprocessor into a
proportional analog voltage or current.
2. Key Components:
o Microprocessor (e.g., 8086): Provides digital data.
o DAC (e.g., DAC0808): Converts digital data to analog.
o Latch (e.g., 74LS373): Temporarily stores the digital data sent from the
microprocessor.
o Operational Amplifier: May be required to scale or buffer the analog output of
the DAC.
3. Control Signals:
o WR (Write): Triggers the DAC to latch and convert data.
o CS (Chip Select): Enables the DAC for communication.
o READY: Optional, used if the DAC is slower than the processor.

Interfacing Steps

1. Address Decoding

 The DAC is mapped to a specific address in the microprocessor’s I/O space or memory
space.
 An address decoder (e.g., 74LS138) generates the CS signal based on the high-order
address lines.

2. Data Transfer

 The microprocessor sends an 8-bit digital value through its data bus to the DAC.
 A latch (like 74LS373) is used to hold the data temporarily for stability during the write
operation.

3. Control Signals

 WR signal is used to inform the DAC to accept the data for conversion.
 ALE may be used to demultiplex address/data buses in microprocessors like the 8086.

Circuit Diagram Description

1. Microprocessor (8086):
o Data bus (D0-D7) is connected to the input of the DAC (through a latch if
needed).
o Address bus (A0-A19) is connected to the address decoder for generating the
DAC's CS signal.
2. DAC (e.g., DAC0808):
o Digital input lines connect to the data bus.
o Analog output voltage is available at the DAC's output pin (requires external
scaling via op-amp).
3. Control Signals:
o The address decoder generates CS for the DAC.
o The WR signal triggers the DAC to latch the data and start conversion.

Example Circuit

Connections:

1. Data Bus:
o Connect 8086 data lines (D0-D7) to the digital input of the DAC (or through a
latch for stability).
2. Address Decoder:
o Use address lines (e.g., A15-A19) to generate the CS signal for the DAC.
3. Control Lines:
o WR is connected directly to the DAC's write-enable pin.
4. Op-Amp:
o Connect the DAC's analog output to an operational amplifier to scale or buffer the
output voltage.

Assembly Program Example

This program generates a ramp waveform by sending increasing digital values to the DAC.

ORG 1000H

MOV DX, 0FF00H ; Port address of DAC


MOV AL, 00H ; Start with 0
MOV CX, 256 ; Loop for 256 values

RAMP_LOOP:
OUT DX, AL ; Send digital value to DAC
INC AL ; Increment digital value
LOOP RAMP_LOOP ; Repeat for 256 values

HLT ; Halt execution


END

Waveform Generation Examples

1. Sine Wave:
o Precompute sine values in memory.
o Send them to the DAC in sequence.
2. Square Wave:
o Alternate between two values (e.g., 00H and FFH) at regular intervals.
3. Triangular Wave:
o Increment digital values up to FFH, then decrement back to 00H.

Debugging Tips

1. Verify the CS and WR signals using an oscilloscope to ensure proper timing.


2. Check the output voltage of the DAC with a multimeter or oscilloscope.
3. Ensure the address decoder generates the correct select signals based on the programmed
address.

Applications

1. Signal and waveform generation.


2. Audio output for sound cards.
3. Analog control for motors or actuators.

This setup provides the foundation for interfacing DACs with microprocessors, enabling the
conversion of digital data into real-world analog signals.

Interfacing a microprocessor like the 8086 with a keyboard involves detecting keypresses,
reading their binary representation, and processing them. This process requires both hardware
and software considerations. Below is a comprehensive guide to interfacing with keyboards.

Types of Keyboards

1. Matrix Keyboards:
o A grid of rows and columns where keys are placed at intersections.
o Pressing a key connects a specific row and column, enabling the microprocessor
to identify the pressed key.
2. Encoded Keyboards:
o These keyboards have built-in circuitry to send a unique code (scan code) for each
key press.
o Often used with standard PC keyboards.

Key Components

1. Microprocessor (e.g., 8086): Reads keypresses and processes them.


2. Keyboard: Provides input.
3. Keyboard Controller (e.g., 8255 PPI or 8279 Keyboard/Display Controller):
o Simplifies the interfacing process by handling scanning and debouncing.
4. Address Decoder: Selects the keyboard or controller when the microprocessor accesses a
specific memory or I/O address.
5. Buffers and Latches: Temporarily hold the key data for the microprocessor to read.

Matrix Keyboard Interfacing

Hardware Setup

1. Matrix Layout:
o Rows connected to the microprocessor's output lines.
o Columns connected to the microprocessor's input lines (with pull-up resistors).
2. Scanning Mechanism:
o The microprocessor drives one row low at a time.
o It reads the columns to detect which key in that row is pressed.
3. Debouncing:
o Mechanical keys may generate multiple signals due to bouncing.
o Software or hardware debounce circuits ensure only one signal is registered per
key press.

Software Flow

1. Drive one row low, keeping all others high.


2. Check the column lines for a low signal, indicating a key press.
3. Identify the key based on the active row and column.
4. Repeat for all rows to scan the entire keyboard.

Assembly Code Example

ORG 1000H

; Assume keyboard rows connected to port 0FF00H and columns to 0FF01H


MOV DX, 0FF00H ; Address for row output
MOV CX, 4 ; Number of rows

SCAN_LOOP:
MOV AL, 11111110B ; Activate first row (set first bit low)
OUT DX, AL ; Send row signal
MOV DX, 0FF01H ; Address for column input
IN AL, DX ; Read column signal
CMP AL, 11111111B ; Check if any key is pressed
JE NEXT_ROW ; If no key pressed, check next row

; Decode key (row and column can be used to identify the key)
; ... (Add key processing logic here)

NEXT_ROW:
ROR AL, 1 ; Shift to activate the next row
LOOP SCAN_LOOP
HLT
END

Encoded Keyboard Interfacing

Hardware Setup

 Use an encoded keyboard that sends a unique scan code for each key.
 Connect the keyboard's output to the microprocessor via an interface (e.g., 8255 or 8279).

Keyboard Controller (8279)

 The 8279 controller simplifies the keyboard interface by:


o Handling key scanning.
o Providing debouncing.
o Sending keypress data to the microprocessor.

Connections

1. Connect the keyboard to the 8279's rows and columns.


2. Connect the 8279's data lines to the microprocessor's data bus.
3. Use address decoding to map the 8279 to a specific address range.

Assembly Code Example with 8279

ORG 1000H

MOV DX, 0FF00H ; Address of 8279 status register


WAIT_FOR_KEY:
IN AL, DX ; Read status
TEST AL, 01H ; Check if key pressed
JZ WAIT_FOR_KEY ; Wait if no key pressed

MOV DX, 0FF01H ; Address of 8279 data register


IN AL, DX ; Read key scan code
; Process the scan code (AL now contains the key code)

HLT
END

PC Keyboard Interfacing

For modern PC keyboards:

1. The keyboard sends scan codes through a serial communication protocol.


2. A UART or similar serial interface can be used to read the data.
Challenges

1. Debouncing: Avoid registering multiple signals for a single press.


2. Ghosting: Occurs when multiple keys are pressed simultaneously, causing ambiguity.
o Mitigated by using diodes in the matrix or by software handling.

Applications

1. Keyboards for Embedded Systems: User input for devices like calculators or industrial
controls.
2. Custom Input Devices: Gaming or specialized input hardware.
3. Human-Machine Interface (HMI): For controlling robots, machinery, or other systems.

By employing these principles, microprocessors can interface effectively with various types of
keyboards, providing robust input functionality for applications.

You might also like