You are on page 1of 67

MP&MC LAB MANUAL

Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Program No: 1 Introduction to MASM /TASM

MASM: (Microsoft assembler)

To Create Source File: An editor is a program which allows you to create a file containing
the assembly language statements for your program. This file is called a source file.
Command to create a source file

C:\MASM\BIN> Edit filename. asm

The next step is to process the source file with an assembler. When you run the
assembler, it reads the source file of your program. On the first pass through the source
program, the assembler determines the displacement of named data items, the offset labels,
etc. and puts this information in a symbol table. On the second pass through the source
program the assembler produces the binary code for each instruction and inserts the offsets,
etc. that it calculated during first pass.

C:\MASM\BIN > Masm filename. asm X, Y, Z

With this command assembler generates three files.

1. The first file (X) called the object file, is given the extension .OBJ
The object file contains the binary codes for the instructions and information about the
addresses of the instructions.

2. The second file (Y) generated by the assembler is called the assembler list file and is given
the extension .LST. The list file contains your assembly language statements, the binary
codes for each instruction and the offset for each instruction.

1
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

3. The third file (Z) generated by this assembler is called the cross-reference file and is given
the extension .CRF. The cross-reference file lists all labels and pertinent information required
for cross – referencing

NOTE : The Assembler only finds syntax errors : It will not tell you whether program does
what it is supposed to do. To determine whether your program works, you have to run the
program and test it.

Next step is to process the object file with linker.

C:\MASM\BIN>LINK filename . obj

Run File [ Filename1.exe] : “filename1.exe”


List file [ nul.map] : NUL
Libraries [.lib] : library_name
Definitions File [ nul.def] :

Creation of Library: Refer Modular Programming Section


A Linker is a program used to join several object files into one layer object file

NOTE : On IBM PC – type Computers, You must run the LINK program on your .OBJ file
even if it contains only one assembly module. The linker produces a link file with the .EXE
extension (an execution file)

The linker produces a link file with the .EXE extension (an execution file)

Next Run: C:\TASM\BIN> CV filename.exe

2
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Program No: 2 (a) MULTY BYTE ADDITION

DATA SEGMENT
N1 DB 55H, 66H, 77H
N2 DB 11H, 22H, 33H
RESULT DB 3H DUP (00)
DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START: MOV AX, DATA
MOV DS, AX
MOV SI, OFFSET N1
MOV DI, OFFSET N2
MOV BX, OFFSET RESULT
CLC
MOV CX, 0003H
MOV AX, 0000H
BACK: MOV AL, [SI]
MOV DL, [DI]
ADC AL, DL
MOV [BX], AL
INC SI
INC DI
INC BX
DEC CX
JNZ BACK
MOV AH, 4CH
INT 21H
INT 3H

3
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

CODE ENDS
END START

RESULT: 55H 66H 44H


11H 22H 33H
66H 88H 77H

----------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------

Program No: 2(b) MULTY BYTE SUBTRACTION

DATA SEGMENT
N1 DB 55H, 66H, 77H, 88H
N2 DB 11H, 22H, 33H, 44H
RESULT DB 4H DUP(00)
DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA

START: MOV AX, DATA


MOV DS, AX
MOV SI, OFFSET N1
MOV DI, OFFSET N2
MOV BX, OFFSET RESULT
CLC
MOV CX, 0004H
MOV AX, 0000H
BACK: MOV AL, [SI]

4
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

MOV DL, [DI]


SBB AL, DL
MOV [BX], AL
INC SI
INC DI
INC BX
LOOP BACK
MOV AH, 4CH
INT 21H
INT 3H

CODE ENDS
END START

RESULT: 55H 66H 77H 88H


11H 22H 33H 44H
44H 44H 44H 44H

***************************************************************************
***************************************************************************

Program No: 2(c) MULTY BYTE MULTIPLICATION

DATA SEGMENT
N1 DB 05H, 04H, 02H
N2 DB 01H, 02H, 03H
RESULT DB 4H DUP (00)
DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA

5
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

START: MOV AX, DATA


MOV DS, AX
MOV SI, OFFSET N1
MOV DI, OFFSET N2
MOV BX, OFFSET RESULT
MOV CL, 03H
MOV AX, 0000H
MOV DX, 0000H
BACK: MOV AL, [SI]
MOV CH, [DI]
MUL DH
MOV [BX], AL
INC SI
INC DI
INC BX
LOOP BACK
MOV AH, 4CH
INT 21H
INT 3H

CODE ENDS
END START

RESULT: 05H 04H 02H


01H 02H 03H
05H 08H 06H

6
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Program No: 2 (d) MULTY BYTE DIVISION


DATA SEGMENT
N1 DB 55H, 66H, 99H
N2 DB 11H, 22H, 33H
RESULT DB 3H DUP(00)
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
MOV SI, OFFSET N1
MOV DI, OFFSET N2
MOV BX, OFFSET RESULT
MOV CL, 03H
MOV AX, 0000H
MOV DX, 0000H
BACK: MOV AL, [SI]
MOV CH, [DI]
DIV CH
MOV [BX], AL
INC SI
INC DI
INC BX
LOOP BACK
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

7
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

RESULT: 55H 66H 99H


11H 02H 03H
05H 33H 33H

***************************************************************************
***************************************************************************

Program No: 3(a) SIGNED MULTIPLICATION

DATA SEGMENT
N1 DB 09H
N2 DB 02H
N3 DB 02H DUP(00)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
SUB AX, AX
MOV AL, N1
MOV CH, N2
MOV BX, OFFSET N3
IMUL CH
MOV [BX], AL
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

8
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

