Sei sulla pagina 1di 44

Microprocessors and Microcontrollers Lab Manual 1

1. FAMILIARISATION OF MASM/PWB, DOS AND


BIOS INTERRUPTS

OBJECTIVE:-

To familiarise MASM/PWB and most commonly used DOS and BIOS Interrupts.

MASM:-
The Microsoft assembler (MASM) is a utility used for creating DOS/Windows
programs using assembly language. The libraries required for the compilation of DOS
programs are present with MASM by default. The basic tools used for creating DOS
programs are MASM, link and ml.

The assembler for the microprocessor (MASM) can be used in two ways:(1)
with models that are unique to a particular assembler and (2) with full segment
definitions that allow complete control over the assembly process and are universal to
all assemblers.

The assembly language program consists of processor specific instructions and


assembler directives. An assembler directive indicates how an operand or section of a
program is to be processed by the assembler. Some directives generate and store
information in the memory, while the others do not.

The assembly language program may be written in a notepad or any editor and
save it as .asm file in the MASM\BIN folder. After saving the program, enter into the
DOS command prompt in the MASM\BIN folder. Now type ML filename.asm in the
DOS command prompt and press the enter key. This will assemble the program and if,
no errors it creates filename.obj file, list file, and filename.exe file. Now we can
execute the program by typing the filename in the command prompt.

The MASM.exe is the basic assembler which is used to convert the assembly
source text file to an intermediate object module with .obj extension. It can also

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 2

generate an assembly listing file, which has the complete assembly instructions with
mapped memory references.

Programmers Work Bench (PWB):-


PWB or programmers work bench is an IDE for the MASM utility. It consists of
an editor with a menu containing a lot of options such as to create/manage projects,
set various compilation (ie assembly/linking), making and language options. The editor
of PWB is customizable for colors, key assignments and various other editor settings. It
also consists of an integrated help which has a complete listing of all assembly
language instructions with indexing, topic search and global search options.

In addition to above, PWB also take various command line options. Its command
line syntax is PWB [<options >][<files>] where <options> are

/D[S!T!A] Disable CURRENT.STS.TOOLS.INI

/PP<file> open PWB project autoload

/PF<file> open foreign project

/PL open last project

IE(and string) Execute commands

/M{(marks!<lines>)} Short at mark or line number

IR set no edit(read only) mode

IT<file> Temporary file to open

If no options are given, PWB returns to the last visited file and cursor positions
without open project. Also various utilities such as CODEVIEW.GREP etc can be
invoked by menus provided in PWB.

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 3

ASSEMBLER MEMORY MODELS

Memory models define the way that a program is stored in the memory system.
The different memory models available with MASM are tiny, small, medium, compact,
large and huge and flat.

1. .Tiny model programs run only under MS-DOS. Tiny model places all
data and code in a single segment. Therefore, the total program file size
can occupy no more than 64K. Tiny programs are written in .COM
format, which means that the program must be originate at location
100H.

2. .Small model two segments one data segment of 64KB and one code
segment of 64KB.

3. .Medium model contains one data segment of 64KB and any number
of code segments for large programs.

4. .Compact model contains one code segment and any number of data
segments.

5. .Large model supports multiple code and multiple data segments.

6. .Huge model implies that individual data items are larger than a single
segment, but the implementation of huge data items must be coded by
the programmer.

i. Since the assembler provides no direct support for this feature,


huge model is essentially the same as large model.

Usually the small memory model is used. The model contains two segments,
one data segments of 64KB and one code segment of 64 KB.

ASSEMBLER DIRECTIVES;-

Certain directives are used to start a particular type of segment for a model. The
.CODE directive indicates the beginning of the code segments. The .DATA indicates s
the start of the data segment. The .STARTUP directive can be used to load the data

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 4

segment register, set up the stack and define the starting address of a program. The
detailed list of assembler directives is included in the appendix A.

DOS INTERRUPTS:-

DOS interrupts consists of a DOS function call followed by an INT 21H to execute
the DOS function. In order to use DOS function calls, the function number is placed
into register AH, and all other information is loaded into registers. The register AH is
known as DOS function register.

COMMONLY USED DOS INTERRUPTS:-

1. AH=01H : Reads character from standard input device and echoes to the video screen.
The ASCII code of the typed character is stored in the AL register.
AL=8-bit input data(ASCII code of the input key)

2. AH=02H : Write character to a standard output device(monitor)


DL=8 bit output data (ASCII character code)

3. AH=08H : Reads character from standard input device without echo. The ASCII code of
the typed character is stored in the AL register.
AL=8-bit input data (ASCII code of the input key)

4. AH=09H : This is used to display a character string ending with an ASCII of $ ie 24H.
DS:DX=address of the character string.

5. AH=0AH : This is used for buffered keyboard input. The first byte of the buffer contains
the size of the buffer (up to 255). The second byte has the number of characters typed
upon return. The third byte through the end of the buffer contains the character string
typed followed by a carriage return. This function continues to read the keyboard
(displaying data as typed) until either the specified number of characters are typed or
until the enter key is pressed. DS:DX =address of the keyboard input buffer.

