Unit 3-4 Mpi Codes and Extra Coding Related Content
Unit 3-4 Mpi Codes and Extra Coding Related Content
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
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
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
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
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
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.
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
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
; Exit to DOS
MOV AH, 4CH
INT 21H
CODE ENDS
END START
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
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
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
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
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
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
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
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
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
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
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
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
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
FOUND_LENGTH:
DEC SI ; Move SI to the last character
DEC CX ; Exclude '$'
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
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
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
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
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:
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
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.
Latch (74LS373)
Debugging Tips
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
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).
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.
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.
MOV AL, 00H ; Turn OFF all LEDs (active low configuration)
OUT 00H, AL ; Send data to Port A
Flashing LEDs
START:
MOV AL, 0FFH ; Turn ON all LEDs
OUT 00H, AL ; Send data to Port A
CALL DELAY ; Call delay subroutine
DELAY:
MOV CX, 0FFFFH ; Load delay count
WAIT:
LOOP WAIT ; Decrement CX until zero
RET
Circuit Design
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
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).
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.
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
Circuit Diagram
Connections:
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
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 Initialization
The microprocessor sends a signal (pulse) on the SOC pin to start the conversion.
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).
Using ADC0804
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.
Algorithm:
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
Key Features:
Steps:
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
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
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.
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.
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.
This program generates a ramp waveform by sending increasing digital values to the DAC.
ORG 1000H
RAMP_LOOP:
OUT DX, AL ; Send digital value to DAC
INC AL ; Increment digital value
LOOP RAMP_LOOP ; Repeat for 256 values
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
Applications
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
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
ORG 1000H
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
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).
Connections
ORG 1000H
HLT
END
PC Keyboard Interfacing
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.