RESULT: 09H
02H
18H
***************************************************************************
***************************************************************************
Program No: 3(b) SIGNED DIVISION
DATA SEGMENT
N1 DB 40H
N2 DB 20H
N3 DB 02H DUP (00H)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV AL, N1
CBW
MOV CH, N2
MOV BX, OFFSET N3
IDIV CH
MOV [BX], AL
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT: 40H
20H
800H

9
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Program No: 4(a) ASCII ADDITION

DATA SEGMENT
N1 DB 14H
N2 DB 04H
N3 DB 2H DUP (00)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV AL, N1
MOV CL, N2
MOV BX, OFFSET N3
ADD AL, CL
AAA
MOV [BX], AL
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT:

***************************************************************************
***************************************************************************

10
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Program No: 4(b) ASCII SUBTRACTION

DATA SEGMENT
N1 DB 04H
N2 DB 02H
N3 DB 02H DUP (00)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV AL, N1
MOV BX, OFFSET N3
SUB AL, N2
AAS
MOV [DL], AL
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT:

***************************************************************************
***************************************************************************

11
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Program No: 4(c) ASCII MULTIPLICATION

DATA SEGMENT
N1 DB 04H
N2 DB 03H
N3 DB 02H DUP(00)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV AL, N1
MOV BX, OFFSET N3
MUL N2
AAM
MOV [BL], AL
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT:

***************************************************************************
***************************************************************************

12
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Program No: 4(d) ASCII DIVISION

DATA SEGMENT
N1 DB 06H
N2 DB 04H
N3 DB 02H DUP (00)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV AL, N1
MOV BX, N3
AAD
DIV N2
MOV [BX], AL
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT:

***************************************************************************
***************************************************************************

13
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Program No: 5(a) PACKED TO UNPACKED BCD

DATA SEGMENT
N1 DB 56H, 49H, 33H
N2 DB 06H DUP(00)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV SI, OFFSET N1
MOV DI, OFFSET N2
MOV CX, 0003H
BACK: MOV AL, [SI]
MOV BL, AL
AND AL, 0F0H
MOV CL, 04H
ROR BL, CL
AND BL, 0FH
MOV [DI], AL
INC DI
MOV [DI], BL
INC SI
INC DI
DEC CX
JNZ BACK
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS

14
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

END START

RESULT:
I/P : 56H 49H 33H
O/P : 05H,06H, 04H,09H, 03H,03H
***************************************************************************
***************************************************************************
Program No: 5(b) UNPACKED BCD TO PACKED

DATA SEGMENT
N1 DB 05H, 06H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV AL, N1
MOV BL, AL
MOV CL, 04H
ROR BL, CL
OR AL, BL
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT:
I/P : 05H 06H
O/P : 56H

15
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Program No: 5(C) BCD to ASCII CONVERSION

DATA SEGMENT
N1 DB 56H
N2 DB 02H DUP (00)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV AL, N1
MOV SI, OFFSET N2
MOV BL, AL
AND AL, 0F0H
ADD AL, 30H
MOV CL, 4H
ROR BL, CL
AND BL, 0FH
ADD BL, 30H
MOV [SI] , BL
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT:
I/P : 56H
O/P: 35H,36H
***************************************************************************

16
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Program No: 5(d) ASCII to BCD CONVERSION

DATA SEGMENT
N1 DB 35H
N2 DB 02H DUP (00)
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV AL, N1
AND AL, 0FH
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT:
I/P : 35H
O/P : 05H

***************************************************************************
***************************************************************************
Application Programs:

Program No: 6 (a) EVEN AND ODD NUMBERS

DATA SEGMENT
N1 DB 56H, 49H, 33H

17
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV SI, OFFSET N1
MOV DX, 0000H
MOV BX, 0000H
MOV CX, 0003H
BACK: MOV AL, [SI]
ROR AL, 01H
JC X
INC BX
JMP Y
X: INC DX
Y: INC SI
DEC CX
JNZ BACK
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT:
I/P : 56H 49H 33H
O/P : BX=0001H,DX=0002H

***************************************************************************
***************************************************************************

18
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Programm:6 (b) POSITIVE AND NEGATIVE NUMBER

DATA SEGMENT
N1 DB 51H, 20H, 33H,80H,19H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV SI, OFFSET N1
MOV DX, 0000H
MOV BX, 0000H
MOV CX, 0005H
BACK: MOV AL, [SI]
ROL AL, 01H
JC X
INC BX
JMP Y
X: INC DX
Y: INC SI
DEC CX
JNZ BACK
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

19
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

RESULT:
I/P : 56H 49H 33H
O/P : BX=0004H,DX=0001H

***************************************************************************
***************************************************************************
Program No: 7(a) ASCENDING ORDER

DATA SEGMENT
N1 DB 56H, 49H, 33H,05H,12H,17H,08H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV BX, 0006H
Z: MOV SI, OFFSET N1
MOV CX, 0006H
BACK: MOV AL, [SI]
INC SI
CMP AL,[SI]
JBE Y
XCHG AL,[SI]
DEC SI
MOV [SI], AL
INC SI
Y: DEC CX
JNZ BACK
DEC BX
JNZ Z

20
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

MOV AH, 4CH


INT 21H
INT 3H
CODE ENDS
END START
RESULT:
I/P : 56H, 49H, 33H,05H,12H,17H,08H
O/P : 05H, 08H,12H,17H, 33H, 49H, 56H

***************************************************************************
***************************************************************************
Program No: 7(b) DESCENDING ORDER

DATA SEGMENT
N1 DB 56H, 49H, 33H,05H,12H,17H,08H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
XOR AX, AX
MOV BX, 0006H
Z: MOV SI, OFFSET N1
MOV CX, 0006H
BACK: MOV AL, [SI]
INC SI
CMP AL,[SI]
JAE Y
XCHG AL,[SI]
DEC SI
MOV [SI],AL

