Professional Documents
Culture Documents
MPMC Lab Manual Cylcle 1
MPMC Lab Manual Cylcle 1
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 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
generate an assembly listing file, which has the complete assembly instructions with
mapped memory references.
In addition to above, PWB also take various command line options. Its command
line syntax is PWB [<options >][<files>] where <options> are
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.
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.
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.
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
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.
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)
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
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.
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.
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)
RESULT:-
Programmers Work Bench , Macro Assembler, DOS and BIOS interrupts are
familiarized.
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
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
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
PROGRAM:
REVERSE A STRING
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
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
CRLF DB 13,10,'$'
ARR DB 100 DUP(?)
.CODE
.STARTUP
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
CRLF DB 13,10,'$'
PUTS CRLF
PUTS CRLF
PUTS CRLF
PUTS CRLF
MOV SI,1
MOV CL,ARR2[SI] ;load CX register with string
MOV CH,0 ;length of second string
.EXIT
END ;end of program
CASE CONVERSION
CRLF DB 13,10,'$'
NEXT:
INC SI ;increment string position
LOOP LOWER_TO_UPPER ;loop till complete string is read
MOV SI,1
MOV CL,ARR[SI] ;load CX with string length
MOV CH,0
STOP:
.EXIT
END ;end of program
OBSERVATION:
REVERSE A STRING
PALINDROME CHECKING
CASE CONVERSION
RESULT:
3. NUMBER SORTING
OBJECTIVE:
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
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
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,','
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
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
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.
4. DRAW A SQUARE
OBJECTIVE:
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
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 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
INC CX ;increments CX by 1
CMP CX,HOR_END ;check whether horizondal end point
;reached
JNA HORIZONTAL ;jump to horizondal if not reached
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.
5. TIMER
OBJECTIVE:
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:
CRLF DB 13,10,'$'
TEN DW 10
COUNT DW 0
SECOND DB 0
ENDM
PUTS CRLF
PUTS MSG1 ;print msg1
PUTS CRLF
PUTS CRLF
LOOP1:
PUTS CRLF
PUTS CRLF
.EXIT
END ;End of program
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
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:
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
L1:
L2:
L3:
PUTS CRLF
PUTS MSG5 ;prints msg5
PUTS CRLF
PUTS CRLF
PUTS CRLF
PUTS MSG1 ;prints msg1
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
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
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
PUTS CRLF
PUTS MSG2 ;prints msg2
GET_NUM NUM2
PUTS CRLF
PUTS MSG3 ;prints msg3
JMP STOP ;jump to stop
PUTS CRLF
PUTS MSG4 ;prints msg4
DISPLAY RESULT ;print the result on screen
STOP:
.EXIT
END ;end of program
OBSERVATION:
CALCULATOR
RESULT = 6
RESULT:
An 8086 MASM program is written for the implementation of a calculator and results
verified.
APPENDIX A
ASCII Table-I
ASCII Table - II