MPLABMANUAL

76
HKBK college of engineering Bangalore Prepared by CS Department Microprocessor Lab Manual -06CS48 Microprocessor Laboratory (Common to CSE and ISE) Sub Code: 06CSL48 IA Marks: 25 Hrs / Week 03 Exam Hours: 03 Total Hrs: 42 Exam Marks: 50 Note: • Develop and execute the following programs using an 8086 Assembly Language. All the programs to be executed using an assembler like MASM, TASM etc. • Program should have suitable comments. • The board layout and the circuit diagram of the interface are to be provided to the student during the examination. 1.a Search a key element in a list of n numbers using the Binary Search algorithm. 1.b Read the status of eight input bits from the logic controller interface and display FF if it is even parity bits otherwise displays 00. Also display number of 1’s in the input data. 2.a Write ALP macros: i) To read a character from the keyboard in the module (1)(in a different file) ii) To display a character in module (2)(from different file) iii) Use the above two modules to read a string of characters from the keyboard Terminated by the carriage return and print the string on the display in the next line. 2.b Perform the following functions using the Logic Controller interface. i) BCD up-down Counter ii ) Ring Counter. 3.a Sort a given set of n numbers in ascending and descending order using Bubble sort algorithm. 3.b Read the Status of two 8-bits inputs(X & Y) from the Logic Controller interface and display X*Y 4.a Read an alphanumeric character and display its equivalent ASCII code at the center of the screen 4.b Display the messages FIRE and HELP alternately with flickering effects on a 7- segment display interface for a suitable period of a time. Ensure a flashing rate that makes it easy to read both messages.(Examiner does not specify these delays values nor it is necessary for the student to compute these values. 5.a Reverse a given string and check whether it is a Palindrome or not..

Transcript of MPLABMANUAL

Page 1: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

Microprocessor Lab Manual -06CS48

Microprocessor Laboratory

(Common to CSE and ISE)

Sub Code: 06CSL48 IA Marks: 25

Hrs / Week 03 Exam Hours: 03

Total Hrs: 42 Exam Marks: 50

Note: • Develop and execute the following programs using an 8086 Assembly

Language. All the programs to be executed using an assembler like MASM,

TASM etc.

• Program should have suitable comments.

• The board layout and the circuit diagram of the interface are to be provided to the

student during the examination.

1.a Search a key element in a list of n numbers using the Binary Search algorithm.

1.b Read the status of eight input bits from the logic controller interface and display

FF if it is even parity bits otherwise displays 00. Also display number of 1’s in

the input data.

2.a

Write ALP macros:

i) To read a character from the keyboard in the module (1)(in a different file)

ii) To display a character in module (2)(from different file)

iii) Use the above two modules to read a string of characters from the

keyboard

Terminated by the carriage return and print the string on the display in

the next line.

2.b Perform the following functions using the Logic Controller interface.

i) BCD up-down Counter ii ) Ring Counter.

3.a Sort a given set of n numbers in ascending and descending order using Bubble

sort algorithm.

3.b Read the Status of two 8-bits inputs(X & Y) from the Logic Controller interface

and display X*Y

4.a Read an alphanumeric character and display its equivalent ASCII code at the

center of the screen

4.b

Display the messages FIRE and HELP alternately with flickering effects on a 7-

segment display interface for a suitable period of a time. Ensure a flashing rate

that makes it easy to read both messages.(Examiner does not specify these delays

values nor it is necessary for the student to compute these values.

5.a Reverse a given string and check whether it is a Palindrome or not..

Page 2: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

5.b

Assume any suitable message of 12 characters length and display it in the rolling

fashion on a 7-segment display interface for a suitable period of a time. Ensure a

flashing rate that makes

It easy to read both the messages (Examiner does not specify these delay values

nor it is necessary for the student to compute these values.

6.a Read two strings, store them in locations STR1 and STR2. Check whether they

are equal or not display appropriated messages. Also display the length of the

stored strings.

6.b Convert a 16-bit binary value(assumed to be an unsigned integer) to BCD and

display it from left to Right and Right to left for specified number of times on a

7-segment display interface.

7.a Read your name from the keyboard and display it at a specified location on the

screen in front of the message WHAT IS YOUR NAME? You must clear the

entire screen before display.

7.b

Drive a Stepper motor interface to rotate the motor in clockwise direction by N

steps. (N is specified from the examiner). Introduce suitable delay between

successive steps.(Any arbitrary value for the delay may be assumed by the

student).

8.a Compute the factorial of a positive integer ‘n’ using recursive procedure.

8.b

Drive a Stepper motor interface to rotate the motor in anticlockwise direction by

N steps. (N is specified from the examiner). Introduce suitable delay between

successive steps.(Any arbitrary value for the delay may be assumed by the

student).

9.a Compute nCr Using recursive procedure. Assume that ‘n’ and ‘r’ are non-

negative integers.

9.b

Drive a Stepper motor interface to rotate the motor by N steps left direction and

N steps Right direction. (N is specified from the examiner). Introduce suitable

delay between successive steps. (Any arbitrary value for the delay may be

assumed by the student).

10.a Find out whether a given sub-string is present or not in a main string of

characters..

10.b Scan a 8x3 keypad for key closure and to store the code of the key pressed in a

memory location or display on screen. Also display row and column numbers of

the key pressed.

11.a Generate the first ‘n’ Fibonacci numbers.

Page 3: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

11.b Scan a 8x3 keypad for key closure and Simulate ADD and SUBTRACT

operations as in a calculator.

12.a Read the current time from the system and display it in the standard format on

the screen.

12.b Generate a sine wave Using DAC interface (The output of the DAC is to be

displayed on a CRO)

13.a Program to simulate a decimal up-counter to display 00-99

13.b Generate a Half Rectified sine wave from using the DAC interface(The output of

the DAC is to be displayed on a CRO)

14.a Read a pair of input co-ordinates in BCD and move the cursor to the specified

location on the screen

14.b Generate a Fully Rectified sine wave form using the DAC interface(The output

of the DAC is to be displayed on a CRO)

15.a Program to create a file(input file) and to delete an existing file.

15.b Drive an elevator interface in the following way to move an elevator from

ground to top floor and top to ground floor.

INTRODUCTION

PROGRAMMING WITH AN ASSEMBLER

The assembly language programming can be done using the Assemblers. There are

a number of assemblers available like MASM, TASM and DOS assembler .MASM

is one of the popular assemblers used along with a LINK program to structure the

codes generated in the form of an executable file. MASM reads the source program

as its input and provides an object file. The LINK accepts the object file produced

by MASM as input and produces an EXE file.

Page 4: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

ENTERING A PROGRAM

Before starting the process, ensure that all the files namely Editor)

TASM.EXE(Assembler),LINK.EXE(Linker),DEBUG.EXE(Debugger) are

available in the same directory in which we are working.

STEPS

1. OPEN THE TEXT EDITOR.

COMMAND IS: C>EDIT OR C>NE

2. ENTER THE PROGRAM AND CHECK FOR THE MISTAKES AND

SYNTAX ERRORS

3. SAVE THE PROGRAM WITH THE .ASM EXTENSION.

COMMAND IS: FILENAME.ASM

4. ASSEMBLE THE PROGRAM.

COMMAND IS: C>TASM FILENAME

OR

C>TASM FILENAME.ASM

After successful assembling, three files are created namely

i) The object file with .OBJ extension.

ii) The listing file with .LST extension.

The listing file contains the total offset map of the source file

including labels, offset addresses, opcodes, memory allotment for

different labels and directives and relocation information.

The cross reference filename with .CRF extension.

The cross reference file is used for debugging the source program

and contains the statistical information size of the file in bytes,

number of labels, list of labels, routines to be called, etc. about the

source program.

Page 5: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

These files are used to link the object modules and generate an

executable (.EXE) file from a .OBJ file.

5. Link the object modules of the source program and the function library

routines to generate an integrated executable code of the source

program.

COMMAND IS: C>LINK FILENAME.OBJ

The output of the LINK program is an executable file with the

entered

filename and .EXE extension.

6. DEBUG.COM, a DOS utility can be used for debugging and trouble-

shooting . of assembly language programs

COMMAND IS: C>DEBUG

A ‘-‘(dash) display signals the successful invoke operation of

DEBUG.

Some of the DEBUG commands used are as follows:

-R Display all registers and flags.

-D Display 128 memory locations of RAM starting

from the current

display pointer.

-g Execute from current CS: IP.

-q Quit the DEBUG and return to DOS.

-T SEG: OFFSET Trace the program execution by single stepping

CODE

SEGEMENT

DATA

SEGEMENT

small 1 1

medium more than 1 1

compact 1 more than 1

large more than 1 more than 1

Page 6: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

DOS service functions

(1)display character string-int 21h function 09h

(a)display a string of character at the standard output device

(b)string terminated by char $ which is not display

register ah=09

reg pair ds:dx=segment :offset of string

(2) Terminate process-int 21h function 4Ch

Terminates the current process returns control either to parent

process or DOS

register ah=4ch

register al=return code(00h,01h,02h,03h)

(3)set cursor position- int 10h Function 02h

reg AH=02

reg DH=row number of cursor(y-axis)(input)

reg DL= column number of cursor(x-axis)(input)

(4)read a character with echo -int 21h function 09h

(a)reads a character from standard input device and echoes it to

the standard output device or wait until a character is available

register ah=01

Register AL= character input(ASCII code)

(5)display a character -int 21h function 02h

(a)displays a character at the standard output device

register ah=02

Register DL = ASCII code for character to be displayed

(6)CHECK INPUT DEVICE STATUS -int 21h function 0Bh

(a)checks whether a character is waiting to be received from the

standard input device.

register ah=0BH

Register AL = 00h if no character is available

FF h if at least one character is available

Page 7: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

(7)buffered input -int 21h function 0ah

(a)reads string of characters from the standard input device

,echoes the characters to the standard output device and places

characters into a buffer.

register ah=0aH

Register ds:DX = store the starting address of string

Byte –

0- Must be non zero and must specify maximum number

characters to read set by program

1- Number of character actually read filled in by function

2- String read from the standard input, terminated by a

carriage return

(8)get time -int 21h function 2ch

(a)obtains the (hours, minutes, seconds and hundredths of

seconds) as maintained by DOS

register ah=2ch

Register CH= Hours(0-23)

Register CL= minutes(0-59)

Register DH= seconds(0-59)