21
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

INC SI
Y: DEC CX
JNZ BACK
DEC BX
JNZ Z
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT:
I/P : 56H, 49H, 33H,05H,12H,17H,08H
O/P : 56H, 49H,33H,17H,12H,08H,05H

***************************************************************************
***************************************************************************
Program No: 8 BLOCK TRANSFER

DATA SEGMENT
N1 DB 01H,02H,03H
DATA ENDS
EXTRA SEGMENT
N2 DB 03H DUP (00)
EXTRA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA, ES:EXTRA
START: MOV AX, DATA
MOV DS, AX
MOV AX, EXTRA
MOV ES, AX

22
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

MOV SI, OFFSET N1


MOV DI, OFFSET N2
CLD
MOV CX, 0003H
REP MOVSB
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT:
I/P : 01H, 02H, 03H
O/P : 01H, 02H, 03H

***************************************************************************
***************************************************************************
Program No: 9(a) READ A CHARACTER WITH & WITH OUT ECHO

ASSUME CS:CODE,
START : MOV AH, 01H
INT 21H
MOV AH, 4CH
INT 21H
INT 3H
CODE END
END START

RESULT:
INPUT : ‘a’
OUPUT:’a’

23
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

ASSUME CS:CODE,
START : MOV AH, 07H
INT 21H
MOV AH, 4CH
INT 21H
INT 3H
CODE END
END START

RESULT:
INPUT: ‘a’
OUPUT:
***************************************************************************
***************************************************************************
Program No: 9(b) DISPLAY CHARACTER

ASSUME CS CODE:
START: MOV AX,DATA
MOV DS, AX
MOV CX, 0005H
X: MOV AH, 02H
MOV DL, ‘Z’
INT 21H
MOV AH, 02H
MOV DL, 20H
INT 21H
DEC CX
JNZ X
INT3H
CODE END

24
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

END START

RESULT:
INPUT : ‘Z’
OUTPUT : Z Z Z Z Z

***************************************************************************
***************************************************************************
Program No:( 10) STRING REVERSAL

DATA SEGMENT
N1 DB ‘MLRIT$’
N2 DB 05H DUP (00)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
MOV SI, OFFSET N1
MOV DI, OFFSET N2
ADD DI, 0005H
CLD
MOV AH, ‘$’
X: CMP AH, [SI]
JE Y
MOV AL, [SI]
MOV [DI], AL
INC SI
DEC DI
LOOP X

25
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

MOV AH, 4CH


INT 21H
Y: INT 3H
CODE ENDS
END START

RESULT:

***************************************************************************
***************************************************************************
Program No: 11 LENGTH OF THE STRING

DATA SEGMENT
STRING1 DB 'EMPTY VESSELS MAKE MORE NOISE$'
STRLEN EQU ($-STRING1)
RES DB 0
CORT DB 'STRLENGTH FOUND CORRECT$'
INCORT DB 'STRLENGTH FOUND INCORRECT$'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA
MOV DS, AX
SUB CL, CL
MOV BL, STRLEN
MOV SI, OFFSET STRING1
BACK: LODSB
INC CL
CMP AL,'$'

26
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

JNZ BACK
MOV RES, CL
CMP CL, BL
JZ CORRECT
MOV DX, OFFSET INCORT
MOV AH, 09
INT 21H
CORRECT: MOV DX, OFFSET CORT
MOV AH, 09
INT 21H
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT:

***************************************************************************
***************************************************************************
Program No: 12 STRING COMPARISION

DATA SEGMENT
STRING1 DB 'EMPTY'
STRLEN EQU ($-STRING1)
NOTSFUL DB 'STRINGS ARE UNEQUAL$'
SFUL DB 'STRINGS ARE EQUAL$'
DATA ENDS
EXTRA SEGMENT
STRING2 DB 'EMPTY'

27
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

EXTRA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA, ES:EXTRA
START: MOV AX, DATA
MOV DS, AX
MOV AX, EXTRA
MOV ES, AX
MOV SI, OFFSET STRING1
MOV DI, OFFSET STRING2
CLD
MOV CX,LENGTH STRING1
MOV CX, STRLEN
REP CMPSB
JZ FORW
MOV AH, 09H
MOV DX, OFFSET NOTSFUL
INT 21H
JMP EXITP
FORW:MOV AH,09H
MOV DX, OFFSET SFUL
INT 21H
EXITP:
NOP
MOV AH, 4CH
INT 21H
INT 3H
CODE ENDS
END START

RESULT:

28
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

II.INTERFACING

1. 8259 Interfacing:

DESCRIPTION OF THE MODULE :

 The Study module card is connected to the 8086 kit through a 50 pin FRC cable.
Before making connections check the polarity of the cable.
switches, debounce switches for single stepping, Buffers and Tags for applying
interrupts, Vcc TAGS, LEDS To Display status.

 The toggle switch at top is for enabling single stepping. Push to ON for single
stepping of every instruction. When single stepping enable will show each data
transferred in an instruction on the data bus, including chip select, Read, Write,
Interrupt and Interrupt Acknowledge Signals.

 8259 is connected to IR4

DEMONSTRATION:
This is a demonstration in which 8259 will be used in the stand alone mode. Program 1 will
be used to illustrate this concept. In the main loop of program, it will be displaying (HELP),
and when L to H pulse is applied to IRO line using patch cord in the ISR, it displays “IRO”

Step 1

Connect the 8259 PIC study module through 50 pin FRC cable.

Step 2

Enter the program as given

Step 3