6. AH=2CH : Get the system time. It is used to read the system time. The system time is
copied to CH, CL, DH, DL registers. CH register indicates current hours in 0 through 23

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 5

format, CL register indicates the current minute in 0 through 59, DH register indicates
current second in 0 through 59, DL register indicates current 1/100th of a second. All
times are returned in binary format, and hundredths of a seconds may not be available.

BIOS INTERRUPTS:-

These are used to control the I/O environment of the computer. Unlike INT 21H, which
exists in the DOS program, the BIOS function calls are found stored in the system and video
BIOS ROMs. These BIOS ROM function directly control the I/O devices with or without DOS
loaded into the system. The BIOS interrupt is also known as the video services interrupt since
it directly controls the video display in a system. The register AH is used to select the video
service.

VIDEO MODE SELECTION:-

The mode of operation for video display is selected by placing AH 00H and AL=Mode
number, such as 00H, 01H, 02H, 03H, 07H for various text modes and 04H, 05H, 06H etc for
various graphics modes.

CURSOR CONTROL AND OTHER STANDARD FEATURES:-

1) AH=00H : This function selects the video mode and is used to clear the screen.
AL=mode number(00H, 01H, 02H, 03H, 04H, 05H, etc.,).
For example MOV AX,03

INT 10H ;Clear the screen by setting display in the video mode.

2) AH=02H ; This function selects the cursor position. BH=page number (usually zero),
DH=row number, DL=column number.
3) AH=0CH : This function draws a point on the display at specified graphic co-
ordinates.
AL=pixel value (colour of the pixel), CX=column numbers (x co-ordinate)

BH=page number DX=Row number (y co-ordinate).

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 6

RESULT:-

Programmers Work Bench , Macro Assembler, DOS and BIOS interrupts are
familiarized.

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 7

2. STRING MANIPULATION
OBJECTIVE:
1. Write an 8086 MASM program to reverse a string entered from the keyboard.
2. Write an 8086 MASM program to check whether a string is a palindrome or
not.
3. Write a MASM program to concatenate two strings entered from the keyboard.
4. Write a program to read a string from keyboard and convert upper case to lower
case and vice versa. If it contains special characters print message.

ALGORITHM:
REVERSE A STRING
1. Start
2. Read string from keyboard and store in array.
3. Calculate and store string length.
4. Print the last character of the array
5. Length =length-1
6. Is length=0?
yes, then go to step 7
else go to step 4
7. Stop

PALINDROME CHECKING

1. start
2. read string to array
3. initialize count=2
4. calculate and store string length
5. length=length+1
6. is array[length]=array[count]
then go to step 8

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 8

else go to step 7
7. print string is not palindrome and go to step 12
8. length=length-1
9. count=count+1
10. is length>count
yes, then go to step 6
else go to step 11
11. print string is palindrome
12. stop

STRING CONCATENATION

1. Start
2. Read string1 to array1
3. len1=length of string 1
4. len1=len1+2
5. Read string to array2
6. len2=length of string 2
7. count=2
8. array1[len1]=array2[count]
9. Is count=len1+2
then goto step 10
else count=count+1
len1=len1+1
goto step 8
10. count=2
11. Print array1[count]
12. If len1=count
then goto step 13
else count=count+1

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 9

goto step 11
13.Stop

CASE CONVERSION

1. Start
2. Read the string to array
3. Initialize count=1
4. Calculate and store string length
5. Count=count+1
6. Is array[count]<65, then go to step 13
else go to step 7
3. Is array[count]>90, then go to step 8
else go to step11
4. Is array[count]<97,then go to step 13
else go to step 9
5. Is array[count]>122,then go to step 13
else go to step 10
10. Array[count]=array[count]-32 and goto step 12
11. Array[count]=array[count]+32
12. Is length+1=count?
Then goto step 14
Else goto step 5
13. Print its a special character contained string and goto step 17
14. count=2
15. Print array[count]
16. Is count=length+1?
then goto step 17
else count=count +1 and goto step 15
17. Stop

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 10

PROGRAM:

REVERSE A STRING

.MODEL SMALL ;Initialise the small memory model


.STACK ;Initialise the stack segment
.DATA ;Initialise the data segment

MSG1 DB 'ENTER THE STRING: $'


MSG2 DB 'REVERSED STRING : $'

CRLF DB 13,10,'$' ;Carriage return and Line feed


ARR DB 100 DUP(?) ;Array ARR is declared and uninitialised

PUTS MACRO STR ;Macro for printing messages


MOV AH,09H ;Load DOS function number to ;display a
string in to AH reg
MOV DX, OFFSET STR ;Move the offset to the message to be
;displayed in to DX register
INT 21H ;call DOS interrupt
ENDM ;End of Macro

.CODE ;Main program starts


.STARTUP

MOV AX,03H ;Clear video buffer


INT 10H ;call Video services (BIOS) Interrupt