Register DL= hundredths of second(0-99)

(9)create a new file -int 21h function 5bh

(a)creates a new file . if file with the same name exists the

function requests returns with an error

register ah=5bh

Page 8: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

Register CX= attributes

Bits significance(if set)

0 read only

1 hidden

2 system

3 volume label

4 reserved(0)

5 archive

6-15 reserved(0)

register pair DS:DX=ASCIIZ path name

returns

if function successful

carry flag=clear

register AX=handle

if function not successful

carry flag=set

register AX=error code

(10)delete a file -int 21h function 41h

(a)deletes a file from specified directory

register ah=41h

register pair DS:DX=ASCIIZ path name(drive and directory)

returns

if function successful

carry flag=clear

if function not successful

carry flag=set

register AX=error code

(11)read a character without echo -int 21h function 08h

(a)reads a character from standard input device and without echoes it to

the standard output device or wait until a character is available

register ah=08

Register AL= character input(ASCII code)

(12)get status of keyboard -int 16h function 01h

(a)checks to see if a character is typed and is available in keyboard buffer

register ah=01

Register AH=keyboard scan code Register AL=ASCII character if

character not available ZF=1 else ZF=0

Page 9: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

PART A Programs(ONLY SOFTWARE PROGRAMS)

1. a) Search a key element in a list of ‘n’ 16-bit numbers using the binary search

algorithm

CODE COMMENTS

.MODEL SMALL allocate 1 cs and 1 ds

.DATA indicate start of data segment

ARY DW 1234H, 2345H, 3456H,

4567H, 5678H create array of 5 nos (2byte size)

LEN DW ($ - ARY)/2

length of array(end-start/2) because each

memory loc store 1 byte

KEY DW 3456H key to searched

SMSG DB 'SUCCESSFUL SEARCH$'

display message -create array of string(each

character is 1 byte)

FMSG DB 'UNSUCCESSFUL

SEARCH$'

display message create array of string(each

character is 1 byte)

.CODE indicate start of code segment

MOV AX,@DATA

store address of ds in ds( 1st store in ax and then

transfer to ds

MOV DS,AX (since direct transfer of address is not possible

MOV CX,KEY store key in cx

MOV BX,00 move (low) value in bx

DEC LEN decrement length

MOV DX,LEN move(high) len to dx

START:CMP BX,DX compare bx with dx bx dx(CF=1 goto failmsg)

JA FAILMSG

branch if bx value more than dx display failure

message (CF=0)

MOV AX,BX else mov bx to ax (ax=0)

ADD AX,DX add ax with dx (0+4)(low+high)

SHR AX,1 shift right to div by 2 =2

MOV SI,AX move to si si=2

ADD SI,SI

add si with si (since 16bit) 8 bit not required

(si=4)

CMP CX,ARY[SI] compare key with array element(mid)

branch if above or equal to found display msg

found if cf=1 or zf=1 JAE FOUND

DEC AX otherwise decrement ax (mid-1)

MOV DX,AX move ax to dx (mid-1)(high)

Page 10: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

JMP START jmp start (again compare bx with dx(low<high)

FOUND :JE SUCMSG if equal branch to success msg(ZF=1)

INC AX else (mid +1)

MOV BX,AX mov to bx(low)

JMP START jmp start

SUCMSG :LEA DX,SMSG load address of smsg into dx

JMP DISP jmp to display

FAILMSG:LEA DX,FMSG load address of fmsg into dx

DISP :MOV AH,09H display function store 09 in ah

INT 21H execute function display string(dos service)

MOV AH,4CH terminate program

INT 21H execute function terminate(dos service)

END End

Page 11: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

2. a) Write ALP macros:

i. To read a character from the keyboard in the module (1) (in a different file)

ii. To display a character in module(2) (from different file)

iii. Use the above two modules to read a string of characters from the keyboard

terminated by the carriage return and print the string on the display in the next line.

code Comments

INCLUDE 2A1.MAC include macro to read a string

INCLUDE 2A2.MAC include macro to display a string

.MODEL SMALL 1 cs & 1 ds

.DATA declare data segment

CHR DB 100 DUP(?) define array of char

MSG DB

13,10,'CHARACTERS:$' store message in message 13-carriage return 10-line feed

.CODE define code segment

MOV AX,@DATA

store address of ds in ds register MOV DS,AX

MOV CX,0 initialize counter to find length of string

LEA SI,CHR store address of chr array in si

AGNRD:READ [SI]

read character from macro and store in chr character by

character

INC CX

increment CX to read next character till enter key(13)

pressed

INC SI increment the loc in chr

CMP AL,13 compare al content with 13 (enter key)

JNE AGNRD not equal repeat read char (ZF=0)

LEA DX,MSG else load address of message

MOV AH,09 move 09 to ah to display string

INT 21H dos service

LEA SI,CHR load start address of chr to SI

NXTDISP:DISP [SI] display string using macro character by charter

INC SI increment SI

LOOP NXTDISP till CX=0

MOV AH,4CH terminate program

INT 21H dos service

END End

Page 12: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

2a1)macro for read a character-2a1.mac

READ MACRO LOC

MOV AH,01 store 01 in ah to read char

INT 21H dos service

MOV LOC,AL result store in al move to loc

ENDM end of macro

2a2)macro for display a character-2a2.mac

DISP MACRO LOC

MOV DL,LOC

mov from loc to dl as input to

function

MOV AH,2

store 02 function code in ah to

display char

INT 21H dos service

ENDM end of macro

Page 13: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

3. a) Sort a given set of ‘n’ numbers in ascending and descending orders using the

Bubble Sort algorithm.

CODE COMMENTS

.MODEL SMALL Allocate 1 CS and 1 DS

.DATA Declaration of data segment

LIST DB 23H,12H,45H,35H create array of 1 byte data (asc)

LEN DW $-LIST find length (current location - last location)

LIST1 DB 4 DUP(0) create array of 1 byte data (asc)

.CODE code segment declaration

MOV AX,@DATA store data segment address in AX reg

MOV DS,AX move address to data segment register

MOV ES,AX move address to data segment register

MOV DX,LEN store length in DX for number passes

DEC DX decrement DX ;number of elements-1

AGAIN0: MOV CX,DX store value in CX for number of comparison

LEA SI,LIST store starting address array in SI register

LEA DI,LIST1 store starting address array in DI register

AGAIN1: MOV AL,[SI] move content of array into AL register

CMP AL,[SI+1] compare 1st element with next element

JL PR1 branch if AL content less then next element

XCHG [SI+1],AL otherwise exchange content AL next

location and transfer to SI because direct

memory transfer is not possible AL<---

>[SI+1]

MOV [SI],AL then move from AL to SI

Page 14: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

PR1 : ADD SI,01 increment SI

LOOP AGAIN1 repeat the process

DEC DX decrement DX for next pass

JNZ AGAIN0 if not zero repeat the pass(ZF=0)

MOV CX,LEN Store length in CX

LEA SI ,LEN Store in SI

DEC SI Decrement to go to last location

L1:MOV AL,[SI] Move last no into al

MOV [DI],AL Then to di(list1)

ADD DI,01 Add 01 with 01

DEC SI Dec SI to next loc

LOOP L1 Loop till cx=0

INT 3 Break see result in memory

END End

Page 15: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

4. a) Read an alphanumeric character and display its equivalent ASCII code at the

center of the screen.

.MODEL SMALL allocate 1CS and 1DS

.CODE declaration of code segment

START: CALL CLS call clear screen procedure

MOV AH,2

to read character from monitor store 2 in

AH(input)dOS function

MOV DH,12 store row no in DH according to dos function

MOV DL,38

store column no in DL according to dos

function

INT 10H call function to position the cursor

MOV AH,1 read a character that position(ASCII CODE)

INT 21H execute that function

MOV DH,12 store row no in DH according to dos function

MOV DL,41

store column no in DL according to dos

function

MOV AH,2 display a character that position

INT 10H call function to position the cursor

MOV BL,AL

transfer content of AL to BL for temporary

storage

MOV CL,4 store 4 to CL to shift right

SHR AL,CL

shift right the content of AL (MSB 4bit 0000

after shifting)

MOV DL,AL store in DL

CMP DL,9 compare with 9

JBE L1

if less or equal(content of DL) (CF=1or

ZF=1) no addition of 07

ADD DL,07H else add 07 to dL to get ASCII value (30+7)

L1 :ADD DL,30H add only 30 to get ASCII value(hex)

MOV AH,2 display a character

INT 21H execute that function

MOV DL,BL

copy original content before shifting from BL

to DL

AND DL,0FH

and with 0f to get lower 4 bit upper 4bit with

0000

CMP DL,9 compare with 9

JBE L2

if less or equal(CF=1or ZF=1) no addition of

07

Page 16: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

ADD DL,07H else add 07 to DL to get ASCII value (30+7)

L2 :ADD DL,30H add only 30 to get ASCII value

MOV AH,2 display a character

INT 21H execute that function

MOV AH,4CH else enter dos prompt

INT 21H execute that function

CLS PROC clear procedure

MOV AH,2 store 2 to call function

MOV DX,0

initialize DX with 0 to position both dl and dh

with (0,0)

INT 10H execute the function

MOV DL,' ' store blank in DL for clear

MOV CX,25*80

depends on monitor 25 *80 or 50*80 counter

to print blank

BLUK :MOV AH,02 display a character

INT 21H execute the function

LOOP BLUK repeat same 25 *80 times

RET return to main program where it is called

ENDP End

END End of main

Page 17: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

5. a) Reverse a given string and check whether it is a palindrome or not.

CODE COMMENTS .MODEL SMALL allocate 1 CS and 1DS

.DATA data segment declaration

MSTR DB 'DAMMA' create array of character to store the string

LEN DW $-MSTR find length of string

RSTR DB 20 DUP (0) create array to store reversed string

PM DB 'PALINDROME$' store message string to display palindrome

NPM DB 'NOT A PALINDROME$'

store message string to display not

palindrome

.CODE declaration of code segment

MOV AX,@DATA store address of data segment

MOV DS,AX move from AX to DS

MOV ES,AX move from AX to ES

LEA SI,MSTR load address of string( damma) in SI

LEA DI,RSTR load address of reversed string in DI

MOV CX,LEN store length of string for counter

ADD SI,CX

add start address with length to get last

address(last letter)

REVR: DEC SI decrement SI

MOV AL,[SI] move content of last location to AL