Enable Single Stepping by Switch of module & kit.

29
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Step 4

Execute the Program and observe the results on the LEDs

Step 5

‘0’ implies LED is OFF and ‘1’ implies LED is ON

At this stage “WAITING FOR INT” will display on the LCD of VMC – 8609. Give
the interrupt to the study card by connecting IRO (Lower) to VCC. Just after giving the
interrupt “IRO” will display on LCD.

Note : While using 8259 Study Module with 8086 kit switch of all position of SW1 Dip
Switch Position.

PROGRAM:

This program is to demonstrate the use of 8259 PIC. Here only Master 8259 is used,
during the main program, “HELP” is displayed while in the interrupt service loop, “IRO” is
displayed.

30
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

2. 8279 Keyboard Display

Interface keyboard and display controller 8279 with 8086 at addresses 0080H. Write
an ALP to set up 8279 in scanned keyboard mode with encoded scan, N-key rollover mode.
Use a 16-character display in right entry display format. Then clear the display RAM with
zeroes. Read the FIFO for key closure. If any key is closed, store its code to register CL.
Then write the byte 55 to all the displays and return to DOS. The lock input to 8279 is 2MHz,
operate it at 100 kHz.

Tools Required: MASM, 8086 Kit, 8279 interfacing card.

31
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Theory:
The 8279 is interfaced with lower byte of the data bus, i.e. D0-D7. Hence the A0
input of 8279 is connected with address line A1. The data register of 8279 is to be addressed
as 0080H, i.e. A0=0. For addressing the command or status word A0 input of 8279 should be
1 (the address line A1 of 8086 should be 1), i.e. the address of the command word should be
0082H.

Procedure:

Step1: Set 8279 command words according to program i.e. Keyboard/Display Mode Set CW,
Program clock selection, Clear Display RAM, Read FIFO, Write Display RAM
commands.

Step2: Read FIFO command for checking display RAM.

Step3: Wait for clearing of Display RAM by reading FIFO Du bit of the status word i.e. if Du
bit is not set wait, else proceed.

Step4: Read FIFO command for checking key closure, also read FIFO status.

Step5: Mask all bits except the number of characters bits. If any key is pressed, take required
action; otherwise proceed to write display RAM by using write display command.

Step 6: Write the byte 55H to all display RAM locations.

Step 7: Call routine to read the key code of the pressed key is assumed available.
This Program displays the code of the key, which is pressed on the keyboard pad. The code is
displayed in the data field and remains unchanged till the next key is pressed.

32
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Description of the Program:

The port of 8255 i.e. P1 is initialized to make port A as input port and port C as output
port. The three Rows of the key are scanned one by one and process is repeated till the key is
pressed, in the routine code and F code (final code). The information of code is then
displayed and the monitor jumps back again to see if any other key is pressed.

33
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

3. Write an ALP to generate Sinusoidal Wave Using 8255

ASSUME CS:CODE,DS:DATA
SINE DB 0,11,22,33,43,54,63,72,81,90,97,104,109,115,119,122
DB 125,,126,127,126,122,119,115,109,104,97,90,81,72,63,54,43,33,22,11
PA EQU 44A0H
CR EQU 44A3H
DATA ENDS
CODE SEGMENT
START: MOV AX, DATA
MOV DS, AX
MOV DX, CR
MOV AL, 80H
OUT DX, AL
REPEAT: MOV DX, PA

LEA SI, SINE


MOV CX, 36
NEXT: MOV AL,[SI]
ADD AL, 128

34
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

OUT DX, AL
INC SI
LOOP NEXT
MOV CX, 36
LEA SI, SINE
NEXT1: MOV AL, 128
MOV AH,[SI]
SUB AL, AH
OUT DX, AL
INC SI
LOOP NEXT1
JMP REPEAT
MOV AH, 4CH
INT 21H
CODE ENDS
END START

4. 8251 (USART)

DEMONSTRATION:

In this experiment we will be using 8253 in Mode3, using counter 0 and load the
count with 16 bit count. The 8251 is also initialized by specifying both command as well as
the mode word. In the Experiment whatever data is transmitted from the CPU (with the help
of RS – 232) will be received by the 8251 and then will be transmitted back to the CPU and
displayed on the screen. The program can be run either in free run mode or single stepping
mode.

Step 1:
Connect the 8253 / 8251 study module card to the 8086 kit via a 50 pin FRC. Check the
polarity of the cable for proper data transmission.

35
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Step 2 :
Connect the 8253 kit to the computer input / output port with a RS232 cable.
Step 3 :
Connect the CLK 0 tag to the 8086 CLK
Step 4 :
Connect Gate 0 tag to +5V VCC
Step 5 :
Connect out O tag to R x C & T x C tags
Step 6 :
Enter the Program given below
Step 7 :
Enter the Program by pressing Reset, Exmem, Next Keys.
Step 8 :
Execute the Program using <Reset>, <Go>, <.> Keys

36
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

5.INTERFACING STEPPER MOTOR WITH 8086:

Stepper motor is a device used to obtain an accurate position control of rotating


shafts. A stepper motor employs rotation of its shaft in terms of steps, rather than
continuous rotation as in case of AC or DC motor. To rotate the shaft of the stepper
motor, a sequence of pulses is needed to be applied to the windings of the stepper motor,
in proper sequence. The numbers of pulses required for complete rotation of the shaft of
the stepper motor are equal to the number of internal teeth on its rotor. The stator teeth
and the rotor teeth lock with each other to fix a position of the shaft. With a pulse applied
to the winding input, the rotor rotates by one teeth position or an angle x. the angle x may
be calculated as.

x = 3600 / no. of rotor teeth