PUTS MSG1 ;Prints msg1


MOV AH,0AH ;BIOS interrupt buffered input
MOV DX,OFFSET ARR ;input string is stored in ARR
MOV ARR,127 ;with string length in ARR[1]
INT 21H ;call DOS interrupt

MOV SI,1
MOV AL,ARR[SI] ;string length is copied to AL
MOV AH,0
MOV SI,AX
INC SI ;to obtain the last character in array

PUTS CRLF
PUTS MSG2 ;prints msg2

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 11

PRINT:
MOV DL,ARR[SI] ;character to print is moved to DL
MOV AH,02H ;write character to STDOUT
INT 21H ;call DOS interrupt
DEC SI
CMP SI,1 ;check whether all characters are read
JNZ PRINT

PUTS CRLF

.EXIT
END ;End of program

PALINDROME CHECKING

.MODEL SMALL ;Initialise the small memory model


.STACK ;Initialise the stack segment
.DATA ;Initialise the data segment
MSG1 DB 'ENTER THE STRING : $'
MSG2 DB ' STRING IS A PALINDROME $'
MSG3 DB 'STRING NOT A PALINDROME $'

CRLF DB 13,10,'$'
ARR DB 100 DUP(?)

PUTS MACRO STR ;macro to print msg's


MOV AH,09H ;Write string to STDOUT
MOV DX, OFFSET STR
INT 21H ;DOS interrupt
ENDM

READ MACRO STR


MOV AH,0AH ;buffered input
MOV DX,OFFSET ARR
MOV ARR,127
INT 21H ;DOS interrupt
ENDM

.CODE
.STARTUP

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 12

MOV AX,03H ;clear the screen using BIOS interrupt


INT 10H

PUTS MSG1 ;prints msg1


READ ARR ;enter the string into the array ARR

MOV SI,1
MOV AL,ARR[SI] ;move string length to AL
MOV AH,0
MOV SI,AX ;move last array location to SI
MOV CX,SI ;load CX with string length
MOV DI,2 ;load DI with 2
INC SI ;increment SI by 1

TOP:
MOV AL,ARR[DI] ;compares the reversed string
CMP AL,ARR[SI] ;with original string
JNE FINISH ;if not equal jumps to finish
INC DI ;increments DI by 1
DEC SI ;decrements SI by 1
LOOP TOP ;CX=CX-1. loop top till CX is zero

PUTS CRLF
PUTS MSG2 ;prints palindrome
JMP LAST ;jump to exit

FINISH:
PUTS CRLF
PUTS MSG3 ;prints not palindrome
LAST:
.EXIT
END ;end of program

STRING CONCATENATION

.MODEL SMALL ;Initialise the small memory model


.STACK ;Initialise the stack segment
.DATA ;Initialise the data segment
MSG1 DB 'ENTER FIRST STRING : $'

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 13

MSG2 DB 'ENTER SECOND STRING : $'


MSG3 DB 'CONCATENATED STRING : $'

CRLF DB 13,10,'$'

ARR1 DB 100 DUP(?)


ARR2 DB 100 DUP(?)
ARR3 DB 200 DUP(?)

PUTS MACRO STR ;macro to print msg's


MOV AH,09H ;Write string to STDOUT
MOV DX,OFFSET STR
INT 21H ;DOS interrupt
ENDM

READ MACRO STR


MOV AH,0AH ;buffered input
MOV DX,OFFSET ARR
MOV ARR,127
INT 21H ;DOS interrupt
ENDM

.CODE ;main program starts here


.STARTUP

MOV AX,03H ;clear screen using BIOS interrupt


INT 10H ;call BIOS interrupt

PUTS MSG1 ;prints msg1


READ ARR1

PUTS CRLF
PUTS CRLF

PUTS MSG2 ;prints msg2


READ ARR2

PUTS CRLF
PUTS CRLF

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 14

MOV SI,1 ;load SI with 1


MOV CL,ARR1[SI] ;load CX register with string
MOV CH,0 ;length of first string

MOV DI,1 ;load DI with 1

COPY1: ;copy first string to third


INC DI
INC SI
MOV AL,ARR1[SI]
MOV ARR3[DI],AL
LOOP COPY1

MOV SI,1
MOV CL,ARR2[SI] ;load CX register with string
MOV CH,0 ;length of second string

COPY2: ;append second string to third


INC DI
INC SI
MOV AL,ARR2[SI]
MOV ARR3[DI],AL
LOOP COPY2

PUTS MSG3 ;prints msg3

MOV SI,1 ;load SI with 1


DEC DI
MOV CX,DI ;load CX with string length of
;third string
PRINT: ;print concatenated string
INC SI
MOV AH,02H ;write character to STDOUT
MOV DL,ARR3[SI]
INT 21H ;call DOS interrupt
LOOP PRINT

NOP ;No Operation

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 15

.EXIT
END ;end of program

CASE CONVERSION

.MODEL SMALL ;Initialise the small memory model


.STACK ;Initialise the stack segment
.DATA ;Initialise the data segment