STOSB

Store string byte (store last letter in AL as

first letter in DI address and automatically

increment DI)

LOOP REVR repeat till CX = 0

MOV CX,LEN store length of string for counter

LEA DI,RSTR store address of reversed string

REPE CMPSB

repeat till equal each letter of SI with DI

(ZF=1 and CX not equal to 0 )

JZ SMSG

branch both strings are equal and display

palindrome message(ZF=1)

LEA DX,NPM

else store address of not palindrome

message address into DX

JMP DISP branch to display

SMSG: LEA DX,PM

store address of palindrome message in

DX

DISP: MOV AH,09h store no 9 for string display function

INT 21H execute function(DOS service)

MOV AH,4CH terminate and go to dos prompt

INT 21H execute function(DOS service)

END end

Page 18: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

6. a) Read two strings, store them in locations STR1 and STR2. Check whether

they are equal or not and display appropriated messages. Also display the length of

the stored strings.

.MODEL SMALL allocate 1CS and 1DS

.DATA declaration of code segment

S1 DB 80

store maximum number of character in

string

S1L DB 0 store len of string filled by function

S1S DB 80 DUP (0)

store actual string read from standard

input

S2 DB 80

store maximum number of character in

string

S2L DB 0 store len of string filled by function

S2S DB 80 DUP (0)

store actual string read from standard

input

M1 DB 13,10,'ENTER FIRST

STRING :$'

\r\n write enter first string and goto next

line

M2 DB 13,10,'ENTER SECOND

STRING :$'

\r\n write enter second string and goto

next line

E DB 13,10,'STRINGS ARE

EQUAL.$'

\r\n write strings are equal and goto next

line

NEQ DB 13,10,'STRINGS ARE

NOT EQUAL.$'

\r\n write strings are not equal and goto

next line

LM DB 13,10,'LENGTH OF

STRING 1 :'

\r\n write 'Length of string1 and goto

next line

L1 DB 30H,30H,13,10,'LENGTH

OF STRING 2 :'

Write length \r\n and display 'Length of

string2

L2 DB 30H,30H,'$' write Length

.CODE Declaration of code segment

MOV AX,@DATA

Move start address of data segment to

dS and es

MOV DS,AX

MOV ES,AX

LEA DX,M1 load address of msg into dx

MOV AH,09H read string

INT 21H

LEA DX,S1 load buffer start address into dx

MOV AH,0AH read string to a buffer

Page 19: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

INT 21H execute function

LEA DX,M2 load address of msg into dx

MOV AH,09H read string

INT 21H

LEA DX,S2 load buffer start address into dx

MOV AH,0AH read string to a buffer

INT 21H execute function

LEA SI,S1S load address of actual string

LEA DI,S2S load address of actual string

MOV CL,S1L store length of original string in cl

CMP CL,S2L

compare length of 1st string with 2nd

string

JNE DISN not equal branch to display not equal

MOV CH,0 intailize ch with 0

REPE CMPSB

compare string and repeat if equal(ZF=1)

and till cx=0

JE EQ1 branch both are equal

DISN: LEA DX,NEQ

otherwise load dx with not equal msg

address

JMP DISP branch display

EQ1 : LEA DX,E load dx with equal msg address

DISP: MOV AH,9 load no 9 for read string

INT 21H execute function

MOV AL,S1L copy length of string into al

AAM Convert number bcd no( al/10-

>al=remainder ah=quotient

CMP AH,09H compare value with 9

JBE AD30 if below add 30 only

ADD L1,7 else add 30+7

AD30 : ADD L1,AH add 30+ah or 30+7+ah

CMP AL,09H Cmpare al with 09

JBE AD130 if below add 30 only

ADD L1+1,7 else add 30+7

Page 20: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

AD130 : ADD L1+1,AL add 30+al or 30+7+al

MOV AL,S2L copy length of string into al

AAM Convert number bcd no( al/10-

>al=remainder ah=quotient

CMP AH,09 compare value with 9

JBE AD230 if below add 30 only

ADD L2,7 else add 30+7

AD230 : ADD L2,AH add 30+ah or 30+7+ah

CMP AL,09H Compare al with 09

JBE AD330 if below add 30 only

ADD L2+1,7 else add 30+7

AD330 : ADD L2+1,AL add 30+al or 30+7+al

LEA DX,LM store the address of length of string1

MOV AH,09H display the length of string

INT 21H execute function

EXT : MOV AH,4CH load no 4c to goto dos promote

INT 21H terminate goto dos promote

END End

Page 21: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

7. a) Read your name from the keyboard and display it at a specified location on

the screen in front of the message What is your name? You must clear the entire

screen before display.

.MODEL SMALL allocate 1CS and 1DS

.DATA declaration of code segment

A DB 'WHAT IS YOUR NAME

$' create array of string

B DB 25 DUP (0) create array of string

.CODE

MOV AX,@DATA store address of data segment

MOV DS,AX transfer from ax to ds

CALL CLS call procedure clear the screen

MOV DH,11 store dh with row line 11

MOV DL,32 store dl with column line 32

MOV AH,02 set cursor position

INT 10H execute function

LEA SI,B

store the address of where name was

stored

NEXT :MOV AH,01 read character from screen

INT 21H execute function read character

MOV [SI],AL load character from al to address(b)

INC SI increment si

CMP AL,13 compare read character with 13

JNE NEXT

repeat reading character till enter key

pressed

MOV BYTE PTR [SI],'$' transfer $ to lower byte of si

MOV DH,12 store dh with row line 12

MOV DL,25 store dl with column line 25

MOV AH,02 set cursor position

INT 10H execute function

LEA DX,A load address of what is your name

MOV AH,09 read string function no into ah

INT 21H execute function

LEA DX,B load address of name

MOV AH,09 read string function no into ah

INT 21H execute function

MOV AH,4CH terminate goto dos promote

INT 21H execute function

CLS PROC

Page 22: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

MOV AH,2 set cursor function no 2 into ah

MOV DX,0 set cursor (0,0)

INT 10H execute set cursor function

MOV DL,' ' stroe blank into dl

MOV CX,25*80

set count to size of window either 25*80

or 50*80

BLANK:MOV AH,02 read character no2

INT 21H execute function

LOOP BLANK repeat till cx=0

RET return to main

ENDP end of procedure

END end of main programe

Page 23: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

8. a) Compute the factorial of a positive integer ‘n’ using recursive procedure.

CODE COMMENTS

.MODEL SMALL allocate 1CS and 1DS

.DATA declaration of data segment

N DW 5 number to be stored in n

RES DW DUP(0) result stores in res location

.CODE declaration of code segment

MOV AX,@DATA store address of data segment into AX

MOV DS,AX mov from AX to DS

MOV AX,N store number in AX

CALL FACT call factorial function

INT 3H break the program without erasing the content

FACT PROC

CMP AX,1 compare number with one

JBE EXIT if below or equal come out of program

PUSH AX else push content of AX into stack

DEC AX decrement AX

CALL FACT call factorial function

POP AX pop the content of AX

MUL RES multiply AX content with res and stored in AX

MOV RES,AX move from AX to res

RET return to after second call fact

EXIT: MOV AX,1 otherwise move 1 to AX

MOV RES,1 move 1 to result

RET

return to after second call fact ie pop AX(till it

remove all no 2,3,4,5 if n=5)

ENDP end of procedure

END end of main

Page 24: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

9. a) Compute nCr using recursive procedure. Assume that ‘n’ and ‘r’ are non-

negative integers.

.MODEL SMALL Allocate 1cs and 1ds

.DATA Declaration of data segment

N DW 6 Store n=6

R DW 3 Store r=3

NCR DW 1 DUP(0) Create ncr variable initialize with zero

.CODE Declaration of code segment

MOV AX,@DATA store address of data segment

MOV DS,AX move from ax to ds

MOV AX,N Store N in AX

MOV BX,R Store R in BX

CALL NCR1 Call ncr1 procedure

INT 3H Break point

NCR1 PROC NCR1 procedure

CMP BX,AX If r=n nCr=1

JE R1 Jump to R1 when N=R

CMP BX,00H If r=0 nCr=1

JE R1 Jump to R1 when R=0

CMP BX,01H If r=1 nCr=n

JE R2 Jump to R2 when R=1

DEC AX Else dec AX

CMP BX,AX If r=n-1 nCr=n

JE R3 Jump to R3 when R=n-1

PUSH AX

Else nCr=n-1Cr +n-1Cr-1 so push AX &push

BX

PUSH BX

CALL NCR1 Call NCR1 procedure

POP BX Then pop BX & AX

POP AX

DEC BX Dec BX to get R=R-1

PUSH AX Then push BX & AX

PUSH BX

CALL NCR1 Call NCR1 procedure

POP BX Then pop BX & AX

POP AX

RET Ret

R1:INC NCR Inc NCR

RET Return back where called

Page 25: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

R3:INC NCR Inc NCR

R2:ADD NCR,AX Add NCR + AX

RET Return back where called

ENDP End of procedure

END End of main

.MODEL SMALL

.DATA

N DW 6

R DW 3

NCR DW 1 DUP(0)

.CODE

MOV AX,@DATA

MOV DS,AX

MOV AX,N ax=4

MOV BX,R bx=2

CALL NCR1

call

ncr1

INT 3H

NCR1 PROC

CMP BX,AX 2 vs 4 2 vs 3

1 vs 3

JE R1 nj nj

Nj

CMP BX,00H 2 vs 0 2 vs 0

1 vs 0

JE R1 nj nj

Nj

CMP BX,01H 2 vs 1 2 vs 1

1 vs 1

JE R2 nj nj

J

DEC AX ax=3 ax=2

CMP BX,AX 2 vs 3 2 vs 2

JE R3 nj j

PUSH AX 3

PUSH BX 2

Page 26: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

CALL NCR1

call

ncr1

POP BX 2

POP AX 3

DEC BX 1

PUSH AX 3

PUSH BX 1

CALL NCR1

CALL

NCR1

POP BX

1

POP AX

3

RET

Ret to

main

R1:INC NCR

RET

R3:INC NCR 1

R2:ADD NCR,AX 1+2=3

3+3=6

RET

ENDP

END

Page 27: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

10. a) Find out whether a given sub-string is present or not in a main string of

characters.

.MODEL SMALL Allocate 1 cs and 1 ds

.DATA Declaration data segment

MSTR DB 'THE MESSAGE' create array of string