After the rotation of the shaft through angle x, the rotor locks it self with the next tooth in the
sequence on the internal surface of the stator. The typical schematic of a typical stepper
motor with four windings is as shown below.

The stepper motors have been designed to work with digital circuits. Binary
level pulses of 0-5V are required at its winding inputs to obtain the rotation of the
shafts. The sequence of the pulses can be decided, depending upon the required

37
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

motion of the shaft. By suitable sequence of the pulses the motor can be used in
three modes of operation.

 One phase ON (medium torque)


 Two phase ON (high torque)
 Half stepping (low torque)

WORKING

8255 is interfaced with 8086 in I/O mapped I/O. port C (PC0, PC1, PC2, PC3)
is used to give pulse sequence to stepper motor. The 8255 provides very less current
which will not be able to drive stepper motor coils so each of the winding of a stepper
motor needs to be interfaced using high speed switching Darlington transistors with max
1A, 80V rating with heat sink, with the output port of 8255. Output the sequence in
correct order to have the desired direction to rotate the motor.

38
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

program:

LOOP MNEMONICS OPERANDS COMMENTS

MOVB AL, 80 Load AL with 80h


MOVW DX, OFFC6 Load CWR add in DX register
OUTW AX, DX Output DX,AL
MOVB AL, 08 Load AL with 08h
Load PORT C add in DX
MOVW DX, OFFC4
register
LOOP1: OUTB AL, DX Output DX, AL
CALL DELAY Call subroutine
ROLB AL, 1 Rotate left
JMP LOOP1 Jump to LOOP1
INT 03 Exit the program

Delay program

LOOP MNEMONICS OPERANDS COMMENTS

MOVW CX, 2000 Load CX with 2000


Loop itself until CX reaches
LOOP2: LOOP LOOP2 zero

RET Return to main program

39
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

6.Digital to Analog Converter (0809):

The ADC-0809 data acquisition component is a monolithic CMOS device with an 8-


bit analog to digital converter, 8 channel multiplexed control logic. The 8 bit A/D converter
uses successive approximation as the converter technique. The converter features a high
impedance chopper stabilized comparator, a 256R voltage divider with analog switch free
and a successive approximation register. The 8 channel multiplexed can directly access any
of 8 single ended analog signal.

FEATURES

1. Resolution 8 bits
2. Conversion time 100 micro sec.
3. Single supply 5V
4. 8 channel multiplexed with latched control logic
5. easy interface to all microprocessor
6. 0 to 5V analog input voltage range with single 5V supply
7. low power consumption 15mW
8. latched tristate output

WORKING

ADC interface consists of a NAND gate oscillator witch feeds 50 KHz as the input
clock to ADC, input to channel is given through terminal blocks provided on the card.
Channel selection is done using port lines PC0, PC1 & PC2, START OF CONVERSION and
ALE is controlled by port line PC7. Converted digital output is read by ADC through
PORTA lines by enabling OE. OE line is connected to port line PC6

In this method of interfacing microprocessor is continuously monitoring EOC line


(which is connected to port line PA7). When this goes high, make OE (PC6) high & then low,
this will put the digital equivalent of analog voltage of the given channel on data lines of
ADC. Read the digital data through port lines PA0 to PA7 and display the same data.

Analog input Digital output

1V 33H
2V 66H

40
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

3V 99H
4V CCH
5V FFH

The following is the assembly language using DAC to interface with 8255 and
generate a square wave on CRO. Here in the code, we use two delay elements one for the
rising part of the wave and the other delay element to reach zero i.e. decrement. Certain value
chosen is delayed or sustained for a time period to form the square wave. The two loops used
in the program are iterated to repeat cycles of a square wave.

Square wave generation:


Program

LOOP MNEMONICS OPERANDS COMMENTS


MOVB AL, 80 Load AL with 80h
Load CWR add in DX
MOVW DX, OFFC6
register
OUTB DX, AL Output DX, AL
LOOP2 MOVB AL, 0FF Load AL with 0FFh
Load PORT C add in DX
MOVW DX, OFFC0 register

OUTB DX, AL Output DX, AL


CALL DELAY Call subroutine
CALL DELAY Call subroutine
MOVB AL, 00 Load AL with 00h
OUTB DX, AL Output DX, AL
CALL DELAY Call subroutine
CALL DELAY Call subroutine
JUMP LOOP2 Jump to loop
Exit the program
INT 03

41
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Delay program:

LOOP MNEMONICS OPERANDS COMMENTS


MOVW CX, 02000 Load CX with 2000
Loop itself until CX reaches
LOOP2 LOOP LOOP2 zero

RET Return to main program

RESULT:

Square wave output

Triangular wave generation

The following is the assembly language using DAC to interface with 8255 and
generate a square wave on CRO. Here in the code, we use two loops one for the rising part of
the wave and the other element to reach zero i.e decrement. The two jump instructions used
in the program are iterated to repeat cycles of a triangular wave.

42
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Program:

LOOP MNEMONICS OPERANDS COMMENTS

MOVB AL, 80 Load AL with 80h


Load CWR add in DX
MOVW DX, OFFC6
register
OUTB DX, AL Output DX, AL
Load PORT C add in DX
MOVW DX, OFFC0
register
LOOP 2: MOVB AL, 00 Load AL with 00h
LOOP3: INCB AL Increment AL value
Output DX, AL
OUTB DX, AL

CMPB AL, 0FF Compare AL and 0FF


JNZ LOOP3 Jump not zero
LOOP1: DECB AL Decrement AL value
OUTB AL, DX Output DX,AL
CMPB AL, 00 Compare AL and 00
JNZ LOOP1 Jump not zero
JMP LOOP2 Jump to LOOP 2
INT 03 Exit the program

