Masm Program PDF
Masm Program PDF
Dept of CSE/ISE,
R EV A IN S TIT U T E O F T EC HN O LOGY
&
M A N AG EM EN T, Ba n g a lo re
IV Semester B.E.
(CSE/ISE)
Dept of CSE/ISE,
Books to be Referred:
1.
2.
3.
4.
Theory:
A Microprocessor is a programmable, digital logic device fabricated on a single
VLSI chip which can perform a set of arithmetic and logic operations as per the
instructions given by the user.
Any microprocessor has minimum three basic functional blocks: Arithmetic Logic
Unit (ALU), Timing & Control unit, Register array
The user writes his/her programs using English-like words (called mnemonics) and
is known as assembly language program (ALP).
A software called Assembler converts the user ALP into HEX/binary form (called
machine language) which is fed to the processor. The processor internally decodes
this binary code and performs the operation.
EXECUTION UNIT:
The execution unit consists of: General purpose (scratch pad) registers AX, BX, CX and DX;
Pointer registers SP (Stack Pointer) and BP (Base Pointer); index registers source index (SI) &
destination index (DI) registers; the Flag register, the ALU to perform operations and a control unit
with associated internal bus. The 16-bit scratch pad registers can be split into two 8-bit registers.
AX AL, AH ; BX BL, BH; CX CL, CH; DX DL, DH.
Dept of CSE/ISE,
Uses/Operations
Dept of CSE/ISE,
AH
BX
CX
CL
DX
AL
8086/8088 MP
IP
Instruction Pointer
CS
DS
SS
ES
AX
AH
AL
BX
BE
BL
CX
CE
CL
DX
DH
DL
SP
BP
SI
DI
SR
Status Register
MEMORY
00000016
FFFFF16
Dept of CSE/ISE,
BIT
15
14
13
12
11
10
OF
DF
IF
TF
SF
ZF
AF
PF
CF
U= UNDEFINED
(a)
(b)
(c)
(d)
(e)
(f)
(g)
(h)
(i)
(a)
(b)
(c)
(d)
(e)
(f)
(g)
(h)
(i)
Dept of CSE/ISE,
SEGMENT REGISTER
0000
ADDER
Programming Models:
Depending on the size of the memory the user program occupies, different types of assembly
language models are defined.
TINY
SMALL
MEDIUM
COMPACT
LARGE
Dept of CSE/ISE,
2. ASSEMBLER:
An assembler is a system software (program) used to translate the assembly language
mnemonics for instructions to the corresponding binary codes.
An assembler makes two passes thro your source code. On the first pass, it determines the
displacement of named data items, the offset of labels etc., and puts this information in a
symbol table. On the second pass, the assembler produces the binary code for each instruction
and inserts the offsets, etc., that is calculated during the first pass. The assembler checks for the
correct syntax in the assembly instructions and provides appropriate warning and error
messages. You have to open your file again using the editor to correct the errors and reassemble
it using assembler. Unless all the errors are corrected, the program cannot be executed in the
next step.
The assembler generates two files from the source file; the first file, called the object file having
an extension .obj which contains the binary codes for instructions and information about the
addresses of the instructions. The second file is called list file with an extension .lst. This
file contains the assembly language statements, the binary codes for each instruction, and the
offset for each inst. It also indicates any syntax errors or typing errors in the source program.
Note: The assembler generates only offsets (i.e., effective addresses); not absolute physical
addresses.
3. LINKER:
Its a program used to join several object files into one large object file. For large programs,
usually several modules are written and each module is tested and debugged. When all the
modules work, their object modules can be linked together to form a complete functioning
program.
The LINK program must be run on .obj file.
The linker produces a link file which contains the binary codes for all the combined modules.
The linker also produces a link map file which contains the address information about the linked
files.
The linker assigns only relative addresses starting from zero, so that this can be put anywhere in
physical primary memory later (by another program called locator or loader). Therefore, this
file is called relocatable. The linker produces link files with .exe extension.
Object modules of useful programs (like square root, factorial etc) can be kept in a library,
and linked to other programs when needed.
4. LOADER:
Its a program used to assign absolute physical addresses to the segments in the .exe file, in
the memory. IBM PC DOS environment comes with EXE2BIN loader program. The .exe
file is converted into .bin file.
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 7 of 56
Dept of CSE/ISE,
The physical addresses are assigned at run time by the loader. So, assembler does not know
about the segment starting addresses at the time program being assembled.
5. DEBUGGER:
If your program requires no external hardware, you can use a program called debugger to load
and run the .exe file.
A debugger is a program which allows you to load your object code program into system
memory, execute the program and troubleshoot or debug it. The debugger also allows you to
look at the contents of registers and memory locations after you run your program.
The debugger allows you to change the contents of registers & memory locations and rerun the
program. Also, if facilitates to set up breakpoints in your program, single step feature, and
other easy-to-use features.
If you are using a prototype SDK 86 board, the debugger is usually called monitor program.
We would be using the development tool MASM 5.0 or higher version from Microsoft Inc.
MASM stands for Microsoft Macro Assembler. Another assembler TASM (Turbo Assembler) from
Borland Inc., is also available.
D7
1 for I/O
D6
D5
PA mode:
00 mode 0, 01
mode1, 10/11 mode 2
D4
D3
D2
D1
D0
PA
direction
0 output
1 input
PCU
direction
0 output
1 input
PB mode
PB
direction
0 output
1 input
PCL
direction
0 output
1 input
0 mode 0
1 mode 1
Dept of CSE/ISE,
are no errors. Otherwise, it indicates the error with line numbers. You have to correct the errors by
opening your file with EDIT command and changing your instructions. Come back to DOS prompt
and again assemble your program using MASM command. This has to continue until MASM
displays 0 Severe Errors. There may still be Warning Errors. Try to correct them also.
4. Once you get the .obj file from step 3, you have to create the .exe file. At the prompt, type
the command LINK followed by filename.obj (say, test.obj) and press Enter key. (Note that you
have to give the extension now as .obj and not as .asm). If there are no linker errors, linker will
create .exe file of your program. Now, your program is ready to run.
5. There are two ways to run your program.
a) If your program accepts user inputs thro keyboard and displays the result on the screen, then
you can type the name of the file at the prompt and press Enter key. Appropriate messages will be
displayed.
b) If your program works with memory data and if you really want to know the contents of
registers, flags, memory locations assigned, opcodes etc., then type
prompt. Another window will be opened with your program, machine codes, register contents etc.,
Now, you also get a prompt > sign within CV window. Here you can use d command to display
memory contents, E command to enter data into memory and g command to execute your
program. Also, you can single step thro your program using the menu options. In many ways, CV
(Code View) is like Turbo C environment.
Once you are familiar with the architecture and basics of assembly language tools, you can start
typing and executing your program.
The programs with comments are listed for your reference. Write the programs in observation book.
2.
Create your own subdirectory in the computer. Edit (type) the programs with program number and
place them in your subdirectory. Have a copy of MASM.EXE, CV.EXE and LINK.EXE files in your
subdirectory. You can write comments for your instructions using Semicolon (;) symbol.
3.
Execute the programs as per the steps discussed earlier and note the results in your observation book.
4.
Make changes to the original program according to the questions given at the END of each program
and observe the outputs.
5.
For part A programs, input-output is through computer keyboard and monitor or through memory.
6.
For part B programs, you need an external interface board. Connect the board to the computer using
the FRC available. Some boards may require external power supply also.
7.
8.
The assembler is not case sensitive. However, we have used the following notation: uppercase letters
to indicate register names, mnemonics and assembler directives; lowercase letters to indicate variable
names, labels, segment names, and models.
Dept of CSE/ISE,
1a. Search a Key element in a list of N 16-bit numbers using binary search
algorithm
.model small
; memory model
.stack
; stack segment
.data
big:
MOV AX, BX
ADD AX, DX
SHR AX, 1
MOV SI, AX
ADD SI, SI
DEC AX
MOV DX, AX
JMP again
JE success
INC AX
MOV BX, AX
JMP again
Dept of CSE/ISE,
fail:
disp:
END
title
1b Read status of 8 input bits from the logic controller interface & display FF if
it is even parity bits otherwise display 00. Also display number of 1's in the
input data.
.model small
.stack
.data
pa EQU 0d400h
pb EQU 0d401h
pc EQU 0d402h
cr EQU 0d403h
cw EQU 82h
; main program
; the first two instructions mandatory for all programs.
MOV AX, @data
MOV DS, AX
MOV DX, cr
MOV AL, cw
OUT DX, AL
; Initialization of 8255
MOV DX, pb
IN AL, DX
Dept of CSE/ISE,
OR AL, AL
MOV BL, AL
JPO oddp
; switch status in BL
MOV DX, pa
MOV AL, 0ffh
OUT DX, AL
; Parity Even
; FF sent to Logic Controller
JMP count
oddp:
MOV DX, pa
MOV AL, 00h
OUT DX, AL
JMP count
Dept of CSE/ISE,
INCLUDE readch.mac
; include the file readch.mac
INCLUDE dispch.mac
; include the file dispch.mac
.model small
.stack
; optional declaration
.data
arr DB 40 DUP (?)
; declaring an array to store 40 bytes
msg1 DB 10,13, "Enter the String : $"
msg2 DB 13,10, "The Entered String is : $"
.code
; main program
; label for the first instruction is optional.
back:
MOV SI, 0
read arr [SI]
INC SI
CMP AL, 13
JNZ back
Dept of CSE/ISE,
2b Perform the BCD up/down (00-99-00) counter and ring counter operations
; using Logic Controller
.model small
.stack
.data
pa EQU 0d400h
pb EQU 0d401h
pc EQU 0d402h
cr EQU 0d403h
cw EQU 82h
.code
; main program
MOV AX, @data
MOV DS, AX
MOV DX, cr
MOV AL, cw
OUT DX, AL
Dept of CSE/ISE,
ring:
MOV AL, BH
CALL disp
ROR BH, 1
JMP again
;decrementing count
; change result to decimal after subtraction
Dept of CSE/ISE,
Exercise questions:
1. Modify prob 2a to accept a string ending with $ sign.
2. Modify prob 2a with only CR or LF values and observe the output.
3. Modify prob 2b to have only two options: UP/Down or Ring counter
4. Modify prob 2b delay procedure for different delays by varying count value.
5. Modify prob 2b for HEX up/down counter and shift left ring counter.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Title
.model small
.stack
.data
list DB 33h, 54h, 0a2h, 17h, 76h
n DW $-list
order EQU 0
; main program
MOV AX, @data
MOV DS, AX
MOV BX, n
DEC BX
Dept of CSE/ISE,
; inner loop
;outer loop
; sorting is over
; display the message
Dept of CSE/ISE,
ADD AH, 7
ADD AH, 30h
MOV DL, AH
MOV AH, 02
INT 21 h
RET
asciidisp ENDP
skip:
; terminate
END
Title
3b) read the status of two 8-bits inputs (x & y) from the logic controller
; interface and display x * y.
.model small
.stack
.data
pa EQU 0d400h
pb EQU 0d401h
pc EQU 0d402h
cr EQU 0d403h
cw EQU 82h
msg1 DB 10,13, "enter number x from the interface and press Enter :$"
msg2 DB 10, 13, "enter number y from the interface and press Enter :$"
msg3 DB 10, 13, "Product is displayed on the interface in binary form: press any key to exit$"
.code
; main program
;start label is optional
;display message
MOV DX, pb
; read first number (switch status) through port B of 8255
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 18 of 56
Dept of CSE/ISE,
;display message
;terminate
; delay procedure
PUSH CX
PUSH AX
MOV CX, 2000h
back1: MOV AX, 0ffffh
back2: DEC AX
JNZ back2
LOOP back1
POP AX
POP CX
RET
delay
ENDP
END start
Exercise questions:
1. Modify prob 3a for a set of N 16-bit numbers.
2. Modify prob 3b to obtain the product in decimal and display it.
3. Name different sorting algorithms.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 19 of 56
Dept of CSE/ISE,
title 7a) Read your name from the keyboard and display it in a specified location
; on the screen in front of the message "what is your name". Clear the screen
; before display.
.model small
.stack
readstr MACRO loc
MOV AH, 01H
INT 21H
MOV loc, AL
ENDM
clrscr MACRO
MOV AL, 2
MOV AH, 0
INT 10H
ENDM
.data
msg0 DB 10, ' ENTER THE NAME:$'
msg1 DB
' WHAT IS YOUR NAME? $'
msg2 DB 10, '$'
;insert line feed
len DW ($-msg1)
arr DB 40 DUP(?)
display MACRO str
LEA DX, str
MOV AH,9
INT 21H
ENDM
.code
; main program
; SI is array pointer
display msg0
Dept of CSE/ISE,
MOV BH, 0
MOV DH, 13
MOV DL, 28
MOV AH, 2
INT 10H
display msg1
MOV SI, 0
LEA DX, arr[SI]
MOV AH, 09H
INT 21H
display msg2
7B) Drive a stepper motor interface to rotate the motor in clockwise direction
; by N steps
.model small
.stack
.data
pa EQU
pb EQU
pc EQU
cr EQU
0d400h
0d401h
0d402h
0d403h
cw EQU 80h
n EQU
50
PHASE_A EQU
88H
.code
start: MOV DX, cr
MOV AL, cw
OUT DX, AL
MOV CX, n
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 21 of 56
Dept of CSE/ISE,
; clockwise rotation
PROC
NEAR
MOV SI, 1000h
back2:
MOV DI, 0FFFH
back1:
DEC DI
JNZ back1
DEC SI
JNZ back2
RET
delay ENDP
; delay procedure
END start
Exercise questions:
1. Modify prob 7a to display the name character-by-character.
2. Write a code to clear the screen.
3. Modify prob 7b to rotate motor (i) 2 rotations (ii) 5 rotations
4. Modify prob 7b delay counts and observe the speed of the motor.
5. Write a note on DOS interrupts.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
title
.model small
.stack
.data
num DW 5
res DW ?
Dept of CSE/ISE,
; main program
CMP num, 0
JE last
; if number is 0, factorial is 1
; else compute the factorial
display msg
PROC NEAR
MUL num
DEC NUM
JZ over
CALL fact
over:
RET
fact ENDP
unpack
PROC NEAR
MOV BX, AX
AND AH, 0F0H
MOV AL, AH
MOV CL, 4
Dept of CSE/ISE,
unpack ENDP
asciidisp PROC NEAR
CMP AL, 0AH
JB skip
ADD AL, 7
skip: ADD AL, 30H
MOV DL, AL
MOV AH, 02
INT 21H
RET
asciidisp ENDP
END
_______________________________________________________
title
.model small
.stack
.data
pa EQU
pb EQU
pc EQU
cr EQU
0d400h
0d401h
0d402h
0d403h
cw EQU 80h
n EQU
50
PHASE_A EQU
88H
Dept of CSE/ISE,
; main program
9a) COMPUTE nCr USING RECURSION PROCEDURE. ASSUME THAT 'n' AND 'r' ARE
; NON NEGATIVE INTEGER NUMBERS.
.model small
.stack
.data
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 25 of 56
Dept of CSE/ISE,
; value of n
; value of r
; to store the result
; main program
start: MOV AX, @data
MOV DS, AX
CMP n, 0
JZ error
MOV BX, n
INC BX
MOV CX, r
CALL ncp
; BX has value of n
display msg
ncp
; CX has value of r
; transfer control to procedure
MOV AH,4CH
INT 21H
PROC
NEAR
CMP CX, 00H
JE over
PUSH CX
DEC CX
CALL NCP
MOV AX,BX
POP CX
SUB AX,CX
MUL NCR
DIV CX
MOV NCR,AX
Dept of CSE/ISE,
over: RET
ncp ENDP
unpack PROC NEAR
MOV BX, AX
AND AH, 0F0H
MOV AL, AH
MOV CL, 4
SHR AL, CL
CALL disp
MOV AX, BX
AND AH, 0FH
MOV AL, AH
CALL disp
MOV AX, BX
AND AL, 0F0H
SHR AL, CL
CALL disp
MOV AX, BX
AND AL, 0FH
CALL disp
RET
unpack ENDP
disp
PROC NEAR
CMP AL, 0AH
JB skip
ADD AL, 7
skip: ADD AL, 30H
MOV DL, AL
MOV AH, 02
INT 21H
RET
disp ENDP
END start
_____________________________________________
title
9B) Drive a stepper motor interface to rotate the motor N steps clockwise
; and N steps in anti-clockwise direction
.model small
.stack
.data
pa EQU 0d400h
pb EQU 0d401h
Dept of CSE/ISE,
.code
start: MOV DX, cr
MOV AL, cw
OUT DX, AL
MOV CX, n1
clockw: MOV BL, 4
MOV AL, PHASE_A
up1:
MOV DX, pa
OUT DX, AL
CALL delay
ROR AL, 1
DEC BL
JNZ up1
LOOP clockw
MOV CX, n2
MOV BL, 4
MOV AL, PHASE_D
up2:
MOV DX, Pa
OUT DX, AL
CALL delay
ROL AL, 1
DEC BL
JNZ up2
LOOP anticlk
MOV AH, 4CH
INT 21H
delay PROC NEAR
MOV SI, 1000h
back2:
MOV DI, 0FFFH
back1:
DEC DI
JNZ back1
DEC SI
JNZ back2
RET
delay
ENDP
END start
anticlk:
Dept of CSE/ISE,
Exercise questions:
1. Modify prob 9a with another logic (mathematically) for finding ncr.
2. Modify prob 9a to check r > n and if yes, print an error condition.
3. Modify prob 9b to rotate the motor either clockwise or anti-clockwise depending on the
key pressed from keyboard.
4. What are the uses of Stepper motor?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
title
10a) Find whether a given Sub- string is present or not in a main string of
; characters.
.model small
.stack
disp_msg MACRO str
; macro to display string on screen
LEA DX, str
; using DOS interrupts
MOV AH, 09h
INT 21H
ENDM
read MACRO str
LEA DX, str
MOV AH, 0AH
INT 21H
ENDM
.data
msg1
msg2
msg3
msg4
DB
DB
DB
DB
10,
10,
10,
10,
13,
13,
13,
13,
z DB 50H
DB 0H
DB 50H DUP (?)
y DB 50H
DB 0H
DB 50H DUP (?)
.code
start: MOV AX, @data
MOV DS, AX
disp_msg msg1
read z
disp_msg msg2
read y
Dept of CSE/ISE,
stop:
MOV AH,4CH
INT 21H
END start
_____________________________________________________
title
10b) Scan a 8 X 3 keypad for key closure and to store the code of the key
; pressed in memory location or display on the screen. Display row and column
; numbers of the key pressed.
.model small
.stack
clrscr MACRO
MOV AL, 2
MOV AH, 0
INT 10h
ENDM
DB
DB
DB
DB
DB
DB
Dept of CSE/ISE,
pa EQU 0D400h
pb EQU 0D401h
pc EQU 0D402h
cr EQU 0D403h
.code
start: MOV AX, @data
MOV DS, AX
clrscr
mdisp msg1
mdisp msg2
mdisp msg3
MOV AX, 90h
MOV DX, cr
OUT DX, AX
Dept of CSE/ISE,
getkey:
MOV BH, 1h
MOV BL, 0h
;Scan Lines
;Initialize a counter. It contains the no of the Key
scanlines:
MOV AL, BH
OUT_pc
in_pa
MOV CH, AL
MOV AL, 0H
check:
MOV CL, CH
AND CL, 01h
CMP CL, 01h
JZ display
INC BL
SHR CH, 01h
INC AL
CMP AL, 08h
JNZ check
SHL BH, 01h
CMP BH, 10h
JNZ scanlines
JMP loopout
display:
PUSH AX
mdisp msg5
MOV AL, BH
cdisp
mdisp msg6
POP AX
cdisp
mdisp msg4
MOV AX, 0h
MOV AL, BL
MOV BL, 02h
MUL BL
MOV BX, AX
MOV DI, OFFSET Show
MOV AL, Keys[BX]
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 32 of 56
Dept of CSE/ISE,
PROC NEAR
MOV CX, 0FFFFh
back2: MOV AX, 0FFh
back1: DEC AX
JNZ back1
LOOP back2
RET
delay
ENDP
; delay procedure
END start
; outer loop
; inner loop
Exercise questions:
1. Modify prob 10a to print the length of the main string and substring.
2. Modify prob 10b to display row number starting from 0.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
title
.model small
.stack
.data
arr DW 0,1,50 DUP (?) ; array to store Fib numbers
arrdec DW 0,1, 50 DUP (?) ; array to store Decimal fib numbers
count DW 15
; how many numbers to generate
.code
start: MOV AX, @data
MOV DS, AX
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 33 of 56
Dept of CSE/ISE,
MOV SI, 0
MOV CX, count
; generating HEX fib numbers
back: MOV AX, arr[SI]
; take the first number from memory
ADD AX, arr[SI+2]
; ADD it to the second number
MOV arr[SI+4], AX
; store the sum in next location
ADD SI, 2
; increment pointer
LOOP back
; repeat until count is over
; Hex result stored in memory
; for generating Decimal Fib numbers use the following code and
use memory location arrdec.
;
LEA DI, arrdec
;
MOV CX, count
;
XOR AX, AX
;back: MOV AL, BYTE PTR [DI]
;
ADD AL, BYTE PTR [DI+2]
;
DAA
;
MOV BYTE PTR [DI+4], AL
;
MOV AL, BYTE PTR [DI+1]
;
ADC AL, BYTE PTR [DI+3]
;
DAA
;
MOV BYTE PTR [DI+5], AL
;
ADD DI, 2
;
LOOP back
MOV AH, 4Ch
INT 21h
END start
_____________________________________________________________-TITLE
11b)
.model small
.stack
clrscr MACRO
MOV AL, 3
MOV AH, 0
INT 10h
ENDM
ENDM
cdisp
Dept of CSE/ISE,
MACRO str
LEA DX, str
MOV AH, 9
INT 21H
MACRO
MOV DL, AL
MOV AH, 02h
INT 21h
ENDM
.data
pa
pb
pc
cr
EQU
EQU
EQU
EQU
msg1
msg2
msg3
msg4
DB
DB
DB
DB
0d400h
0d401h
0d402h
0d403h
10,13,
10,13,
10,13,
10,13,
disp1 DB '0123456789.+-*/%c$'
inp DB 4
.code
MOV AX, @data
MOV DS, AX
MOV DX, cr
MOV AL, 90h
OUT DX, AL
mdisp msg2
INC DI
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 35 of 56
Dept of CSE/ISE,
PUSH AX
MOV AH, 07h
INT 21h
POP AX
mdisp msg3
INC DI
CALL delay
CALL delay
CALL keypress
PUSH AX
MOV AH, 07h
INT 21h
POP AX
mdisp msg4
MOV AL, [DI-2]
SUB AL, 30h
MOV BL, [DI-1]
SUB BL, 30h
MOV DL, [DI]
CMP DL, '+'
JNZ subt
ADD AL, BL
DAA
JMP exit
subt: SUB AL, BL
DAS
exit: MOV DL, AL
AND AL, 0f0h
MOV CL, 04h
SHR AL, CL
ADD AL, 30h
PUSH DX
cdisp
POP DX
MOV AL, DL
AND AL, 0fh
ADD AL, 30h
cdisp
; invoke macro
; invoke macro
Dept of CSE/ISE,
repeat:
;delay procedure
Dept of CSE/ISE,
JNZ back1
LOOP back2
POP CX
POP AX
RET
ENDP
delay
END start
Exercise questions:
1. Modify prob 11a to display the Fib numbers on the screen.
2. Modify prob 11b to include Multiplication operation also.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
title 15a (i) Program to Create a file using DOS interrupts.
.model small
.stack
Disp MACRO str
LEA DX, str
MOV AH, 09h
INT 21h
ENDM
.data
filen DB 'd:\mpa_09\test.txt $'
msg1 DB 'Creation successful $'
msg2 DB 'Creation Fails $'
.code
MOV AX, @data
MOV DS, AX
MOV AH, 3ch
MOV CX, 00h
LEA DX, filen
INT 21h
JC error
disp msg1
JMP stop
Dept of CSE/ISE,
MOV AH,4ch
INT 21h
END start
Title
.model small
.stack
disp MACRO str
LEA DX, str
MOV AH, 09h
INT 21h
ENDM
.data
filen DB 'd:\mpa_09\test.txt'
msg1 DB 10,13, ' file deleted successfully . $'
msg2 DB 10, 13, ' !!!! file not found !!!$'
.code
MOV AX, @data
MOV DS, AX
LEA DX, filen
MOV CX, 20h
MOV AH, 41h
INT 21h
JC fail
disp msg1
JMP next
fail: disp msg2
next: MOV AH, 4CH
INT 21H
END
_____________________________________________
title
15b) Drive an Elevator Interface
; Initially, Elevator is in Ground floor, with all requests in OFF state. When a
; request is made, the Elevator moves to that floor and stays there until further
; requests.
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 39 of 56
Dept of CSE/ISE,
; floor numbers
; code to clear the request LED
.code
MOV AX, @data
MOV DS, AX
MOV DX, cr
MOV AL, 82H
OUT DX, AL
XOR AX, AX
back1: MOV AL, AH
OR AL, 0F0H
MOV DX, pa
OUT DX, AL
MOV DX, pb
back2: MOV CH, AH
; point to port B
; initially AH =0
; initially, elevator in grd floor
delay
PROC
NEAR
Dept of CSE/ISE,
; delay procedure
PUSH CX
PUSH AX
MOV CX, 04fffh
back3: MOV AX, 02fffh
back4: DEC AX
JNZ back4
LOOP back3
POP AX
POP CX
RET
delay ENDP
END
Exercise questions:
1. Modify prob 15a to display the present working directory
2. Modify prob 15b to move the Elevator to Ground floor after all the requests are serviced.
Dept of CSE/ISE,
12a) Read the current time from system and display it in a standard format on
; the screen.
.model small
.stack
.data
msg1 DB 10,13, " @@@ Reading system Time :::$"
msg2 DB 10, 13, ' The system time is >> $'
clrscr MACRO
MOV AL, 2
MOV AH, 0
INT 10H
ENDM
dispm MACRO str
LEA DX, str
MOV AH, 9H
INT 21H
ENDM
set_cursor MACRO
MOV BL, 0
MOV AL, 3
MOV DH, 15
MOV DL, 20
MOV AH, 2
INT 10H
ENDM
.code
;invoke macros
;Hours in CH register
; unpack the digits
; use a procedure to convert to ASCII and display on screen
; the format is hh:mm:ss
Dept of CSE/ISE,
display
MOV AL, CL
AAM
MOV BX, AX
CALL display
MOV DL, ':'
MOV AH, 02h
INT 21h
; minutes in CL register
MOV AL, DH
AAM
MOV BX,AX
CALL display
MOV AH,4ch
INT 21h
; seconds in DH register
PROC NEAR
MOV DL, BH
ADD DL, 30h
MOV AH, 02h
INT 21h
MOV DL, BL
ADD DL, 30h
MOV AH, 02h
INT 21h
RET
display ENDP
END
______________________________________________________title
12b) Generate a sine wave using the dac interface (the output of the dac is to
; be displayed on a CRO).
.model small
.data
porta EQU
portb EQU
portc EQU
cwr
EQU
0d400h
0d401h
0d402h
0d403h
sines DB 00,11,22,33,43,53,63,72,81,89,97,104,109,115,119,122,125,126,127
; array to store values of sin
msg DB 10, 13, ' Observe Sine wave on CRO; Press any key to exit', 10, 13, '$'
.code
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 43 of 56
Dept of CSE/ISE,
second_quart:
MOV AL, 7FH
MOV BL, byte ptr [SI]
ADD AL, BL
OUT DX, AL
DEC SI
LOOP second_quart
MOV SI, offset sines
MOV CX, 13h
third_quart:
MOV AL, 7fh
MOV BL, byte ptr [SI]
SUB AL, BL
OUT DX, AL
INC SI
LOOP third_quart
DEC SI
MOV CX, 12h
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 44 of 56
Dept of CSE/ISE,
fourth_quart:
MOV AL, 7Fh
MOV BL, BYTE PTR [SI]
SUB AL, BL
OUT DX, AL
DEC SI
LOOP fourth_quart
MOV AH, 1
INT 16H
JNZ stop
JMP full_wave
.model small
.stack
.data
msg DB
clrscr
MACRO
MOV AL, 2
MOV AH, 0
INT 10H
ENDM
dispm
MACRO str
LEA DX, str
MOV AH, 9H
INT 21H
ENDM
.code
MOV AX, @data
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 45 of 56
Dept of CSE/ISE,
; save value of AL
;BL contains second digit
; display second (lower) digit
; increment second digit
; inner loop
; display all second digit (0-9)
MOV DL, 0
MOV AH, 2
INT 10h
MOV AX, SI
INC AL
CMP AL, 39h
JLE again
MOV AH, 4Ch
INT 21h
delay PROC
NEAR
PUSH CX
PUSH AX
MOV CX, 1000H
back2: MOV AX, 04FFFh
back1: DEC AX
JNZ back1
LOOP back2
POP AX
; delay procedure
Dept of CSE/ISE,
POP CX
RET
delay
ENDP
END
_____________________________________________________
title
13b) Generate a half rectified sine wave form using the DAC interface
; output of the DAC is to be displayed on a CRO).
(the
.model
.data
sines DB 00,22,44,66,87,108,127,146,164,180,195,209,221,231,240,246,251,254,255
msg DB 10,13, 10, 'Observe Half Rectified wave on CRO. Press any key to exit $'
porta
portb
portc
ctrl
EQU
EQU
EQU
EQU
0d400h
0d401h
0d402h
0d403h
.stack
.code
MOV AX, @data
MOV DS, AX
LEA DX, msg
MOV AH, 9
INT 21H
MOV AL, 80h
MOV DX, ctrl
OUT DX, AL
CALL delay
half_wave:
MOV DX, porta
MOV CX, 13h
MOV SI, OFFSET sines
first_quart:
MOV AL, BYTE PTR [SI]
OUT DX, AL
CALL delay
INC SI
LOOP first_quart
DEC SI
MOV CX, 12H
second_quart:
MOV AL, BYTE PTR [SI]
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 47 of 56
Dept of CSE/ISE,
OUT DX, AL
CALL delay
DEC SI
LOOP second_quart
MOV CX, 25H
no_wave:
MOV AL, 00h
OUT DX, AL
CALL delay
LOOP no_wave
MOV AH, 1
INT 16H
JNZ stop
JMP half_wave
stop:
14a) Read a pair of input co-ordinates in BCD and move cursor to specified
; position on screen.
.model small
.stack
.data
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 48 of 56
Dept of CSE/ISE,
DB ?
MACRO
MOV AH, 0
MOV AL, 3
INT 10h
ENDM
dispm MACRO str
MOV DX, OFFSET str
MOV AH,9H
INT 21H
ENDM
.code
; using a procedure
; X coordinate value stored
MOV DL,'*'
MOV AH, 02h
INT 21h
MOV AH, 1H
INT 21H
MOV AH, 4Ch
INT 21h
Dept of CSE/ISE,
END
___________________________________________________________
title
14b) Generate a fully rectified sine wave form using the DAC interface
; (the output of the DAC is to be displayed on a CRO).
.model small
.data
porta EQU
portb EQU
portc EQU
cwr
EQU
0d400h
0d401h
0d402h
0d403h
sines DB 00,11,22,33,43,53,63,72,81,89,97,104,109,115,119,122,125,126,127
; array to store values of sin
msg DB 10, 13, ' Observe Full Rectifier Sine wave on CRO; Press any key to exit', 10, 13, '$'
.code
MOV AX, @data
MOV DS, AX
MOV DX, cwr
MOV AL, 80h
OUT DX, AL
LEA DX, msg
MOV AH, 9H
INT 21H
MOV DX, porta
Dept of CSE/ISE,
fullrec_wave:
MOV SI, OFFSET sines
MOV CX, 13h
; the full rectified sinewave will have first two quadrants repeated continuously.
first_quart:
MOV AL, 7FH
MOV BL, BYTE PTR [si]
; take sine value from array
ADD AL, BL
OUT DX, AL
; and send it to port (CRO)
INC SI
LOOP first_quart
MOV CX, 12H
DEC SI
second_quart:
MOV AL, 7FH
MOV BL, BYTE PTR [SI]
ADD AL, BL
OUT DX, AL
DEC SI
LOOP second_quart
MOV AH, 1
INT 16H
JNZ stop
JMP fullrec_wave
.model small
.stack
.data
msg1 DB 10, 'ENTER A KEY FROM KEYBOARD',10,13,'$'
msg2 DB
'The ASCII value is: $'
-------------------------------------------------------------------Compiled by: L. Krishnananda, Asst Professor, Dept of ISE
Page 51 of 56
Dept of CSE/ISE,
;Video mode = 3
;To clear the screen
.code
MOV AX, @data
MOV DS, AX
;Initialize DS
clrscr
dispm msg1
; invoke macro
; invoke macro to DISPLAY MSG
MOV AH, 1
INT 21H
MOV BL, AL
MOV BH, 0
MOV DH, 12
MOV DL, 40
MOV AH, 2
INT 10H
dispm msg2
MOV AL, BL
AND AL, 0F0H
MOV CL, 4
SHR AL, CL
CALL DISP
MOV AL, BL
AND AL, 0FH
CALL disp
MOV AH, 4CH
INT 21H
disp: CMP AL, 0AH
JB SKIP
ADD AL, 7
SKIP: ADD AL, 30H
MOV DL, AL
MOV AH, 02
;Store it in BL
;set cursor position using BIOS function
;page 0
;row=12 central row
;col=40 central col
Dept of CSE/ISE,
INT 21H
RET
END
Exercise questions:
1. Modify prob 4a to display the ASCII value at any position on the screen
2. Modify prob 4b to display messages PASS and FAIL alternately on a 7-segment display
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
title
.model small
.stack
.data
buf DB 60
DB ?
DB 60 DUP(?)
revbuf DB 60 DUP (?)
MACRO str
LEA DX, str
MOV AH, 9H
INT 21H
ENDM
clrscr MACRO
MOV AL, 2
MOV AH, 0
INT 10H
ENDM
.code
MOV AX, @data
MOV DS, AX
MOV ES, AX
clrscr
dispm msg
LEA DX, buf
MOV AH, 0AH
INT 21H
; invoke macro
; read a string from keyboard
Dept of CSE/ISE,
MOV CH, 0
MOV CL, buf+1
ADD SI, CX
back: MOV AL, BYTE PTR [SI]
MOV BYTE PTR [DI], AL
INC DI
DEC SI
LOOP back
CLD
LEA SI, buf+2
LEA DI, revbuf
MOV CL, SIZE buf+2
repe CMPSB
JNZ noteq
dispm msg1
JMP stop
noteq: dispm msg2
6a) read two strings from keyboard and store them in locations.
; check whether they are equal or not and display appropriate messages.
; also display the length of the strings
.model small
.stack
.data
str1 DB 150
DB ?
DB 150 dup(?)
str2 DB 150
DB ?
DB 150 dup(?)
msg1 DB 10,10,13, '
; Messages
DB
DB
DB
DB
DB
Dept of CSE/ISE,
10,10,13, '
Strings Not Equal !!!!! $'
10,13, ' Enter string1 (upto 9 characters): $'
10,13, ' Enter string2 (upto 9 characters): $'
10,13, ' Length of string1 = $'
10,13, ' Length of string2 = $'
clrscr MACRO
MOV AL, 2
MOV AH,0
INT 10H
ENDM
dispm
MACRO str
LEA DX, str
MOV AH, 09h
INT 21h
ENDM
.code
MOV AX,@data
MOV DS, AX
MOV ES, AX
clrscr
dispm msg3
MOV DX, OFFSET str1
MOV AH, 0ah
INT 21h
dispm msg4
MOV DX, OFFSET str2
MOV AH, 0AH
INT 21h
dispm msg5
MOV DL, str1[1]
ADD DL, 30H
MOV AH, 2
INT 21H
dispm msg6
MOV DL, str2[1]
ADD DL, 30H
MOV AH, 2
INT 21H
MOV AL, str1[1]
CMP AL, str2[1]
Dept of CSE/ISE,