MLEN DW MLEN-MSTR length(end address-start address)

SSTR DB 'MES' create array of substring

SLEN DW SLEN-SSTR

length of substring(end address-

start address)

P DB 'SUBSTRING IS PRESENT$'

store string to display substring

not present

NP DB 'SUBSTRING IS NOT

PRESENT$'

store string to display substring

present

.CODE

MOV AX,@DATA store address of data segment

MOV DS,AX move from ax to ds

MOV ES,AX move from ax to es

MOV AX,MLEN store length of string in ax

SUB AX,SLEN

subtract main string from

substring length

INC AX

increment ax which gives how

many comparison requires

LEA BP,MSTR load address of main string in bp

AGN:MOV SI,BP move address into si from bp

LEA DI,SSTR load substring address in di

MOV CX,SLEN

store substring length in cx to

compare with main string

REPE CMPSB

repeat compare each letter in

main with all letter in substring

till cx=0

JE SUS

branch if equal to display sustring

found

INC BP

else increment bp compare next

letter with substring letters

DEC AX decrement ax

JNZ AGN

not 0 repeat compare next letter

of main with substring

LEA DX,NP

else store the address of not

present message

Page 28: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

JMP EXIT branch to exit

SUS:LEA DX,P

if present store address of present

message

EXIT:MOV AH,09 store function 09 to display string

INT 21H execute function

MOV AH,4CH store 4c in ah terminate program

INT 21H execute function

END end of main

Page 29: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

11. a) Generate the first ‘n’ Fibonacci numbers.

CODE COMMENTS

.MODEL SMALL allocate 1 cs and 1 ds

.DATA declare data segment

X DB 10 DUP(?) create array for storing fibo series

N DW 0AH store n value

.CODE

MOV AX,@DATA store address of data segment in ax

MOV DS,AX move address from ax to ds

MOV AL,0 store 0 in al

MOV X,AL move to memory address x

MOV AL,1 store 1 in al

MOV X+1,AL move al content to next location in x+1

MOV CX,N store n value in cx acts as counter

LEA SI,X store address of x in si

L1: MOV AL,[SI] mov content of si in al

ADD AL,[SI+1] add content of al with next address content

DAA convert into decimal number

MOV [SI+2],AL transfer content of al to next location

INC SI increment si before storing next value

LOOP L1 loop till cx=0 auto decrement by loop instn

INT 3H execute function

END end of main

Page 30: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

12. a) Read the current time from the system and display it in the standard format

on the screen.

.MODEL SMALL Allocate 1cs and 1ds

.CODE Declaration of code segment

MOV AH,2CH Store function code 2ch to get system time

INT 21H Execute function

MOV AL,CH Store hours from ch to al

AAM Separate both digit (ah and al)

MOV BX,AX Mov to bx

MOV DL,BH To display 1st digit store in dl(input)

CALL DISP Call disp procedure disp a character

MOV DL,BL To display 2nd digit store in dl(input)

CALL DISP Call disp procedure disp a character

MOV DL,':' Store : in dl

MOV AH,02H Store code of display character

INT 21H Execute function

MOV AL,CL Store minutes from cl to al

AAM Separate both digit (ah and al)

MOV BX,AX Mov to bx

MOV DL,BH To display 1st digit store in dl(input)

CALL DISP Call disp procedure disp a character

MOV DL,BL To display 2nd digit store in dl(input)

CALL DISP Call disp procedure disp a character

MOV AH,4CH Store 4ch to Terminate process

INT 21H Execute function

DISP PROC Procedure to display a character

ADD DL,30H Add 30h to get ascii code

MOV AH,02H To display a character store 02 in ah

INT 21H Execute function

RET Return to main

ENDP End of procedure

END End of main

Page 31: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

13. a) Program to simulate a Decimal Up-counter to display 00- 99.

CODE COMMENTS

.MODEL SMALL allocate 1 cs and 1 ds

.CODE declare code segment

START: CALL CLS call procedure to clear the screen

MOV CL,30H store 30h in cl to display in ascii(0)

MOV CH,30H store 30h in ch to display in ascii(0)

NXT: MOV AH,2 store 2 in ah to position cursor

MOV DH,12 store row number in dh

MOV DL,39 store column in dl

INT 10H execute function to set cursor

MOV DL,CH store 0 in dl

MOV AH,2 to display from 00-99 character store 2 in ah

INT 21H execute function to display a character

MOV DL,CL store 0 in dl

MOV AH,2 to display 00-99 character store 2 in ah

INT 21H execute function to display a character

CALL DLY

call delay program to wait for sometime before

display next number

INC CL increment cl

CMP CL,39H

compare cl with 39 after 39(9) it should start

with 0

JBE NXT if below or equal repeat display

MOV CL,30H else again store 0 in cl

INC CH increment ch

CMP CH,39H

compare cl with 39 after 39(9) it should start

with 0

JBE NXT

if below or equal repeat display (CH<=39)

CF=1OR ZF=1

MOV AH,4CH store 4c to ah to terminate program

INT 21H execute function

DLY PROC delay procedure

MOV SI,05FFH

store count(05ff) to repeat instruction to increase

time)

OLUP: MOV DI,0FFFFH

store count(0fff) to repeat instruction to increase

time)

ILUP: NOP

no operation just to provide delay by execute

instruction

Page 32: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

DEC DI decrement di

JNZ ILUP branch if not zero ilup ZF=0

DEC SI decrement si

JNZ OLUP branch if not zero olup ZF=0

RET return to main

ENDP end of proc

CLS PROC clear procedure

MOV AH,2 store 2 in ah to position the cursor

MOV DX,0 store 0 in dx(0,0)

INT 10H execute function set cursor position

MOV DL,' ' store blank in dl

MOV CX,25*80 store cx with either with 25* 80 or 50*80

BLNK: MOV AH,02 display blank store 02 in ah

INT 21H execute function to display

LOOP BLNK repeat till cx=0

RET return to main where called

ENDP end of procedure

END START end start

Page 33: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

14. a) Read a pair of input co-ordinates in BCD and move the cursor to the

specified location on the screen.

.MODEL SMALL Allocate 1 CS and 1 DS

.DATA Declare data segment

X DB ? create a variable x without intialize

Y DB ? create a variable y without intialize

XMSG DB 13,10,'ENTER THE X-

CO-ORDINATE(0-79):$'

store message enter value of x in array of

string xmsg

YMSG DB 13,10,'ENTER THE Y-

CO-ORDINATE(0-24):$'

store message enter value of x in array of

string xmsg

.CODE Declare code segment

CLS PROC Clear procedure start

MOV AH,2 display character function no 2 in ah

MOV DX,0 store (0,0) in DX for set cursor

INT 10H execute function for set cursor

MOV DL,' ' store blank in DL

MOV CX,25*80 store count in CX

BLNK:MOV AH,02 read character

INT 21H execute function for read character

LOOP BLNK repeat till CX=0

RET return from procedure

ENDP end procedure

PROC RDKB procedure for read from keyboard

AGN:MOV AH,1 store 8 in AH function no to read

Page 34: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

INT 21H execute function

CMP AL,13

compare content of AL with 13(check for

enter key pressed

JE NXT branch if equal to nxt

CMP AL,30H compare AL content with 30h

JB AGN branch if less read again

CMP AL,39H compare AL with 39h

JA AGN branch if above ,read again

NXT:RET return from the procedure

ENDP Edn of procedure

START:MOV AX,@DATA store address of data segment in AX

MOV DS,AX transfer from ax to DS

MOV AH,9 store 9 in AH to read the string

LEA DX,XMSG load message in DX

INT 21H execute function

L1:CALL RDKB call read keyboard user defined procedure

CMP AL,13 compare AL content with 13

JE L1 if equal branch l1

SUB AL,30H subtract 30 from AL

MOV X,AL transfer from AL to x

CALL RDKB call read keyboard procedure

CMP AL,13 compare AL content with 13

Page 35: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

JE L2 if equal branch l1

SUB AL,30H subtract 30 from AL

MOV AH,X transfer from AL to x

AAD

used to convert decimal to hex value(ex:- 25

ah=02 al=05 2*10+5=20+5=25=19h)

MOV X,AL move from AL to x

L2:MOV AH,9 display a string

LEA DX,YMSG load ymsg address into DX

INT 21H execute function

L3:CALL RDKB call read keyboard procedure

CMP AL,13 compare al content with 13

JE L3 if equal branch l1

SUB AL,30H subtract 30 from AL

MOV Y,AL transfer from AL to y

CALL RDKB call read keyboard procedure

CMP AL,13 compare AL content with 13

JE L4 if equal branch l1

SUB AL,30H subtract 30 from AL

MOV AH,Y transfer from AL to y

AAD

used to convert decimal to hex value(ex:- 25

ah=02 al=05 2*10+5=20+5=25=19h)

MOV Y,AL move from AL to y

L4:CALL CLS call clear screen procedure

Page 36: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

MOV DH,Y transfer y content to DH

MOV DL,X transfer x content to DL

MOV AH,2 to set cursor store 2 in AH

INT 10H execute function to set the cursor

MOV AH,08 read a character without echo

INT 21H execute function

MOV AH,4CH terminate program

INT 21H execute function

END START end of main

Page 37: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

15. a) Program to create a file (input file) and to delete an existing file.

.MODEL SMALL Allocate 1 CS and 1 DS

.DATA Declare data segment

CPATH DB 'SVS',0

Name of the file(filepath DB "C:\mydir", 0)path

to be created, zero terminated string

CSMSG DB 'FILE CREATED

SUCCESSFULLY$' store message in csmsg

CFMSG DB 'FILE CREATION

ERROR!.....$' store error message in cfmsg

DPATH DB 'SVS',0 path to created file to delete

DSMSG DB 13,10,'FILE

DELETED SUCCESSFULLY$' store dsmsg to delete file

DFMSG DB 13,10,'FILE

DELETION ERROR!.....$' store error message in dfmsg

.CODE Declare code segment

MOV AX,@DATA store address of data segment in AX

MOV DS,AX move from AX to DS

LEA DX,CPATH load address of file path in DX

MOV CX,0

store 0 in CX for normal operation (file

attribute)instead of specifying read, write only

etc

MOV AH,5BH store function no 5b in AH to create a file

INT 21H execute function

JC CERR branch if carry (if error CF=1)

LEA DX,CSMSG else load address of create message