Delay program

LOOP MNEMONICS OPERANDS COMMENTS


MOVW CX, 2000 Load CX with 2000
Loop itself until CX reaches
LOOP2: LOOP LOOP2 zero

RET Return to main program

43
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

RESULT:

Triangular wave output:

44
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

7.Interfacing 8279 keyboard & display controller with 8086 microprocessor

8279 theory:

Features:

 A programmable keyboard and display interfacing chip.

 Scans and encodes up to a 64-key keyboard.

 Controls up to a 16-digit numerical display.

 Keyboard section has a built-in FIFO 8 character buffer.

 The display is controlled from an internal 16x8 RAM that

 Stores the coded display information.

Functional block diagram:

45
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Pin out Definition 8279:

 A0: Selects data (0) or control/status (1) for reads and

 Writes between micro and 8279.

 BD: Output that blanks the displays.

 CLK: Used internally for timing. Max is 3 MHz.

 CN/ST: Control/strobe, connected to the control key on the keyboard.

 CS: Chip select that enables programming, reading the keyboard, etc.

 DB7-DB0: Consists of bidirectional pins that connect to data bus on micro.

 IRQ: Interrupt request, becomes 1 when a key is pressed, data is available.

 OUT A3-A0/B3-B0: Outputs that sends data to the most significant/least significant

 Nibble of display.

 RD (WR): Connects to micro's IORC or RD signal, reads data/status registers.

 RESET: Connects to system RESET.

 RL7-RL0: Return lines are inputs used to sense key depression in the keyboard
matrix.

 Shift: Shift connects to Shift key on keyboard.

 SL3-SL0: Scan line outputs scan both the keyboard and displays.

46
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Pin diagram:

47
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Whether new data are entered to the rightmost or leftmost display position.

48
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

8279 interfaced to the 8086:

49
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Program:

LOOP MNEMONICS OPERANDS COMMENTS

L5 MOVB AL,08 Load AL with 08


MOV W DX,3002 Store 3002 to DX
OUT DX,AL Output DX,AL
MOV B AL,31 Load AL with 31
OUT DX,AL Output DX,AL
MOVB AL,D0 Load AL with 00
OUT DX,AL Output DX,AL
Load count value in CX
MOVW CX,0FFFF
register
L1 LOOP L1 Continue until CX=0

50
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

MOVB AL,00 Load AL with 00


OUT DX,AL Output DX,AL
MOVB AH,08 Load AH with 08
MOVW SI,6000 Load SI with 6000
MOVB AL,90 Load AL with 90
OUT DX,AL Output DX,AL
L3 MOVB AL,[SI] Load AL with SI

MOVW DX,3000 Store 3000 to DX

OUT DX,AL Output DX,AL

LOOP MNEMONICS OPERANDS COMMENTS

Load count value in CX


MOV CX,0FFFF
register
L2 LOOP L2 Continue until CX=0
INC SI Increment SI value
DEC AH Dec AH value
JNZ L3 Jump not zero
Load count value in CX
MOV CX,0FFFF
register
L4 LOOP L4 Continue until CX=0
JMP L5 Jump to L5

51
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

RESULT:

INPUT: “ASIST” DATA HEX: 77H, 6DH, 30H, 6DH, 78H

OUTPUT: DISPLAY: “ASIST” ON THE 7 SEGMENT DISPLA

52
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

III.8051 Programming:
1.Reading and writing on a parallel port.

1. Writing to a port pin

SETB P3.5; set pin 5 of port 3


MOV P1, #4AH; sending data 4AH to port 1 - the binary pattern on the port will be
0100 1010
MOV P2, A; send whatever data is in the accumulator to port 2

2. Reading a port pin

SETB P1.0 ; initialize pin 0 of port 1 as an input pin


MOV P2, #FFH ; set all pins of port 2 as inputs
MOV C, P1.0 ; move value on pin 0 of port 1 to the carry
MOV R3, P2 ; move data on port 2 into R3

2. Timer in Different Modes

The basic 8051 has two on-chip timers that can be used for timing durations or for
counting external events Interval timing allows the programmer to perform operations at
specific instants in time. For example, in our LED flashing program the LED was turned on
for a specific length of time and then turned off for a specific length of time. We achieved
this through the use of time delays. Since the microcontroller operates at a specific frequency,
we could work out exactly how many iterations of the time delay was needed to give us the
desired delay. However, this is cumbersome and prone to error. And there is another
disadvantage; the CPU is occupied, stepping through the loops. If we use the on-chip timers,
the CPU could be off doing something more useful while the timers take on the menial task
of keeping track of time

53
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

The Timers' SFRs

The 8051 has two 16-bit timers. The high byte for timer 1 (TH1) is at address 8DH
while the low byte (TL1) is at 8BH The high byte for timer 0 (TH0) is at 8CH while the low
byte (TL0) is at 8AH.Both timers can be used in a number of different modes. The
programmer sets the timers to a specific mode by loading the appropriate 8-bit number into
the Timer Mode Register (TMOD) which is at address 89H.

Timer Mode Register:

The functions of the 8-bits of TMOD are described in the above table. The top four bits are
for timer 1 and the bottom four bits have the exact same function but for timer 0. The Gate
bits are used in conjunction with interrupts and will be dealt with at a later stage. For the
moment we can take it that bits 7 and 3 are always cleared. As mentioned above, the timers
can be used for counting external events or for timing intervals. If you wish the timer to be an
event counter you set the corresponding C/T-bar bit. Similarly, if you wish it to be an interval
timer you reset the corresponding C/T-bar bit. There are two mode bits (M1 and M0) for each
timer. The table below describes their function.

54
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