MSG1 DB 'ENTER A STRING : $'


MSG2 DB 'CASE CONVERTED STRING : $'
MSG3 DB 'THERE IS SOME SPECIAL CHARACTER $'

CRLF DB 13,10,'$'

ARR DB 100 DUP(?)

PUTS MACRO STR ;macro to print msg's


MOV AH,09H ;Write string to STDOUT
MOV DX,OFFSET STR
INT 21H ;call DOS interrupt
ENDM

READ MACRO ARR ;define a macro to read a string from


;keyboard
MOV ARR,127
MOV AH,0AH ;buffered keyboard input
MOV DX,OFFSET ARR
INT 21H ;DOS interrupt
ENDM

.CODE ;main program starts here


.STARTUP

MOV AX,03H ;clear screen using BIOS interrupt


INT 10H ;call BIOS interrupt

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 16

PUTS MSG1 ;prints msg1

READ ARR ;call macro READ


MOV SI,1 ;load SI with 2
MOV CL,ARR[SI] ;move string length to CL
MOV CH,0
INC SI

LOWER_TO_UPPER: ;to convert lower case to upper


MOV AL,ARR[SI] ;move character to AL
CMP AL,65 ;compare if AL is below 65
JB SPECIAL ;if yes jump to special else continue

CMP AL,91 ;compare if AL is below 91


JB UPPER_TO_LOWER ;if yes jump to convert it to lower case

CMP AL,97 ;compare if AL is below 97


JB SPECIAL ;if yes jump to special else continue

CMP AL,122 ;compare if AL is above 97


JA SPECIAL ;if yes jump to special else continue

SUB AL,32 ;convert to upper by subtracting 32


MOV ARR[SI],AL ;save to same string location
JMP NEXT ;jumpt to next

UPPER_TO_LOWER: ;to convert upper case to lower


ADD AL,32 ;convert to lower by adding 32
MOV ARR[SI],AL ;save to same string location

NEXT:
INC SI ;increment string position
LOOP LOWER_TO_UPPER ;loop till complete string is read

PUTS CRLF ;New line after carriage return


PUTS MSG2 ;prints msg2

MOV SI,1
MOV CL,ARR[SI] ;load CX with string length

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 17

MOV CH,0

PRINT: ;prints string


INC SI
MOV DL,ARR[SI] ;load DL with character to write
MOV AH,02H ;write character to STDOUT
INT 21H ;call DOS interrupt
LOOP PRINT ;loop till complete string is written
JMP STOP ;jump to stop

SPECIAL: ;for special characters in string


PUTS CRLF
PUTS MSG3 ;print msg3

STOP:
.EXIT
END ;end of program

OBSERVATION:

REVERSE A STRING

ENTER THE STRING: computer


REVERSED STRING: retupmoc

PALINDROME CHECKING

ENTER THE STRING : NOON


STRING IS A PALINDROME

ENTER THE STRING : ROBOT


STRING NOT A PALINDROME
STRING CONCATENATION

ENTER FIRST STRING : GOOD


ENTER SECOND STRING : MORNING

CONCATENATED STRING : GOODMORNING

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 18

CASE CONVERSION

ENTER A STRING : PENdrive@

CASE CONVERTED STRING : penDRIVE

THERE IS SOME SPECIAL CHARACTER

RESULT:

1. 8086 MASM program to reverse a string is executed and results verified.


2. MASM program to check a string is palindrome or not was executed and the
result verified.
3. MASM program to concatenate the two strings are executed and results
verified.
4. MASM program to change the case of a string is executed and the results
verified.

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 19

3. NUMBER SORTING
OBJECTIVE:

To write a 8086 MASM program to sort a set of numbers into ascending /


descending order.

ALGORITHM:
1. Start
2. Store the numbers separated by comma
3. Store the numbers entered into an array
4. Total = number count (i.e. no. of numbers)
5. count=0
6. If count < Total
Then goto step 7
Else goto step 13
7. POS=0
8. If POS< Total -2
Then goto step 9
Else goto step 12
9. Is array[POS] > array[POS+2]
Then goto step 10
Else goto step 11
10. Swap array[POS] and array[POS+2]
11. POS = POS + 2
12. count = count + 1 and goto step 6
13. Print sorted numbers
14. Stop

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 20

PROGRAM:SORTING IN ASCENDING ORDER

.MODEL SMALL ;Initialise small memory model


.STACK ;initialise the stack segment
.DATA ;initialise the data segment
MSG1 DB 'Enter the numbers separated by comma(below 9999): $'
MSG2 DB 'The stored numbers are: $'
MSG3 DB 'Invalid Entry !!!: $'
MSG4 DB 'The array of sorted numbers are : $'
CRLF DB 13,10,'$'
CBUF DB 100 DUP(?)
DBUF DB 100 DUP(?)
NUM DW 1 DUP(?)
ARR DB 100 DUP(?)
NUMB DW 50 DUP(?)
COUNT DW 1 DUP(?)
AFLAG DW 1 DUP(?)
CLEN DW 1 DUP(?)
DLEN DW 1 DUP(?)
ARPOS DW 1 DUP(?)