JMP DISP branch to display string

CERR:LEA DX,CFMSG if error load address of error message

Page 38: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

DISP:MOV AH,09H store 09 in AH to display string

INT 21H execute function

LEA DX,DPATH load address of file in DX

MOV CX,0 store 0 to CX

MOV AH,41H store function no 41h in AH to delete a file

INT 21H execute function

JC DERR branch if CF=1 then error

LEA DX,DSMSG load address of message in DX

JMP DSP branch to display

DERR:LEA DX,DFMSG else load address of delete a file info message

DSP:MOV AH,09H store 09 to AH

INT 21H execute function

EXIT:MOV AH,4CH terminate program fun no 4ch in AH

INT 21H execute function

END End

Page 39: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

PART B:-

1b) Read the status of eight input bits from the Logic Controller Interface and

display ‘FF’ if it is even Parity bits otherwise display 00. Also display number of

1’s in the input data.

.MODEL SMALL allocate 1 CS and 1 DS

.CODE declaration of code segment

MOV DX,0C80FH store the address for to load control word

MOV AL,82H making B port as input and A & C as output

OUT DX,AL send control word to program 8255 as I/o

AGN: MOV DX,0C80DH store address of B port(logic controller) in dx

IN AL,DX input to al

MOV CX,8 set counter to 8 to count even and odd all bits

MOV BL,0 initialize counter bl to zero to count odd

NXT: SHR AL,1

shift right one bit at a time to find odd or even(0-

>MSB->LSB->CF)

Page 40: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

JNC CNTN if no carry set jump

INC BL else increment bl

CNTN: LOOP NXT loop until cx!=0

TEST BL,01

and content of bl with 01 without alter the content

of bl only ZF affects

JZ EVP if ZF=1 jump

MOV AL,0 else number is odd so store zero in al

JMP DISP jmp to disp

EVP: MOV AL,0FFH if even store ff in al

DISP: MOV DX,0C80CH output address of A port store in dx

OUT DX,AL output content of al (FFor00)

MOV AL,BL move content of bl to al

MOV DX,0C80EH move address of port C(L) into DX

OUT DX,AL output number of ones

MOV AH,0BH check input status

INT 21H execute function

CMP AL,0 compare al with 0

JZ AGN if ZF=1 jump

MOV AH,4CH else terminate

INT 21H execute function

END end

Page 41: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

2b) Perform the following functions using the Logic Controller Interface.

i. BCD up-down Counter ii. Ring Counter .MODEL SMALL allocate 1 CS and 1 DS

.DATA declaration of data segment

MSG DB 13,10,'1. UP-DOWN

COUNTER',13,10 choice for updown or ring counter

DB '2. RING COUNTER' ,13,10

DB 10,13,'ENTER YOUR CHOICE : $'

EMSG DB 10,13,'ERROR IN CHOICE

...',13,10

error choice message if they press other then 1 and

2

DB 'ENTER CORRECT(1 OR 2)

ANSWER: $'

.CODE

MOV AX,@DATA store address of data segment into ax

MOV DS,AX then to ds

MOV DX,0C80FH move control word address to dx

MOV AL,80H select only A port as output

OUT DX,AL output to program 8255

ERRCH: LEA DX,MSG load address of choice into dx to display

MOV AH,9 display 9 to ah

INT 21H execute function

MOV AH,1 move 01 to ah to accept the character

INT 21H execute function

CMP AL,'1' compare al with 1

JE CHCE1 if 1 choice is updown counter

CMP AL,'2' compare al with 2

JE CHCE2 if 2 choice is ring counter

LEA DX,EMSG

else display error message store address of error

message

MOV AH,9 display string 9 to ah

INT 21H execute function

JMP ERRCH jmp again to enter choice

CHCE1: CALL UPDWN else call updown if 1

JMP EXIT come out

CHCE2: else call ring if 2

CALL RING

EXIT: MOV AH,4CH terminate program

INT 21H execute function

PROC UPDWN procedure start

MOV DX,0C80CH store address of PORT A in dx

MOV AL,0 store 0 in al

UPC: OUT DX,AL output 0

Page 42: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

CALL DLY call delay

ADD AL,1 add 1 to al

DAA convert to BCD

CMP AL,99H compare al with 99h

JB UPC jump below repeat

DNC: OUT DX,AL else output

CALL DLY call delay

SUB AL,1 subtract 1 from al content

DAS convert to BCD

JNZ DNC if al not equal to zero reapeat

RET else return to main

ENDP end of procedure

PROC RING procedure for ring counter

MOV DX,0C80CH store address of PORT A in dx

MOV BL,1 store bl with 1

RC: MOV AL,BL mov bl to al

OUT DX,AL output 1

CALL DLY call delay

ROL BL,1 rotate left bl content by 1

MOV AH,0BH wait status of input

INT 21H execute function

CMP AL,0 compare al with 0

JE RC if equal repeat

RET else return from procedure

ENDP end of procedure

PROC DLY delay procedure

MOV DI,5FFH

store count(05ff) to repeat instruction to increase

time)

OLUP: MOV SI,0FFFFH

store count(0fff) to repeat instruction to increase

time)

ILUP: DEC SI decrement si

JNZ ILUP branch if not zero ilup ZF=0

DEC DI decrement di

JNZ OLUP branch if not zero olup ZF=0

RET Return to main

ENDP Endp

END End

Page 43: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

3b) Read the status of two 8-bit inputs (X & Y ) from the Logic Controller

Interface and display X*Y.

.MODEL SMALL allocate 1CS and 1 DS

.DATA declare data segment

XMSG DB 10,13,'ENTER X VALUE

IN PORT B & PRESS ANY KEY $' enter first value message

YMSG DB 10,13,'ENTER Y VALUE

IN PORT B & PRESS ANY KEY $' enter second value message

PMSG DB 10,13,'LOWER BYTE IS

DISPLAYED PRESS ANY KEY TO

DISPLAY HIGHER BYTE OF THE

RESULT $'

first display lower byte then higher

byte

.CODE declare code segment

MOV AX,@DATA move address of data segment into ax

MOV DS,AX then to DS

MOV DX,0C80FH store address of control word

MOV AL,82H

control word in al(PORT-A O/P

AND PORT-B I/P)

OUT DX,AL output to program 8255

AGN: LEA DX,XMSG load address of xmsg

MOV AH,9 display string

INT 21H execute function

MOV AH,08H accept value without echo

INT 21H execute function

MOV DX,0C80DH store address of PORT-B as an input

IN AL,DX store 1st value in al

MOV BH,AL move that to bh

LEA DX,YMSG store address of ymsg

MOV AH,9 display string

INT 21H execute function

MOV AH,08H accept value without echo

INT 21H execute function

MOV DX,0C80DH store address of PORT-B as an input

IN AL,DX store 2nd value in al

MUL BH multiply al with bh(AX=AL*BH)

MOV BH,AH BH=AH

MOV DX, 0C80CH store address of PORT-A as output

OUT DX,AL output lower byte first(al)

Page 44: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

LEA DX,PMSG print message

MOV AH,9 display string

INT 21H execute function

MOV AH,08H check input status

INT 21H execute function

MOV DX,0C80CH store address of PORT-A as output

MOV AL,BH store HIGHER BYTE in al

OUT DX,AL Is sent as output to PORT-A

MOV AH,4CH terminate program

INT 21H execute function

END end of main

Page 45: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

4b) Display messages FIRE and HELP alternately with flickering effects on a 7-

segment display interface for a suitable period of time. Ensure a flashing rate that

makes it easy to read both the compute these values).

.MODEL SMALL allocate 1 CS and 1 DS

.DATA declare data segment

FLUT DB 86H,88H,0CFH,8EH seven segment code of FIRE(storing ERIF)

SLUT DB 8CH,0C7H,86H,89H seven segment code of HELP(Storing PLEH)

.CODE declare code segment

MOV BX,@DATA load ds with data segment address

MOV DS,BX

MOV AL,80H cw=80h,PB-o/p,PC-o/p

MOV DX,0C80FH move control word address to dx

OUT DX,AL to program 8255

AGN: LEA SI,FLUT load effective address of flut to si

CALL DISP call display procedure

CALL DLY call delay procedure

LEA SI,SLUT load effective address of slut to si

CALL DISP call display procedure

CALL DLY call delay procedure

MOV AH,0BH check if key is pressed in computer keyboard

Page 46: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

INT 21H

CMP AL,0 if zero, no key pressed

JZ AGN so, continue

MOV AH,4CH else, exit to dos prompt

INT 21H

DISP PROC disp procedure

MOV CX,4 cx has no. of digits to be displayed

NXTDIGIT:MOV BL,[SI] move contents of [si] to bl

MOV BH,8 bh has no. of bits to be displayed(number of

segment per digit)

NXTBIT: ROL BL,1 rotate bl left by 1(serially bit per bit sending

to 7 segment display)

MOV AL,BL move bl to al

MOV DX,0C80DH move port b address to dx

OUT DX,AL output bit to PB0(only LSB of al shifted to

shift register)

MOV AL,00H make al 0 for clock low(to activate accept

PB0 bit)

INC DX get PORT C address

OUT DX,AL output clk to PC4

MOV AL,10H make bit 4 high for clk high(to deactivate)

OUT DX,AL output clk to PC4

DEC BH decrement bits count

JNZ NXTBIT if not zero go to next bit

INC SI increment si for next digit

LOOP NXTDIGIT loop 4 times for 4 digits

RET return from procedure

ENDP end of procedure

DLY PROC dly procedure

MOV CX,0300H mov cx with 0300

OLUP: MOV DI,0FFFFH load di with ffff

ILUP: DEC DI decrement di

JNZ ILUP zf=0 loop

LOOP OLUP cx!=0 loop

RET return from procedure

ENDP end of procedure

END end of main

Page 47: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

5b) Assume any suitable message of 12 characters length and display it in the

rolling fashion on a 7-segment display interface for a suitable period of time.

Ensure a flashing rate that makes it easy to read the messages. (Examiner does not

specify these delay values nor it is necessary for the student to compute these

values).

.MODEL SMALL ;memory model

.DATA ;data segment

TBL DB

0C0H,0CFH,0A4H,0B0H,99H,92H,83H 7 segment code 0-9A,B and 3 blank

DB

0F8H,80H,98H,88H,83H,0FFH,0FFH,0FFH

.CODE code segment

MOV BX,@DATA load ds with segment base address