There are four timer modes, set by the bits M1 and M0. Mode 0 is not commonly used.

Mode 1 - 16-bit mode

The high byte (THx) is cascaded with the low byte (TLx) to produce a 16-bit timer.
This timer counts from 0000H to FFFFH - it has 216 (65,536) states. An overflow occurs
during the FFFFH to 0000H transition, setting the overflow flag (to be dealt with shortly).

Mode 2- 8-bit auto-reload mode

The timer low byte (TLx) operates as an 8-bit timer (counting to FFH) while the high-
byte holds a reload value. When the timer overflows from FFH, rather than starting again
from 00H, the value in THx is loaded into TLx and the count continues from there

55
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

3. Serial communication implementation

All communication we are dealing with can be serial or parallel.

In Parallel communication, data being transferred between one location and another
(R0 to the accumulator, for example) travel along the 8-bit data bus. Because of this data bus,
data bytes can be moved about the microcontroller at high speed.

However, parallel communication has the disadvantage of requiring at least eight


separate lines (in an 8-bit system) and in most cases extra lines to synchronize the data
transfer (in the case of the microcontroller, the control bus).

Serial communication has the advantage of requiring only one line for the data, a
second line for ground and possibly a third line for the clock. Therefore, because serial
communication requires less physical wires, it is more suitable for transmitting data over
longer distances.

56
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Synchronous Serial Communication

Synchronous serial communication requires an extra line for the clock signal. For
serial communication, the 8-bit parallel data byte must be shifted down the serial line (in
transmission). Therefore, one bit is followed by another. Some kind of system must be used
to determine how long each bit is on the line. For example, the serial system designer may
decide each bit will be on the line for 1us and, as explained above, transmission of the full
eight bits would take 8us. With synchronous communication, the clock signal is transmitted
on a separate line, as shown in the diagram below.

In this way, the receiver is synchronized with the transmitter. As we shall see, the 8051 serial
port in mode 0 is an example of synchronous serial communication.

Asynchronous Serial Communication

A good example of asynchronous serial communication is the interface between a


keyboard and a computer. In this case, the keyboard is the transmitter and the computer is the
receiver. With asynchronous communication, a clock signal is not sent with the data. There
are a number of reasons why this form of communication might be desirable over
synchronous communication. One advantage is the fact that the physical line for the clock is
not needed. Also, asynchronous communication is better over long distances. If we try to
synchronize a remote receiver by sending the clock signal, due to propagation delays and
interference, the validity of the clock is lost Another reason for not transmitting the clock
arises when the data rate is erratic. For example, data rate from a keyboard to a computer is

57
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

dependent upon the typist. The user may type at a rate of sixty words per minute, but at other
times he/she may type a lot less. And for long periods there may be no data sent at all.
Because of this erratic data rate an asynchronous communication system is suitable.

Serial Communication Protocol

In any communication system, the receiver must know what kind of data to expect
and at what rate the data will arrive. In both synchronous and asynchronous serial
communication, the receiver needs to know with which bit the transmitter begins. In most
systems the LSB is the first bit transmitted. For an asynchronous system, the number of bits
transmitted per second must be known by the receiver. Since the clock signal is not
transmitted, the receiver needs to know what clock frequency the transmitter is using so that
it can use the same. The receiver also needs to know how many bits per word the transmitter
is using (in most cases we deal with 8-bit words, but we will see cases where nine bits are
transmitted per word). And the receiver needs to know where the data begins and where the
data stops. All these parameters make up the protocol. If the receiver uses the same protocol
as the transmitter is should receive the data correctly (although errors can occur and we will
look at how we catch these errors at a later date). If the receiver uses a protocol other than the
one used by the transmitter, then the two devices are effectively speaking two different
languages and the data received will be garbage.

Start Bits and Stop Bits

In asynchronous communication, at least two extra bits are transmitted with the data
word; a start bit and a stop bit. Therefore, if the transmitter is using an 8-bit system, the actual
number of bits transmitted per word is ten. In most protocols the start bit is a logic 0 while the
stop bit is logic 1. Therefore, when no data is being sent the data line is continuously HIGH.
The receiver waits for a 1 to 0 transition. In other words, it awaits a transition from the stop
bit (no data) to the start bit (logic 0). Once this transition occurs the receiver knows a data
byte will follow. Since it knows the data rate (because it is defined in the protocol) it uses the
same clock as frequency as that used by the transmitter and reads the correct number of bits

58
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

and stores them in a register. For example, if the protocol determines the word size as eight
bits, once the receiver sees a start bit it reads the next eight bits and places them in a buffer.
Once the data word has been read the receiver checks to see if the next bit is a stop bit,
signifying the end of the data. If the next bit is not logic 1 then something went wrong with
the transmission and the receiver dumps the data. If the stop bit was received the receiver
waits for the next data word, ie; it waits for a 1 to 0 transition

The obvious disadvantage of serial communication, compared with parallel, is the


reduction in the data transfer rate. If we imagine a system where it takes 1us for data to settle
on the data bus, we could say it takes 1us to transfer a data byte using parallel
communication. If we imagine the same timeframe for data bits settling on the serial line, it
would take 8us to transfer a data byte using serial communication (1us for each bit).

The 8051 Serial Port

The 8051 includes an on-chip serial port that can be programmed to operate in one of
four different modes and at a range of frequencies. In serial communication the data is rate is
known as the baud rate, which simply means the number of bits transmitted per second. In the
serial port modes that allow variable baud rates, this baud rate is set by timer 1.

59
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