PUTS MACRO STR ;define macro to display a string


MOV AH,09H
MOV DX,OFFSET STR
INT 21H
ENDM

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 21

GETS MACRO ARR ;macro to read the number into the array
MOV ARR,127
MOV AH,0AH
MOV DX,OFFSET ARR
INT 21H
MOV SI,1
MOV AL,ARR[SI]
MOV AH,0
MOV SI,AX
ADD SI,2
MOV ARR[SI],'$'
ENDM
CONV_C2D MACRO CBUF,CLEN,DBUF ;ASCII to binary conversion
MOV SI,0
LOOP2:
CMP CBUF[SI],30H
JB INVLD
CMP CBUF[SI],39H
JA INVLD
MOV AL,CBUF[SI]
SUB AL,30H
MOV DBUF[SI],AL
INC SI
CMP SI,CLEN
JB LOOP2
ENDM

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 22

CONV_D2N MACRO DBUF,DLEN,NUM ;number processing


MOV AX,0
MOV SI,0
LOOP3:
MOV DX,AX
MOV AX,10
MUL DX
MOV BL,DBUF[SI]
MOV BH,0
ADD AX,BX
INC SI
CMP DLEN,SI
JA LOOP3
MOV NUM,AX
ENDM
CONV_N2C MACRO NUM,CBUF,DLEN ;number processing for display
MOV SI,0
MOV AX,NUM
LOOP4:
MOV DX,0
MOV BX,10
DIV BX
MOV DBUF[SI],DL
INC SI
CMP AX,0

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 23

JNE LOOP4
MOV DLEN,SI
DEC SI
MOV DI,0
LOOP5:
MOV AL,DBUF[SI]
ADD AL,30H
MOV CBUF[DI],AL
INC DI
DEC SI
CMP DI,DLEN
JB LOOP5
ENDM
PUTSC MACRO CBUF,CLEN
MOV DI,0
LOOP6:
MOV AH,02H
MOV DL,CBUF[DI]
INT 21H
INC DI
CMP DI,CLEN
JB LOOP6
ENDM
PUTSCOMMA MACRO
MOV AH,02H
MOV DL,','

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 24

INT 21H
ENDM
SORT_ARRAY MACRO NUMB,COUNT ;Macro to sort the numbers
MOV CX,COUNT ;Count value is loaded in to CX reg
SUB CX,2
UP:
MOV DX,CX
MOV SI,0
SLOOP1:
MOV AX,NUMB[SI]
CMP NUMB[SI+2],AX
JAE SLOC1
MOV BX,NUMB[SI+2]
MOV NUMB[SI],BX
MOV NUMB[SI+2],AX
SLOC1:
ADD SI,2
SUB DX,2
JNZ SLOOP1
SUB CX,2
JNZ UP
ENDM
.CODE ;main program starts here
.STARTUP
MOV AX,03H ;Clear screen
INT 10H ;call BIOS interrupt

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 25

PUTS MSG1 ;display MSG1


GETS ARR ;enter the numbers to be sorted
PUTS CRLF
MOV COUNT,0
MOV AFLAG,0
MOV ARPOS,2
LOOP8:
MOV BX,0
LOOP1:
CMP BX,4
JAE INVLD
MOV SI,ARPOS
MOV AL,ARR[SI]
MOV CBUF[BX],AL
INC BX
INC ARPOS
MOV SI,ARPOS
CMP ARR[SI],','
JE NEXT1
CMP ARR[SI],'$'
JNE LOOP1
MOV AFLAG,1
NEXT1:
INC ARPOS
MOV CLEN,BX

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 26

CONV_C2D CBUF,CLEN,DBUF
CONV_D2N DBUF,CLEN,NUM
MOV SI,COUNT
MOV AX,NUM
MOV NUMB[SI],AX
INC COUNT
INC COUNT
CMP AFLAG,1
JNZ LOOP8

SORT_ARRAY NUMB,COUNT
PUTS CRLF
PUTS MSG4
PUTS CRLF
MOV CX,0
LOOP7:
MOV SI,CX
MOV AX,NUMB[SI]
MOV NUM,AX
CONV_N2C NUM,CBUF,CLEN
PUTSC CBUF,CLEN
INC CX
INC CX
CMP CX,COUNT
JAE NEXT2
PUTSCOMMA

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 27

JMP LOOP7
NEXT2:
JMP STP1
INVLD:
PUTS MSG3
STP1:
.EXIT
END

OBSERVATION:
Enter the numbers separated by comma (below 9999):98,987,9,1,19
The array of sorted numbers are: 1,9,19,98,987

RESULTS:
MASM program to sort a set of numbers into ascending order is executed and results
verified.

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 28

4. DRAW A SQUARE
OBJECTIVE:

To write an 8086 MASM program to draw a square on the screen.