MOV DS,BX

MOV AL,80H cw=80h,PB-o/p,PC-o/p

MOV DX,0C80FH move control word address to dx

OUT DX,AL to program 8255

AGN: MOV BP,14 Store counter 0-14 times

NXTSTR: MOV CX,4 Each time 4 digit

MOV SI,BP

Store last address of table(display

from blank)

NXTD: MOV BL,TBL[SI] Store 7 segment code in bl to display

CALL DISP Display proecdure

DEC SI Decrement for next charcter

JNS LUP loop till si>0

MOV SI,14 Otherwise store 14 again in si

LUP: LOOP NXTD loop till cx!=0

CALL DLY (after 4 digit delay)

DEC BP

Decrement bp to display next

character

JNS NXTSTR

If sf is not equal to 1 then loop to

nxtstr

MOV AH,0BH check if key is pressed

INT 21H in computer keyboard]

CMP AL,0 if zero, no key pressed

JZ AGN so, continue

MOV AH,4CH else, exit to dos prompt

INT 21H

Page 48: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

DISP PROC disp procedure

MOV BH,8 bh has no. of bits to be displayed

NXTBIT: ROL BL,1 rotate bl left by 1

MOV AL,BL move bl to al

MOV DX,0C80DH move port b address to dx

OUT DX,AL output bit to PB0

MOV AL,00H make al 0 for clock low

INC DX get port c address

OUT DX,AL output clk to PC4

MOV AL,10H make bit 4 high for clk high

OUT DX,AL output clk to PC4

DEC BH decrement bits count

JNZ NXTBIT if not zero go to next bit

RET return from procedure

ENDP end of procedure

DLY PROC dly procedure

MOV CX,0200H mov count to cx outer loop

OLUP: MOV DI,0FFFFH mov count to di inner loop

ILUP: NOP no oeration

DEC DI decrement di

JNZ ILUP if di not zero loop to ilup

LOOP OLUP

check cx is not equal to zero repeat go

to olup

RET return from procedure

ENDP end of procedure

END end of program

Page 49: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

6b) Convert a 16-bit binary value (assumed to be an unsigned integer) to BCD and

display it from left to right and right to left for specified number of times on a 7-

segment display interface.

.MODEL SMALL allocate 1 CS AND 1 DS

.DATA declare data segment

LUT DB

0C0H,0F9H,0A4H,0B0H,99H,92H

lookup table to store 0-9 7 segment

code

DB 82H,0F8H,80H,98H

SCRL DB 4 DUP(0FFH) blank to display from right to left

NOS DB 4 DUP(?)

DB 4 DUP(0FFH) blank to display from left to right

BIN DW 1234H store binary value

.CODE declare code segment

MOV AX,@DATA load ds with segment base address

MOV DS,AX

MOV AL,80H cw=80h,PB-o/p,PC-o/p

MOV DX,0C80FH cwport :move cw port address to dx

OUT DX,AL to program 8255

XOR AX,AX clear ax

MOV CX,BIN move bin to cx

CMP CX,0 if cx==0,

JZ NXT jump to nxt

AGN: ADD AL,1 increment al

DAA perform decimal adjust after addtion

JNC CNTN if no carry, goto cntn

MOV BL,AL

MOV AL,AH move ah to al

ADD AL,1 increment al

DAA perform decimal adjust

MOV AH,AL move al back to ah

MOV AL,BL and move bl to al

CNTN: LOOP AGN loop till cx=0

NXT: LEA BX,LUT load effective address of lut to bx

MOV BP,AX move ax to bp

MOV CH,04H digit count in ch

LEA SI,NOS load address of nos to 7 segment code

NXTDGT: MOV CL,4 store cl with to rotate left 1 digit

ROL BP,CL rotate left bp by value in cl

MOV AX,BP move rotated value to ax

Page 50: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

AND AX,000FH mask all bits except the lower nibble

XLAT perform mov al,[al][bx]

MOV [SI],AL mov 7segment code to nos array

INC SI inc si

DEC CH decrement digit count

JNZ NXTDGT if nonzero, jump to nxtdigit

NOKY: MOV BP,7 mov bp with 7 4660 - - - -

- - - -

0 - - -

6 0 - -

6 6 - -

4 6 6 0

NXTSTR: MOV CX,4

store 4 in cx to display 4 character at a

time

MOV SI,BP store that in si

NXTD: MOV BL,NOS[SI]

take from nos array store in bl to

display

CALL DISP call display to display a character

DEC SI decrement si

JNS LUP till sf is not equal to 1

MOV SI,7

if sf=1 store si with 7 start again to

display

LUP: LOOP NXTD loop till cx is not equal to 0

CALL DLY call delay

DEC BP dec bp to display next character

JNS NXTSTR till sf is not equal to 1

MOV BP,0

if sf=1 store bp with 0 start again to

display in other direction - - - -4660

- - - -

- - - 4

- - 4 6

- 4 6 6

4 6 6 0

NXTLS: MOV CX,4

store 4 in cx to display 4 character at a

time

MOV SI,BP store that in si

ADD SI,3 add si with 3 to go to 4th position

NXTLD: MOV BL,SCRL[SI]

take from nos array store in bl to

display

Page 51: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

CALL DISP call display to display a character

DEC SI decrement si

JNS LUPL till sf is not equal to 1

MOV SI,7

if sf=1 store si with 7 start again to

display

LUPL: LOOP NXTLD loop till cx is not equal to 0

CALL DLY call delay

INC BP inc bp to display next character

CMP BP,7 compare with 7

JBE NXTLS if below or equal repeat to display

MOV AH,0BH check if key is pressed

INT 21H in computer keyboard]

CMP AL,0 if zero, no key pressed

JZ NOKY so, continue

MOV AH,4CH else, exit to dos prompt

INT 21H

DISP PROC disp procedure

MOV BH,8 bh has no. of bits to be displayed

NXTBIT: ROL BL,1 rotate bl left by 1

MOV AL,BL move bl to al

MOV DX,0C80DH move port b address to dx

OUT DX,AL output bit to PB0

MOV AL,00H make al 0 for clock low

INC DX get port c address

OUT DX,AL output clk to PC4

MOV AL,10H make bit 4 high for clk high

OUT DX,AL output clk to PC4

DEC BH decrement bits count

JNZ NXTBIT if not zero go to next bit

RET return from procedure

ENDP end of procedure

DLY PROC dly procedure

MOV CX,0200H mov count to cx outer loop

OLUP: MOV DI,0FFFFH mov count to di inner loop

ILUP: NOP no oeration

DEC DI decrement di

JNZ ILUP if di not zero loop to ilup

LOOP OLUP

check cx is not equal to zero repeat go

to olup

Page 52: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

RET return from procedure

ENDP end of procedure

END end of program

Page 53: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

7b) Drive a Stepper Motor interface to rotate the motor in clockwise direction by N

steps (N is specified by the examiner). Introduce suitable delay between successive

steps. (Any arbitrary value for the delay may be assumed by the student).

.MODEL SMALL Allocate 1 CS and 1 DS

.DATA Declare data segment

N DW 50 n=no. of steps. 1.8*50=90 degree

.CODE Declare code segment

MOV BX,@DATA load bx with data segment address

MOV DS,BX load ds with segment base address

MOV CX,N move n to cx(no of steps)

MOV DX,0C80FH move control word address to dx

MOV AL,80H control word=80h,port C=o/p

OUT DX,AL to program 8255

DEC DX get port C address

MOV AL,33H 0=on,1=off,so value for 2-phase

clockwise

NXT: OUT DX,AL output to port C

ROR AL,1 rotate right for next phase-on

CALL DELAY wait, so delay

Page 54: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

LOOP NXT till cx!=0

MOV AH,4CH exit to dos prompt

INT 21H

DELAY PROC delay procedure

MOV SI,020H outer loop count

OLUP: MOV DI,0FFFFH inner loop count

ILUP: DEC DI decrement di

JNZ ILUP till di!=0 loop

DEC SI decrement si

JNZ OLUP till si!=0 loop

RET return to main

ENDP end of procedure

CLS PROC Clear procedure start

MOV AH,2 display character function no 2 in ah

MOV DX,0 store (0,0) in DX for set cursor

INT 10H execute function for set cursor

MOV DL,' ' store blank in DL

MOV CX,25*80 store count in CX

BLANK:MOV AH,02 read character

INT 21H execute function for read character

LOOP BLANK repeat till CX=0

RET return from procedure

ENDP end procedure

END end of main

Page 55: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

8b) Drive a Stepper Motor interface to rotate the motor in anti clockwise direction

by N steps (N is specified by the examiner). Introduce suitable delay between

successive steps. (Any arbitrary value for the delay may be assumed by the

student).

.MODEL SMALL Allocate 1 CS and 1 DS

.DATA Declare data segment

N DW 50 n=no. of steps. 1.8*50=90 degree

.CODE Declare code segment

MOV BX,@DATA load bx with data segment address

MOV DS,BX load ds with segment base address

MOV CX,N move n to cx(no of steps)

MOV DX,0C80FH move control word address to dx

MOV AL,80H control word=80h,port C=o/p

OUT DX,AL to program 8255

DEC DX get port C address

MOV AL,33H 0=on,1=off,so value for 2-phase

clockwise

NXT: OUT DX,AL output to port C

ROL AL,1 rotate left for next phase-on

CALL DELAY wait, so delay

LOOP NXT till cx!=0

MOV AH,4CH exit to dos prompt

INT 21H

DELAY PROC delay procedure

MOV SI,020H outer loop count

OLUP: MOV DI,0FFFFH inner loop count

ILUP: DEC DI decrement di

JNZ ILUP till di!=0 loop

DEC SI decrement si

JNZ OLUP till si!=0 loop

RET return to main

ENDP end of procedure

CLS PROC Clear procedure start

Page 56: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

MOV AH,2 display character function no 2 in ah

MOV DX,0 store (0,0) in DX for set cursor

INT 10H execute function for set cursor

MOV DL,' ' store blank in DL

MOV CX,25*80 store count in CX

BLANK:MOV AH,02 read character

INT 21H execute function for read character

LOOP BLANK repeat till CX=0

RET return from procedure

ENDP end procedure

END end of main

Page 57: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

9b) Drive a Stepper Motor interface to rotate the motor in clockwise and

anticlockwise direction by N steps (N is specified by the examiner). Introduce