The 8051 serial port is full duplex. In other words, it can transmit and receive data at
the same time. The block diagram above shows how this is achieved. If you look at the
memory map you will notice at location 99H the serial buffer special function register
(SBUF). Unlike any other register in the 8051, SBUF is in fact two distinct registers – the
write-only register and the read-only register. Transmitted data is sent out from the write-only
register while received data is stored in the read-only register. There are two separate data
lines, one for transmission (TXD) and one for reception (RXD). Therefore, the serial port can
be transmitting data down the TXD line while it is at the same time receiving data on the
RXD line.

The TXD line is pin 11 of the microcontroller (P3.1) while the RXD line is on pin 10
(P3.0). Therefore, external access to the serial port is achieved by connecting to these pins.
For example, if you wanted to connect a keyboard to the serial port you would connect the
transmit line of the keyboard to pin 10 of the 8051. If you wanted to connect a display to the
serial port you would connect the receive line of the display to pin 11 of the 8051. This is
detailed in the diagram below.

60
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Transmitting and Receiving Data

Essentially, the job of the serial port is to change parallel data into serial data for transmission

and to change received serial data into parallel data for use within the microcontroller.

 Serial transmission is changing parallel data to serial data.

 Serial reception is changing serial data into parallel data.

 Both are achieved through the use of shift registers

As discussed earlier, synchronous communication requires the clock signal to be sent

along with the data while asynchronous communication requires the use of stop bits

and start bits. However, the programmer wishing to use the 8051 need not worry

about such things. To transmit data along the serial line you simply write to the serial

buffer and to access data received on the serial port you simply read data from the

serial buffer.

Transmitting and Receiving Data :

Essentially, the job of the serial port is to change parallel data into serial data for
transmission and to change received serial data into parallel data for use within the
microcontroller.

 Serial transmission is changing parallel data to serial data.


 Serial reception is changing serial data into parallel data.
 Both are achieved through the use of shift registers.

As discussed earlier, synchronous communication requires the clock signal to be sent


along with the data while asynchronous communication requires the use of stop bits and start
bits. However, the programmer wishing to use the 8051 need not worry about such things. To

61
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

transmit data along the serial line you simply write to the serial buffer and to access data
received on the serial port you simply read data from the serial buffer.

For example:

MOV SBUF, #45H - this sends the byte 45H down the serial line
MOV A, SBUF - this takes whatever data was received by the serial port and puts it in
the accumulator.

How do we know when the complete data byte has been sent?

As mentioned earlier, it takes a certain length of time for a data byte to be transmitted
down the serial line (determined by the baud rate). If we send data to SBUF and then
immediately send more data to SBUF, as shown below, the initial character will be
overwritten before it was completely shifted down the line.

MOV SBUF, #23H


MOV SBUF, #56H

Therefore, we must wait for the entire byte to be sent before we send another. The serial port
control register (SCON) contains a bit which alerts us to the fact that a byte has been
transmitted; ie; the transmit interrupt flag (TI) is set by hardware once an entire byte has been
transmitted down the line. Since SCON is bit-addressable we can test this bit and wait until it
is set, as shown below:

MOV SBUF, #23H; send the first byte down the serial line
JNB TI, $; wait for the entire byte to be sent
CLR TI; the transmit interrupt flag is set by hardware but must be cleared by software
MOV SBUF, #56H; send the second byte down the serial line

62
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

How do we know when data has been received?

Similarly, we need to know when an entire byte has been received by the serial port.
Another bit in SCON, the receive interrupt flag (RI) is set by hardware when an entire byte is
received by the serial port. The code below shows how you would program the controller to
wait for data to be received and to then move that data into the accumulator.

JNB RI, $; wait for an entire byte to be received


CLR RI; the receive interrupt flag is set by hardware but must be cleared by software
MOV A, SBUF; move the data stored in the read-only buffer to the accumulator

4.Simple programs of 8051 Microcontroller:

Addition:

MNEMONICS OPERANDS COMMENTS

MOV A,#30 Loading 30 into accumulator

MOV B,#40 Loading 40 into accumulator

ADD A,B Performing addition

LCALL 03 Halt

Subtraction:

MNEMONICS OPERANDS COMMENTS

MOV A,#20 Loading 20 into accumulator

MOV B,#10 Loading 10 into accumulator

SUB A,B Performing subtraction

LCALL 03 Halt

63
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Multiplication:

MNEMONICS OPERANDS COMMENTS

MOV A,#20 Loading 20 into accumulator

MOV F0,#10 Loading 10 into accumulator

MUL AB Performing multiplication

LCALL 03 Halt

Division:

MNEMONICS OPERANDS COMMENTS

MOV A,#20 Loading 20 into accumulator

MOV F0,#10 Loading 10 into accumulator

DIV AB Performing division

LCALL 03 Halt

Exchange:

MNEMONICS OPERANDS COMMENTS

MOV A,#20 Loading 20 into accumulator

MOV R0,#10 Loading 10 into accumulator

XCH A,R0 Exchange operation A&R0

LCALL 03 Halt

64
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

Increment :

MNEMONICS OPERANDS COMMENTS

MOVB A, #08 Load AL with 08

INCB A Perform INC operation on A

LCALL 03 Halt

Decrement:

MNEMONICS OPERANDS COMMENTS

MOVB A, #08 Load AL with 08

DECB A Perform DEC operation on A

LCALL 03 Halt

65
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

B.SURESH

Asst Professor

Department of ECE

surem9@gmail.com

66
www.sureshQ.blogspot.in
MP&MC LAB MANUAL
Department of ELECTRONICS AND COMMUNICATION ENGINEERING

AMRITA SAI INSTITUTE OF SCIENCE & TECHNOLOGY

MICROPROCESSORS

AND

MICROCONTROLLERS

67
www.sureshQ.blogspot.in

You might also like