ALGORITHM:
1. Start
2. Get the dimensions of the square
3. Initialise graphics mode and set pixel colour
4. CX=Column width
5. DX=Row height
6. Draw horizontal lines at 2 values of y coordinates
7. Draw vertical lines at 2 values of x coordinates
8. stop
PROGRAM:TO DRAW A SQUARE

.MODEL SMALL ;initialise the small memory model


.STACK ;initialise the stack segment
.DATA ;initialise the data segment

CRLF DB 13,10,'$'
MSG DB 'ENTER SIZE OF THE SQUARE ( IN PIXELS ): $'
SIZE DW 0 ;initialise the word type variable
HOR_START DW 0
VER_START DW 0
HOR_END DW 0
VER_END DW 0

PUTS MACRO STR ;macro to display the string


MOV AH,09H
MOV DX,OFFSET STR
INT 21H
ENDM

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 29

.CODE ;main program starts here


.STARTUP

PUTS CRLF
PUTS MSG ;prints msg

MOV BX,0
LOOP1:
MOV AH,01H ;Read character from STDIN
INT 21H ;DOS interrupt
CMP AL,13 ;check enter key press
JE LOOP2 ;if yes jump to loop2 else continue
SUB AL,30H ;convert ASCII to BCD
MOV CL,AL
MOV CH,0
MOV DX,10 ;mov 10 to DX
MOV AX,BX ;mov BX to AX
MUL DX ;DX-AX=AX*DX
ADD AX,CX ;AX=AX+CX
MOV BX,AX ;move AX to BX
JMP LOOP1 ;jump to loop1 till a enter key press

LOOP2:
MOV AX,BX ;mov pixels to AX
MOV SIZE,AX ;mov AX to SIZE of square

MOV CX,2H ;load CX with 2


DIV CX ;Divide AX by 2

MOV CX,140H ;Load CX with 320(horizondal centre of


;screen)
SUB CX,AX ;CX=CX-AX

MOV HOR_START,CX ;Move CX to horizondal starting


;position
MOV DX,0F0H ;Load DX with 240(vertical centre of
;screen)
SUB DX,AX ;DX=DX-AX

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 30

MOV VER_START,DX ;move DX to vertical starting


;position
ADD CX,SIZE ;CX=CX+SIZE
MOV HOR_END,CX ;move CX to horizondal ending
;position
ADD DX,SIZE ;DX=DX+SIZE
MOV VER_END,DX ;move DX to vertical ending
;position
MOV AH,0 ;Set Video mode
MOV AL,12H ;graphics mode(640*480pixels)(16colors)
INT 10H ;Video Interrupt

MOV CX,HOR_START ;load CX with horizondal starting position


JMP HORIZONTAL ;Jump to horizondal

DRAW: ;SubRoutine DRAW


MOV AH,0CH ;Set Pixel
MOV AL,0FH ;Pixel color WHITE(1111)
INT 10H ;Video interrupt
RET ;return

HORIZONTAL: ;DX is row and CX is column for set pixel


;mode
MOV DX,VER_START ;Load DX with vertical starting position
CALL DRAW ;Calls draw function(subroutine)
MOV DX,VER_END ;Load DX with vertical ending position
CALL DRAW ;Calls draw function(subroutine)

INC CX ;increments CX by 1
CMP CX,HOR_END ;check whether horizondal end point
;reached
JNA HORIZONTAL ;jump to horizondal if not reached

MOV DX,VER_START ;load DX with vertical starting position

VERTICAL: ;DX is row and CX is column for set pixel


;mode
MOV CX,HOR_START ;Load CX with horizondal starting
position

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 31

CALL DRAW ;Calls draw function(subroutine)


MOV CX,HOR_END ;Load CX with horizondal ending position
CALL DRAW ;Calls draw function(subroutine)

INC DX ;increments DX by 1
CMP DX,VER_END ;check whether vertical end point reached
JNA VERTICAL ;jump to vertical if not reached

.EXIT
END ;end of program

OBSERVATION:
ENTER SIZE OF THE SQUARE ( IN PIXELS ): 100

100 PIXEL

100 PIXEL

RESULT:
MASM program to draw a square is executed and results verified.

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 32

5. TIMER

OBJECTIVE:

To implement a count-down timer to count down from the number (<=9)


entered.

ALGORITHM:

1) Start
2) Read starting count
3) Is 1 second elapsed
4) Then goto step 6
5) Else goto step 3
6) Print count
7) Count=count-1
8) Is count>=0
9) Then goto step 3
10) Else goto step 11
11) Stop

PROGRAM:

.MODEL SMALL ;initialise the small memory model


.STACK ;initialise the stack segment
.DATA ;initialise the data segment

MSG1 DB 'ENTER COUNT IN SECONDS : $'


MSG2 DB ' TIMER $'

CRLF DB 13,10,'$'

ARR DB 10 DUP(?) ;decalred array

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 33

TEN DW 10
COUNT DW 0
SECOND DB 0

PUTS MACRO STR ;Macro for printing


MOV AH,09H ;Write string to STDOUT
MOV DX,OFFSET STR ;move offset of string to DX
INT 21H ;DOS interrupt
ENDM