suitable delay between successive steps. (Any arbitrary value for the delay may be

assumed by the student).

.MODEL SMALL Allocate 1 CS and 1 DS

.DATA Declare data segment

N DW 50 n=no. of steps. 1.8*50=90 degree

.CODE Declare code segment

MOV BX,@DATA load bx with data segment address

MOV DS,BX load ds with segment base address

MOV CX,N move n to cx(no of steps)

MOV DX,0C80FH move control word address to dx

MOV AL,80H control word=80h,port C=o/p

OUT DX,AL to program 8255

DEC DX get port C address

MOV AL,33H 0=on,1=off,so value for 2-phase

clockwise

RIGHT: OUT DX,AL output to port C

ROR AL,1 rotate right for next phase-on

CALL DELAY wait, so delay

LOOP RIGHT till cx!=0

MOV CX,N move n to cx(no of steps)

MOV AL,33H 0=on,1=off,so value for 2-phase

clockwise

LEFT: OUT DX,AL output to port C

ROL AL,1 rotate left for next phase-on

CALL DELAY wait, so delay

LOOP LEFT till cx!=0

MOV AH,4CH exit to dos prompt

INT 21H

DELAY PROC delay procedure

Page 58: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

MOV SI,020H outer loop count

OLUP: MOV DI,0FFFFH inner loop count

ILUP: DEC DI decrement di

JNZ ILUP till di!=0 loop

DEC SI decrement si

JNZ OLUP till si!=0 loop

RET return to main

ENDP end of procedure

END end of program

CLS PROC Clear procedure start

MOV AH,2 display character function no 2 in ah

MOV DX,0 store (0,0) in DX for set cursor

INT 10H execute function for set cursor

MOV DL,' ' store blank in DL

MOV CX,25*80 store count in CX

BLANK:MOV AH,02 read character

INT 21H execute function for read character

LOOP BLANK repeat till CX=0

RET return from procedure

ENDP end procedure

END end of main

Page 59: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

10b) Scan a 8 x 3 keypad for key closure and to store the code of the key pressed in

a memory location or display on screen. Also display row and column numbers of

the key pressed.

Column 1 value-001 column 2 value-010 and column 3 value -100

.MODEL SMALL ;memory model

.DATA ;data segment

LUT DB '0123456789.+-

*/%CEAB'

Store key ascii code of

keyboard in lut

MSG DB 'KEY PRESSED : ' Display key pressed

KEY DB ?,10,13,'ROW NO IS : ' Store key pressed in key

ROW DB ?,13,10,'COLUMN NO

IS : ' Store row value in ROW

COL DB ?,13,10,'$' Store row value in COL

.CODE code segment

MOV AX,@DATA

load ds with segment base

address

MOV DS,AX

MOV DX,0C80FH move control word port

Page 60: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

address to dx

MOV AL,90H

control word=90h,PA-

i/p,PC-o/p

OUT DX,AL to program 8255

AGN: MOV BH,1 col. Count

MOV CX,03 3 columns

MOV BL,01

col. Value(001-1st ,010-2

nd

,100-3rd

) to send to port C

NXT: MOV AL,BL move bl to al

MOV DX,0C80EH move port c address to dx

OUT DX,AL

send particular column as

high

MOV DX,0C80CH move PORT A address to dx

IN AL,DX read i/p from PORT A

CMP AL,0 compare al with 0

JNZ KYPRSD

if key pressed,al will not be

zero

SHL BL,1

else, shift bl left to send next

col. high (010 or 100)

INC BH increment col. Count

LOOP NXT loop nxt

JMP CHKCMPKB check computer keyboard

KYPRSD: CALL DELAY if key pressed, delay

MOV CL,1 make cl as 1(row no)

ONCEAGN:SHR AL,1 shift al right by 1

JC CNTN

if carry,i.e.,if bit shifted

out=1,goto cntn

INC CL else, increment cl

JMP ONCEAGN and jump to onceagn

CNTN: MOV ROW,30H

move 30h to add and get

ascii value

MOV COL,30H

move 30h to add and get

ascii value

ADD ROW,CL add cl to row

Page 61: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

ADD COL,BH add bh to col

MOV AL,CL move cl to al

DEC AL Decrement correct ROW

AGN1: CMP BH,1 check if column is 1

JE DISP if equal, go to disp

ADD AL,8 else, add 8 to al

DEC BH and decrement bh

JMP AGN1 jump to agn1

DISP: LEA BX,LUT

move effective address of lut

to bx

XLAT

perform mov al,[al][bx]

using xlat

MOV KEY,AL

move value obtained from

lut into key

LEA DX,MSG

move msg's effective

address to dx

MOV AH,09H

to display a string use 09h of

int 21h

INT 21H Execute service

CHKCMPKB:MOV AH,0BH check if any key is pressed

INT 21H in computer keyboard

CMP AL,0 if zero, key not pressed

JZ AGN so, continue

MOV AH,4CH else, exit to dos prompt

INT 21H Execute service

DELAY PROC delay procedure

MOV DI,02FFH Move count to di

OLUP: MOV SI,0FFFFH Move count to si

INLUP: DEC SI Decrement si

JNZ INLUP if not zero repeat inner loop

DEC DI Decrement di

JNZ OLUP if not zero repeat outer loop

RET return from procedure

ENDP end of procedure

Page 62: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

END end of program

11b) Scan a 8 x 3 keypad for key closure and simulate ADD and

SUBTRACT operations as in a calculator.

.MODEL SMALL memory model

.DATA data segment

LUT DB '0123456789.+-

*/%CEAB'

Store key ascii code in

lookuptable

MSG DB 13

Declare variable to display

output as 4 + 5 =+09

A DB ?,' ' Declare a to store 1st number

S DB '$',' ' Declare s to store operator

B DB '$',' = + ' Declare b to store 2nd

number

R DB 30H,30H,13,10,'$' Declare r to store result

.CODE code segment

MOV AX,@DATA load ds with

MOV DS,AX segment base address

MOV DX,0DC0FH

move control word port

address to dx

MOV AL,90H

control word=90h,PA-i/p,PC-

o/p

OUT DX,AL

Output thro control port to

program 8255

L1: CALL RDKY Call read keyboard

CMP AL,'0' Compare al content with 0

JB L1 If below repeat read again

CMP AL,'9' Else al content with 9

JA L1 If above repeat read again

MOV A,AL Mov 1st number to A

LEA DX,MSG

Load address of msg to

display 1st number with blank

MOV AH,9

Store function code 9 to

display

Page 63: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

INT 21H Execute service

L2: CALL RDKY Call read keyboard

CMP AL,'+' Compare with +

JE AND If equal jmp to addition

CMP AL,'-' Else compare with -

JNE L2 Else read again

MOV S,AL Mov operator to s

JMP L3 Jmp to l3

ADN: MOV S,AL Mov al to s if +

L3: LEA DX,MSG Load msg address to dx

MOV AH,9

Store function code 9 to ah to

display 1st number with sign/1

st

number with sign 2nd

number

INT 21H Execute service

CALL RDKY Call read keyboard

CMP AL,'0' Compare al content with 0

JB L3 If below repeat read again

CMP AL,'9' Else al content with 9

JA L3 If above repeat read again

MOV B,AL Mov 2nd number to B

CMP S,'+' Compare s content with +

JNE SBTRCT If not equal goto subtraction

ADD AL,A Else add al with A(A+B)

SUB AL,60H

Sub al with 60h because ascii

code added

DISP: AAM

Convert number into

unpacked BCD

ADD R,AH

Add ah(higer byte) with

r(30H)

ADD R+1,AL

Add al(lower byte) with

r+1(30h)

LEA DX,MSG Load address of msg in dx

MOV AH,9

Store 9 in ah to display in

above format

Page 64: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

INT 21H Execute service

MOV AH,4CH Terminate program

INT 21H Execute service

SBTRCT: MOV AL,A Move a to al

SUB AL,B Subtract A-B

JNS DISP If SF=0 then display directly

MOV B+2,'-' Else change sign that position

NEG AL

Take 2s comp of al to get

actual value

JMP DISP Jmp to disp

RDKY PROC Read keyboard procedure

AGN: MOV BH,0 col. Count

MOV BL,01

col. Value(001,010,100) to

send to port C

NXT: MOV AL,BL move bl to al

MOV DX,0DC0EH move portc address to dx

OUT DX,AL send particular column as high

MOV DX,0DC0CH move port A address to dx

IN AL,DX read i/p from port a

CMP AL,0 compare al with 0

JNZ KYPRSD

if key pressed,al will not be

zero

SHL BL,1

else,shift bl left to send next

col. high (010 or 100)

INC BH increment col. count

CMP BH,3 loop nxt 3 times

JB NXT If below repeat

JMP AGN Jmp to agn

KYPRSD: CALL DELAY if key pressed, delay

MOV CL,0 make cl as 0

ONCEAGN:SHR AL,1 shift al right by 1

JC CNTN

if carry,i.e.,if bit shifted

out=1,goto cntn

INC CL else, increment cl

Page 65: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

JMP ONCEAGN and jump to onceagn

CNTN: CMP BH,0 check if column is 1

JE LTBL if equal, go to disp

ADD AL,8 else, add 8 to al

DEC BH and decrement bh

JMP CNTN jump to agn till bh =0

LTBL: ADD AL,CL Add al with cl

LEA BX,LUT

move effective address of lut

to bx

XLAT Replace code

RET Ret to main

ENDP End procedure

DELAY PROC delay procedure

MOV DI,02FFH Move count to di

OLUP: MOV SI,0FFFFH Move count to si

INLUP: DEC SI Decrement si

JNZ INLUP If not zero repeat inner loop

DEC DI Decrement di

JNZ OLUP If not zero repeat outer loop

RET return from procedure

ENDP end of procedure

END end of program

Page 66: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

12b) Generate the Sine Wave using DAC interface (The output of the DAC is to be

displayed on the CRO).

.MODEL SMALL allocate 1 CS and 1 DS

.DATA declare data segment

LUT DB

0H,0DH,1AH,27H,34H,40H,4B

H,55H,5EH,66H,6EH,74H,78H,

7CH,7EH,7FH,7EH,7CH,78H

create lookup table(difference of 6

degree) =sinθ *

7f(0,6,12,……..90……………0)

DB

74H,6EH,66H,5EH,55H,4BH,40

H34H,27H,1AH,0DH,00H

.CODE declare code segment

MOV AX,@DATA

move address of data segment to ds

reg

MOV DS,AX

MOV AL,80H

Initializes control word register

with control word

MOV DX, 0C80FH Control word register address

OUT DX, AL

output control word thro control

port to program 8255

BACK3: MOV SI,OFFSET

LUT

load lookuptable starting address to

si

MOV CX,31 store 31 as count full sine wave

Page 67: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

MOV DX, 0C80CH o/p thro port A ( ;Port A address)

BACK4: MOV AL, [SI] Output Zero volts to Dac

ADD AL,7FH 7f is the reference line

OUT DX, AL output thro port A

INC SI increment si

CALL DELAY call delay

LOOP BACK4 repeat till cx =0

MOV SI,OFFSET LUT

again load si with start address of

lookuptable

MOV CX,31 store 31 as count full sine wave

BACK2: MOV AL,7FH 7f is the reference line

SUB AL,[SI]

subtract al with lookuptable data to

get -ve side of sin wave

OUT DX, AL output thro port A

INC SI increment si

CALL DELAY call delay

LOOP BACK2 loop till cx=0

MOV AH,01 check status of keyboard

INT 16H execute function

JZ BACK3 if zf=1 no key pressed continue

MOV AH, 4CH else terminate

INT 21H execute function

DELAY PROC NEAR delay procedure

MOV BX,0FFFFH mov count to bx

D1: DEC BX decrement bx

JNZ D1 not zero repeat the loop

RET ret to main

DELAY ENDP delay procedure end

END end of main

Page 68: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

13b) Generate a Half Rectified Sine wave form using the DAC interface. (The

output of the DAC is to be displayed on the CRO).

.MODEL SMALL allocate 1 CS and 1 DS

.DATA declare data segment

LUT DB

0H,0DH,1AH,27H,34H,40H,4BH,

55H,5EH,66H,6EH,74H,78H,7CH,

7EH,7FH,7EH,7CH,78H

create lookup table(difference of 6

degree) sinθ *

7f(0,6,12,……..90……………0)

DB

74H,6EH,66H,5EH,55H,4BH,40H

34H,27H,1AH,0DH,00H

.CODE declare code segment

MOV AX,@DATA

move address of data segment to

ds reg

MOV DS,AX

MOV AL,80H

Initializes control word register

with control word

MOV DX, 0C40FH Control word register address

OUT DX, AL

output control word thro control

port to program 8255

BACK3: MOV SI,OFFSET LUT

load lookuptable starting address

to si

MOV CX,31 store 31 as count half wave

MOV DX, 0C40CH o/p thro port A ( ;Port A address)

BACK4: MOV AL, [SI] Output Zero volts to Dac

ADD AL,7FH

7f is the reference line add to

content of al

OUT DX, AL output thro port A

INC SI increment si

CALL DELAY call delay

LOOP BACK4 repeat till cx =0

MOV CX,31 store 31 as count full sine wave

MOV DX, 0C40CH Port A address

MOV AL,7FH 7f is the reference line to al

Page 69: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

BACK2: OUT DX, AL output thro port A

CALL DELAY call delay

LOOP BACK2 loop till cx=0

MOV AH,01 check status of keyboard

INT 16H execute function

JZ BACK3 if zf=1 no key pressed continue

MOV AH, 4CH else terminate

INT 21H execute function

DELAY PROC NEAR delay procedure

MOV BX,0FFFFH mov count to bx

D1: DEC BX decrement bx

JNZ D1 not zero repeat the loop

RET ret to main

DELAY ENDP delay procedure end

END end of main

Page 70: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

14b) Generate a Fully Rectified Sine waveform using the DAC interface. (The

output of the DAC is to be displayed on the CRO).

.MODEL SMALL allocate 1 CS and 1 DS

.DATA declare data segment

LUT DB

0H,0DH,1AH,27H,34H,40H,4BH,55H

,5EH,66H,6EH,74H,78H,7CH,7EH,7F

H,7EH,7CH,78H

create lookup table (difference

of 6 degree) sinθ *

7f(0,6,12,……..90……………

0)

DB

74H,6EH,66H,5EH,55H,4BH,40H34H

,27H,1AH,0DH,00H

.CODE declare code segment

MOV AX,@DATA

move address of data segment

to ds reg

MOV DS,AX

MOV AL,80H

Initializes control word register

with control word

MOV DX, 0C40FH Control word register address

OUT DX, AL

output control word thro

control port to program 8255

BACK3: MOV SI,OFFSET LUT

load lookuptable starting

address to si

MOV CX,31

store 31 as count full sine

wave

MOV DX, 0C40CH

o/p thro port A ( ;Port A

address)

BACK4: MOV AL, [SI] Output Zero volts to Dac

ADD AL,7FH 7f is the reference line

OUT DX, AL output thro port A

INC SI increment si

CALL DELAY call delay

LOOP BACK4 repeat till cx =0

Page 71: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

MOV AH,01 check status of keyboard

INT 16H execute function

JZ BACK3 if zf=1 no key pressed continue

MOV AH, 4CH else terminate

INT 21H execute function

DELAY PROC NEAR delay procedure

MOV BX,0FFFFH mov count to bx

D1: DEC BX decrement bx

JNZ D1 not zero repeat the loop

RET ret to main

DELAY ENDP delay procedure end

END end of main

Page 72: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

15b) Drive an elevator interface in the following way:

i. Initially the elevator should be in the ground floor, with all requests in OFF state.

ii. When a request is made from a floor, the elevator should move to that floor,

wait there for a couples of seconds, and then come down to ground floor and stop.

If some requests occur during going up or coming down they should be ignored

Switch ON and RED LEDS

-indicate the positions of the elevator when it is ON

-indicate the servicing the request when it is OFF

Yellow LEDS-glow when lift moving

4 to 10 decoder:-specify the location on of the elevator-delay to move slow

PA0 -- PA3 -connected to output of 4 to 10 decoder

PA4 -- PA7 -connected to clear output of D-flip flop

PB0 -- PB3 -connected to input from LEDs

GREEN LEDS ---

Page 73: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

-indicate the positions of the elevator when moving

Dual D Flip flop –used to sense the request output the status Q bar

connected to port B through PB0 -- PB4(IN port)

4 to 10 decoder floor

PA0 -- PA3

1001 3rd(G)

1000 Y

0111 Y

0110 2nd(G)

0101 Y

0100 Y

0011 1st(G)

0010 Y

0001 Y

0000 gnd(G)

Code store in lookuptable:-

PB0 PB1 PB2 PB3PA0 PA1 PA2 PA3- code-floor

0 1 1 1 1 0 0 1-79-3rd

floor

1 0 1 1 0 1 1 0-B6-2rd

floor

1 1 0 1 0 0 1 1-D3-1st floor

1 1 1 0 0 0 0 0-E0-Gnd floor

-elevator start from ground floor

- moves up to the top depend upon request

Page 74: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

-highest priority request and one at a time and ignore others

-request each time is sensed to display

- once lift moved all cleared by making LED OFF

-speed of elevator can be changed by varying the number of time the delay routine

is called.

-set flip flop by switch

-reset flip flop by program thro PORT A (PA7 PA6 PA5 PA4)

.MODEL SMALL allocate 1 CS and 1 DS

.DATA declare data segment

CLRTBL DB 0E0H,0D3H,0B6H,79H

create look up table which floor and end address of floor 79-01111001

.CODE declare code segment

MOV AX,@DATA store base address of DS

MOV DS,AX

MOV AL,82H store control word in al -port A -output and PORT B -input

MOV DX,0D60FH store address of control port

OUT DX,AL send CW to 8255 to program

MOV AL,0

The value 00 is passed to port A to clear the 4 flip flop and the elevator position is set to ground floor

MOV DX,0D60CH store address of PORT A OUT DX,AL output throu PORT A

MOV AL,0F0H

The value 0F0 is passed to portA to reset high of the flip flop and the elevator position is still at ground floor

OUT DX,AL output throu PORT A

MOV DX,0D60DH store address of pORT B

SCAN: IN AL,DX Port B value is read to check the status of the request

AND AL,0FH Higher bits pB7 to pB4 are masked(not using)

CMP AL,0FH check for any key pressed by checking

Page 75: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

lower 4bits(PB0 -- PB3 )

JZ SCAN If the value in al is not OF there is a request

REQST: MOV CH,0 check for (gnd to 3rd)so counter start with 0

FNDST: SHR AL,1 shift right 1 bit to check 0 or 1 ,if 0 that floor request

JNC FID if no carry there is request and jump to fid label

INC CH else ch register stores the floor no of the request

JMP FNDST repeat shift right

FID: MOV AL,CH mov ch (floor no ) in al

LEA BX,CLRTBL store starting address of lookuptable in bx

XLAT The value to OFF the corresponding Led is fetched from the look up table

MOV BL,AL move temporary in bl code

OR AL,0F0H Hiher bits are made all ones so that it does not clear the flipflop while it is moving up

MOV BH,AL mov al content bh indicate which floor key is pressed

MOV AL,0F0H start with 1st floor

MOV DX,0D60CH store address of port A

NOTRCHD:CALL DLY call dly procedure

OUT DX,AL output throu PORT A

CMP AL,BH compare content of al with bh wheather it reached request floor

JE REACHED if equal reached

INC AL The position of the elevator is moved to the floor where there is request

JMP NOTRCHD jmp to repeat to search for key pressed

REACHED: CALL DLY wait

Page 76: MPLABMANUAL

HKBK college of engineering Bangalore

Prepared by CS Department

CALL DLY wait

CALL DLY wait

CALL DLY wait slow movement of floor

MOV AL,BL The value fetched from the Lut is passed to porA to clear the flip flop

OUT DX,AL output throu PORT A

OR AL,0F0H or with al content to get count

GODOWN :OUT DX,AL output throu PORT A

CALL DLY call dly procedure

CMP AL,0F0H check for the elevator reached the ground floor

JE EXIT yes exit

DEC AL else decrement count

JMP GODOWN jmp to repeat till al =0

EXIT: MOV AH,4CH terminate the program

INT 21H execute service

DLY PROC delay procedure MOV SI,600H store count in si

OULUP: MOV DI,0FFFFH store count in di

INLUP: DEC DI decrement di

JNZ INLUP if not zero repeat inner loop

DEC SI decrement si

JNZ OULUP if not zero repeat outer loop

RET ret to main

ENDP end procedure

END end of main