GET_TIME MACRO ;Macro to retrieve system time


MOV AH,2CH ;Get System time
INT 21H ;DOS interrupt
ENDM

GET_COUNT MACRO COUNT ;Macro to get the counter value


LOCAL L1,L2 ;loops inside a macro declaration
MOV COUNT,0
MOV BX,0
L1:
MOV AH,01H ;Read character from STDIN
INT 21H ;DOS interrupt
CMP AL,13 ;to check whether enter key is pressed
JE L2
SUB AL,48 ;converting ASCII to digit
MOV CL,AL ;move digit to CL
MOV AX,BX ;move BX to AX
MUL TEN ;AX=AX*10
MOV CH,0
ADD AX,CX ;AX=AX+CX
MOV BX,AX ;move AX to DX
JMP L1 ;jump to L1
L2:
MOV COUNT,BX ;move BX to COUNT
ENDM

DISPLAY MACRO ARG ;Macro to print a number

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 34

LOCAL L3,L4 ;Loops inside Macro Declaration


MOV SI,9 ;Move to end of array
MOV ARR[SI],'$' ;Place $ at end of array
MOV AX,ARG ;Move dividend to AX
MOV DX,0 ;Set remainder as 0
L3:
DIV TEN ;Divide AX by 10
ADD DL,48 ;convert remainder to its ASCII
DEC SI ;decrement array position
MOV ARR[SI],DL ;Move remainder ASCII value to array
MOV DX,0 ;Set remainder as 0
CMP AX,0 ;check whether quotient is zero
JNE L3 ;if quotient not equal to zero jump to l3
L4:
DEC SI
MOV ARR[SI],20H ;move blank space to remaining
CMP SI,2 ;array positions
JNE L4

PUTS ARR ;print count present in ARR

ENDM

.CODE ;code segment


.STARTUP

MOV AX,03H ;clear video buffer


INT 10H ;Video interrupt

PUTS CRLF
PUTS MSG1 ;print msg1

GET_COUNT COUNT ;call macro to get count in seconds

MOV AX,03H ;clear video buffer


INT 10H ;Video Interrupt

PUTS CRLF
PUTS CRLF

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 35

PUTS MSG2 ;print msg2

LOOP1:

MOV AH,02H ;set cursor position


MOV BH,0 ;page number
MOV DH,8 ;row
MOV DL,30 ;column
INT 10H ;Video interrupt

DISPLAY COUNT ;To display the Counter value

GET_TIME ;Retrieve System time


MOV SECOND,DH ;Move present second value to SECOND

LOOP2: ;Continuously Poll the system time


GET_TIME ;to get a delay of 1 second
CMP SECOND,DH
JZ LOOP2
DEC COUNT ;Decrement the counter Value

CMP COUNT,0 ;check whether counter value


JNE LOOP1 ;reached zero. else jump to loop1

MOV AH,02H ;set cursor position


MOV BH,0 ;page number
MOV DH,8 ;row
MOV DL,30 ;column
INT 10H ;Video interrupt

DISPLAY COUNT ;To display counter Value 0

PUTS CRLF
PUTS CRLF

.EXIT
END ;End of program

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 36

6. CALCULATOR

OBJECTIVE:
To write a program for a calculator which evaluates an arithmetic expression
involving addition, subtraction, multiplication and division?
ALGORITHM:
1. Start
2. Read the expression to array
3. Convert numbers to BCD
4. Check the numbers in expression
5. Is numbers?
then go to step 6
else print expression is incorrect and go to step 23
6. Is operator= +
then go to step 10
else go to step 7
7. Is operator= -
then go to step 12
else go to step 8
8. Is operator= *
then go to step 17
else go to step 9
9. Is operator= /
then go to step 19
else print not a valid operator and go to step 23
10. reg=array[2]+array[4]
11. go to step 21
12. Is array[2]<array[4]
then go to step 13
else go to step 15

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 37

13. reg=array[4]-array[2]
14. go to step 21
15. reg=array[2]-array[4]
16. go to step 21
17. reg=array[2]*array[4]
18. go to step 21
19. Is array[4]=0?
Then print result is infinity and goto step 23
else goto step 20
20. reg=array[2]/array[4]
21. process result and convert to ASCII
22. print the result
23. STOP

PROGRAM:

.MODEL SMALL ;initialise a small memory model


.STACK ;initialise a stack segment
.DATA ;initialise the data segment

MSG1 DB 'ENTER THE OPERATION : $'


MSG2 DB 'ENTER THE NUMBER : $'
MSG3 DB 'SORRY. NO SUCH OPERATION $'
MSG4 DB 'RESULT = $'
MSG5 DB ' CALCULATOR $'

CRLF DB 13,10,'$'

NUM1 DB 0
NUM2 DB 0
RESULT DW 0
TEN DW 10
ARR DB 10 DUP(?) ;declare an array of byte type

PUTS MACRO STR ;macro to print msg's

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 38

MOV AH,09H ;Write string to STDOUT


MOV DX,OFFSET STR
INT 21H ;call DOS interrupt
ENDM

GET_NUM MACRO ARG ;Macro for inputting a number


LOCAL L1,L2 ;declaration of loops of macro
MOV ARG,0
MOV BX,0

L1:

MOV AH,01H ;read character from STDIN


INT 21H ;DOS interrupt
CMP AL,13 ;check Enter key press
JE L2
SUB AL,48 ;convert ASCII to digit
MOV CL,AL ;move digit to CL
MOV AX,BX ;move BX to AX
MUL TEN ;AX=AX*10
MOV CH,0
ADD AX,CX ;AX=AX+CX
MOV BX,AX ;move AX to BX
JMP L1 ;jump to L1

L2:

MOV ARG,BL ;move BL to ARG


ENDM

DISPLAY MACRO ARG ;Macro to display number


LOCAL L3,L4 ;declaration of loops of macro
MOV SI,9
MOV ARR[SI],24H ;move $ to end of array
MOV AX,ARG ;set AX as the number to be printed
MOV DX,0 ;set remainder as zero

L3:

DIV TEN ;divide AX by 10


ADD DX,48 ;convert remainder to ASCII

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 39

DEC SI ;decrement array position


MOV ARR[SI],DL ;move remainder to array
MOV DX,0 ;set remainder as zero
CMP AX,0 ;check whether quotient reached 0
JNE L3

L4: ;Fill remaining array positions

DEC SI ;with blank spaces


MOV ARR[SI],20H
CMP SI,2
JNE L4 ;print ARR
PUTS ARR
ENDM

.CODE ;main program starts here


.STARTUP

MOV AX,03H ;clear video buffer


INT 10H ;call video services interrupt

PUTS CRLF
PUTS MSG5 ;prints msg5
PUTS CRLF
PUTS CRLF

PUTS MSG2 ;prints msg2


GET_NUM NUM1 ;get first number

PUTS CRLF
PUTS MSG1 ;prints msg1

MOV AH,01H ;read character from STDIN


INT 21H ;call DOS interrupt

CMP AL,43 ;check whether '+' is operation


JE ADDITION ;if yes jump to addition else continue

CMP AL,45 ;check whether '-' is operation

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 40

JE SUBTRACT ;if yes jump to subtract else continue

CMP AL,42 ;check whether '*' is operation


JE MULTIPLY ;if yes jump to multiply else continue

CMP AL,47 ;check whether '/' is operation


JE DIVISION ;if yes jump to division else continue

JMP SORRY ;jump to sorry as no such operation

ADDITION: ;addition operation

PUTS CRLF
PUTS MSG2 ;prints msg2
GET_NUM NUM2 ;get second number
MOV AL,NUM1 ;first number moved to AL
MOV AH,0 ;AH is set 0
ADD AL,NUM2 ;second number added with AL
MOV RESULT,AX ;AX is moved to RESULT
JMP PRINT ;jump to print

SUBTRACT: ;subtraction operation

PUTS CRLF
PUTS MSG2 ;prints msg2
GET_NUM NUM2 ;get second number
MOV AL,NUM1 ;first number moved to AL
MOV AH,0 ;AH is set 0
SUB AL,NUM2 ;second number subtracted from AL
MOV RESULT,AX ;AX is moved to RESULT
JMP PRINT ;jump to print

MULTIPLY: ;multiplication operation

PUTS CRLF
PUTS MSG2 ;prints msg2
GET_NUM NUM2 ;get second number
MOV AL,NUM1 ;first number moved to AL
MOV AH,0 ;AH is set 0
MUL NUM2 ;second number multiplied with AL

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 41

MOV RESULT,AX ;AX is moved to RESULT


JMP PRINT ;jump to print

DIVISION: ;Division operation

PUTS CRLF
PUTS MSG2 ;prints msg2
GET_NUM NUM2

CMP NUM2,0 ;check division by 0


JE SORRY

MOV AL,NUM1 ;first number moved to AL


MOV AH,0 ;AH is set 0
DIV NUM2 ;AX is divided by second number
MOV AH,0 ;AH is set 0
MOV RESULT,AX ;AX is moved to RESULT
JMP PRINT ;jump to print

SORRY: ;To print no operation

PUTS CRLF
PUTS MSG3 ;prints msg3
JMP STOP ;jump to stop

PRINT: ;print result

PUTS CRLF
PUTS MSG4 ;prints msg4
DISPLAY RESULT ;print the result on screen

STOP:

.EXIT
END ;end of program

OBSERVATION:
CALCULATOR

ENTER THE OPERATION : +

ENTER THE NUMBER :2,4

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 42

RESULT = 6

RESULT:
An 8086 MASM program is written for the implementation of a calculator and results
verified.

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 43

APPENDIX A
ASCII Table-I

Department of ECE, GEC Thrissur


Microprocessors and Microcontrollers Lab Manual 44

ASCII Table - II

Department of ECE, GEC Thrissur

Potrebbero piacerti anche