Anda di halaman 1dari 61

Girisha G K, Asst Professor

1: PROGRAMS INVOLVING DATA TRANSFER INSTRUCTIONS


; 1:- PROGRAM 01a.
; FILE NAME: blktrawl.asm
; Program is tested and found OK
; //PROGRAM TO TRANSFER A BLOCK OF DATA FROM ONE MEMORY
; LOCATION TO ANOTHER MEMORY LOCATION WITH OVERLAP\\
.model small
.stack
.data
memloc1 db 02h, 04h, 06h, 08h, 0ah
memloc2 db 01h, 03h, 05h, 07h, 09h
.code

back:

mov ax, @data


mov ds, ax
mov cx, 05h
lea si, memloc1
lea di, memloc2
mov al, [si+4]
mov [di+2], al
dec si
dec di
loop back
mov ah, 4ch
int 21h

end

;// Intialize
;data segment\\
;intialize block counter
;load the offset address of source memory location into SI
;load the offset address of destination mem location into DI
;load the element pointed by SI+4 to AL
;transfer the content of AL to mem location pointed by DI+2
;decrement SI to point to next element of the block
;decrement Destination pointer
;if counter is not zero continue the transfer
;// exit to
; DOS\\
;end the program

EXAMPLE RESULT:
Before execution:
[????]: 02h, 04h, 06h, 08h, 0ah, 01h, 03h, 05h, 07h, 09h
After execution:
[????]: 02h, 04h, 06h, 02h, 04h, 06h, 08h, 0ah, 07h, 09h
OBSERVED RESULT:

Dept of ECE,

NMIT, Bangalore.

Girisha G K, Asst Professor


; 1:- PROGRAM 01b.
; FILE NAME: blktrawo.asm
; Program is tested and found OK
; //PROGRAM TO TRANSFER A BLOCK OF DATA FROM ONE MEMORY
; LOCATION TO ANOTHER MEMORY LOCATION WITHOUT OVERLAP\\
.model small
.stack
.data
memloc1 db 02h, 04h, 06h, 08h, 0ah
memloc2 db 5 dup (0)
.code

back:

mov ax, @data


mov ds, ax
mov cx, 05h
lea si, memloc1
lea di, memloc2
mov al, [si]
mov [di], al

;// intialize
;data segment\\
;intialize block counter
;load the offset address of src mem loc into SI
;load the offset address of dest mem loc into DI
;load the element pointed by SI to AL
;transfer the content of AL to mem loc pointed by

inc si
inc di
loop back
mov ah, 4ch
int 21h

;increment SI to point to next element of the block


;increment Destination pointer
;if counter is not zero continue the transfer
;// exit to
; DOS\\
;end the program

DI

end

EXAMPLE RESULT:
Before execution:
Memloc1: [????]: 02h, 04h, 06h, 08h, 0ah
Memloc2: [????]: 00h, 00h, 00h, 00h, 00h
After execution:
Memloc1: [????]: 02h, 04h, 06h, 08h, 0ah
Memloc2: [????]: 02h, 04h, 06h, 08h, 0ah
OBSERVED RESULT:

Dept of ECE,

NMIT, Bangalore.

Girisha G K, Asst Professor


; 1:- PROGRAM 02
; FILE NAME: blkint.asm
; Program is tested and found OK
; //PROGRAM TO INTERCHANGE A BLOCK OF DATA OF TWO MEMORY
; LOCATIONS\\
.model small
.stack
.data
memloc1 db 02h, 04h, 06h, 08h, 10h
memloc2 db 12h, 14h, 16h, 18h, 20h
.code

back:

mov ax, @data


mov ds, ax
mov cx, 05h
lea si, memloc1
lea di, memloc2
mov es, ax
mov bl, [si]
xchg bl, [di]
mov [si], bl
inc si
inc di
loop back
mov ah, 4ch
int 21h

;// Intialize
; the data segment\\
;intialize the counter
;effective address of mem loc1 in si
;effective address of mem loc1 in di
;transfer the contents of si into bl
;exchange the contents of di and bl
;load the contents of bl into si
;increment si
;increment di
;continue the transfer till count = 0
;// Terminate
; the program\\

end
EXAMPLE RESULT:
Before execution:
Memloc1: [????]: 02h, 04h, 06h, 08h, 10h
Memloc2: [????]:12h, 14h, 16h, 18h, 20h
After execution:
Memloc1: [????]:12h, 14h, 16h, 18h, 20h
Memloc2: [????]: 02h, 04h, 06h, 08h, 10h
OBSERVED RESULT:

Dept of ECE,

NMIT, Bangalore.

Girisha G K, Asst Professor

-2: PROGRAMS INVOLVING ARITHMETIC AND LOGICAL


OPERATIONS
; 2:- PROGRAM 01a
; FILE NAME: addbyte.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO ADD TWO 8-BIT DATA\\
.model small
.stack
.data
num1 db 33h
num2 db 44h
result dw (0)
.code
mov ax, @data
mov ds, ax
mov al, num1
add al, num2
mov result, al
mov ah, 4ch
int 21h

;//Intialize the
; data segment\\
;get first number in al
;add al(first number) with 2nd number
; store the sum in result loc
;// terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 01b
; FILE NAME: addword.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO ADD TWO 16-BIT NUMBERS\\
.model small
.stack
.data
num dw 1133h, 2244h
result dw 2 dup(0)
carry db (0)
.code
mov ax, @data
mov ds, ax
mov ax, num
clc
add ax, num+2
mov result, ax
adc carry, 00h
mov ah, 4ch
int 21h

;//Intialize the
; data segment\\
;get first num word in ax
;CF = 0
;add first num word with 2nd word
;save the sum in result
;save the carry if any
;// terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 01c
; FILE NAME: adddword.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO ADD TWO 32-BIT (MULTI-PRECISION)
; NUMBERS\\
.model small
.stack
.data
num1 dd 01234567h
num2 dd 89abcdefh
sum dd (0)
carry db (0)

;// two 32-bit numbers


; to be added\\

.code
mov ax, @data
mov ds, ax
mov ax, word ptr num1
mov bx, word ptr num2
clc
add ax, bx
mov bx, word ptr [num1+2]
mov cx, word ptr [num2+2]
adc bx, cx
mov word ptr [sum+2], bx
mov word ptr sum, ax
adc carry, 00h
mov ah, 4ch
int 21h

;//Intialize the
; data segment\\
;ax = LSBs of 1st num
;bx = LSBs of 2nd num
;ax = ax + bx
;bx = MSBs of 1st num
;cx = MSBs of 2nd num
;bx = bx + cx + CF
;// store the result
; in the memory\\
;save carry if any
;// Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 02a
; FILE NAME: subbyte.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO SUBTRACT TWO 8-BIT NUMBERS\\
.model small
.stack
.data
num1 db 77h
num2 db 88h
result db (0)
borrow db (0)

;//the two numbers


; to be added\\

.code
mov ax, @data
mov ds, ax
clc
mov al, num1
sub al, num2
mov result, al
sbb borrow,00h
mov ah, 4ch
int 21h

;//Intialize the
; data segment\\
;CF = 0
;1st num in al
;subtract 2nd num from 1st num
;store the result
;save borrow if any
;// terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 02b
; FILE NAME: subword.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO SUBTRACT TWO 16-BIT NUMBERS\\
.model small
.stack
.data
num dw 5555h, 4444h
result dw 2 dup(0)
borrow db (0)

;two words for subtraction

.code
mov ax, @data
mov ds, ax
clc
mov ax, num
sub ax, num+2
mov result, ax
sbb borrow, 00h
mov ah, 4ch
int 21h

;//Intialize
; the data segment\\
;minuend in ax
;subtract subtrahend from minuend
;store the result in memory
;save the borrow if any
;//Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 02c
; FILE NAME: subdword.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO SUBTRACT TWO 32-BIT NUMBERS\\
.model small
.stack
.data
num1 dd 89abcdefh
num2 dd 01234567h
result dd (0)
borrow db(0)

;minuend
;subtrahend

.code
mov ax, @data
mov ds, ax
mov ax, word ptr num1
mov bx, word ptr num2
clc
sub ax, bx
mov bx, word ptr num1+2
mov cx, word ptr num2+2
sbb bx, cx
mov word ptr result+2, bx
mov word ptr result, ax
sbb borrow, 00h
mov ah, 4ch
int 21h

;//Intialize
; the data segment\\
;LSBs of minuend
;LSBs of subtrahend
;CF = 0
;ax = ax - bx
;MSBs of minuend
;MSBs of subtrahend
;bx = bx - cx
;// store the
; result in memory locations\\
;save borrow if any
;//Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 03a
; FILE NAME: mulsign.asm
; Program is tested and found OK
; //ASSEMBLY LEVEL PROGRAM TO MULTIPLY SIGNED HEXADECIMAL
; NUMBERS\\
.model small
.stack
.data
num db -19h, -05h
result db ?
.code
mov ax, @data
mov ds, ax
mov al, num
mov bl, num+1
imul bl
mov result, al
mov ah, 4ch
int 21h
end

;two unsigned nos for multiplication


;//Intialize
; the data segment\\
;get the first no
;get the second no
;multiply two signed nos
;store the result in mem loc
;//Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

10

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 03b
; FILE NAME: mulusign.asm
; Program is tested and found OK
; //ASSEMBLY LEVEL PROGRAM TO MULTIPLY UNSIGNED HEXADECIMAL
; NUMBERS\\
.model small
.stack
.data
num db -19h, -05h
result db ?
.code
mov ax, @data
mov ds, ax
mov al, num
mov bl, num+1
mul bl
mov result, al
mov ah, 4ch
int 21h
end

;two unsigned nos for multiplication


;// Intialize
; the data segment\\
;get the first no
;get the second no
;multiply two unsigned nos
;store the result in mem loc
;//Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

11

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 04a
; FILE NAME: divsign.asm
; Program is tested and found OK
; //ASSEMBLY LEVEL PROGRAM TO DIVIDE TWO UNSIGNED
HEXADECIMAL
; NUMBERS\\
.model small
.stack
.data
dividend dw -19h
divisor db -05h
result db ?
.code
mov ax, @data
mov ds, ax
mov ax, dividend
mov bl, divisor
idiv bl
mov result, al
mov ah, 4ch
int 21h
end

;dividend
;divisor
;//Intialize
; the data segment\\
;get the first no
;get the second no
;divide 1st num by 2nd num
;store the result in mem loc
;//Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

12

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 04b
; FILE NAME: divusign.asm
; Program is tested and found OK
; //ASSEMBLY LEVEL PROGRAM TO DIVIDE TWO UNSIGNED
HEXADECIMAL
; NUMBERS\\
.model small
.stack
.data
dividend dw 19h
divisor db 05h
result db ?
.code
mov ax, @data
mov ds, ax
mov ax, dividend
mov bl, divisor
div bl
mov result, al
mov ah, 4ch
int 21h
end

;dividend
;divisor
;//Intialize
; the data segment\\
;get the first no
;get the second no
;divide 1st num by 2nd num
;store the result in mem loc
;//Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

13

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 05a
; FILE NAME: bcdtobin.asm
; Program is tested and found OK
; //PROGRAM TO CONVERT A BCD NUMBER TO BINARY NUMBER (HEX
; NUMBER)\\
.model small
.stack
.data
num db 67h
result db (0)
.code
mov ax, @data
mov ds, ax
mov al, num
mov cl, 04
and al, 0fh
mov bl, al
mov al, num
and al, 0f0h
rcr al, cl
mov dl, 0ah
mul dl
add al, bl
mov result, al
mov ah, 4ch
int 21h

;// Intialize
; the data segment\\
;get the num to be converted
;intialize rotation count
;mask higher number
;save ax in bx
;restore ax
;mask lower nibble
;rotate ax, cl times
;dl = 0ah
;al * 10
;al = 60h + 07h, --> 43h
;store the result
;// Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

14

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 05b
; FILE NAME:- bintobcd.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO CONVERT A BINARY (HEX) NUMBER TO BCD
; NUMBER\\
.model small
.stack
.data
num dw 0abch
.code
mov ax, @data
mov ds, ax
mov ax, num
mov bx, 0ah
mov cx, 00h
repeat: mov dx, 00h
div bx
push dx
inc cx
cmp ax, 00h
jnz repeat
skip:
pop dx
add dl, 30h
mov ah, 06h
int 21h
loop skip
mov ah, 4ch
int 21h
end

;number to be converted
;//Intialize
; the data segment\\
;ax = num
;bx = 10h
;cx = 00h ; clear counter
;dx = 00h
;ax = ax/bx
;save remainder
;increment counter
;test if quotient = 0
;if not divide again
;get remainder
;convert to ASCII
;display digit
;//Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

15

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 06
; FILE NAME: squcube.asm
; Program is tested and found OK
; //PROGRAM TO FIND THE SQUARE AND CUBE OF A GIVEN NUMBER\\
.model small
.stack
.data
num db 04h
squ db (0)
cube db (0)
.code
mov ax, @data
mov ds, ax
mov al, num
mul al
mov squ, al
mul num
mov cube, al
mov ah, 4ch
int 21h

;// Intialize
; the data segment\\
;al = 05h
;al = al * al
;squ = al = 10h
;al = al(squ) * num
;cube = al = 40h
;// Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

16

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 07
; FILE NAME: lcm.asm
; Program is tested and found OK
; ASSEMBLY LEVEL PROGRAM TO FIND THE LCM OF TWO NUMBERS

Wrong program
.model small
.stack
.data
num1 db 05h, 03h
lcm dw (0)

;// two numbers

.code

back:

exit:

mov ax, @data


mov ds, ax
mov ax, 00h
mov dx, 00h
mov bx, 00h
mov al, num1
mov bl, num1+1
push ax
div bx
cmp dx, 00h
je exit
pop ax
add al, num1
mov dx, 00
jmp back
pop lcm
mov ah, 4ch
int 21h

;// Intialize
; the data segment\\

;store num1 into al


;store num1+1 into bl
;divide ax by bx
;compare if remainder = 0
;if yes, ax is LCM otherwise not
;retrieve ax
;add ax with num1
;clear dx
;jump to back
;get the result (LCM)
;// Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

17

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 08
; FILE NAME:- gcd.asm
; Program is tested and found OK
; //PROGRAM TO FIND THE GCD OF TWO 16-BIT NUMBERS\\
.model small
.stack
.data
num1 dw 15
num2 dw 20
gcd dw ?

;two numbers

.code
mov ax, @data
mov ds, ax
mov bx, num1
mov cx, num2
cmp bx, cx
jb divide
xchg bx, cx
divide: mov dx, 00h
mov ax, cx
div bx
cmp dx, 00h
je over
mov cx, bx
mov bx, dx
jmp divide
over:
mov gcd, bx
mov ah, 4ch
int 21h
end

;//Intialize
; the data segment\\
;get num1 in ax
;get num2 in bx
;compare for the biggest no
;//if cx is big then proceed
; else exchange the nos\\
;//dividend/divisor
;//if rem = 0, the
; divisor is the GCD\\
;//else divisor = dividend
; divisor = remainder
;store the GCD in mem loc
;//Terminate
; the main program\\

OBSERVED RESULT:

Dept of ECE,

18

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 09
; FILE NAME:- factorl.asm
; Program is tested and found OK
; //PROGRAM TO FIND THE FACTORIAL OF A GIVEN NUMBER\\
.model small
.stack
.data
num dw 05h
msg db 'factorial of 0 is 1 $'
factorl dw (?)
.code
mov ax, @data
;//Intialize
mov ds, ax
; the data segment\\
mov bx, num
;get the number
cmp bx, 00h
;//factorial of zero
je disp
; is one\\
call factorial
;find factorial calling subroutine
mov factorl, ax
;store factorial in mem loc
jmp exit
disp:
mov ah, 09h
;//string display
lea dx, msg
; interrupt\\
int 21h
exit:
mov ah, 4ch
;//Terminate
int 21h
; the program\\
; //SUBROUTINE TO FIND FACTORIAL OF A GIVEN NUMBER\\
factorial proc near
;start of the procedure
cmp bx, 01h
;//factorial of
jbe return
; one is one\\
push bx
;save bx in stack
dec bx
;decrement bx
call factorial
;call the subroutine recursively
pop bx
;get back the value for bx
mul bx
;multiply the factors of a given num
ret
;return to the main prgm
factorial endp
;end of the procedure
return: mov ax, 01h
ret
end
OBSERVED RESULT:

Dept of ECE,

;// return the


; factorial for one\\

19

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 10a
; FILE NAME: aaa.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM USING AAA INSTRUCTION AFTER ADDITION\\
.model small
.stack
.data
num dw 38h, 37h
result dw ?
.code
mov ax, @data
mov ds, ax
mov ax, num
mov bx, num+2
add ax, bx
aaa
add ax, 3030h
mov result, ax
mov ah, 4ch
int 21h

;// Intialize
; the data segment\\
;ax = 38h
;bx = 37h
;ax = ax + bx = 6fh
;ah = 01h, al = 05h
;convert to ascii value
;ax = 3135h
;// terminate the
; program\\

end
RESULT:
Input: num = 0038h, num+2 = 0037h
Output: result = 35, result+1 = 31
OBSERVED RESULT:

Dept of ECE,

20

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 10b
; FILE NAME: aas.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM USING AAS INSTRUCTION AFTER ADDITION\\
.model small
.stack
.data
num dw 39h, 33h
result dw ?
.code
mov ax, @data
mov ds, ax
mov ax, num
mov bx, num+2
sub ax, bx
aas
add ax, 3030h
mov result, ax
mov ah, 4ch
int 21h

;// Intialize
; the data segment\\
;ax = 39h
;bx = 33h
;ax = ax - bx = 06h
;ah = 00h, al = 06h
;convert to ascii value
;ax = 3036h
;// terminate the
;program\\

end
RESULT:
Input: num = 0039h, num+2 = 0033h
Output: result = 00, result+1 = 36
OBSERVED RESULT:

Dept of ECE,

21

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 10c
; FILE NAME: aam.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM USING AAM INSTRUCTION AFTER ADDITION\\
.model small
.stack
.data
num db 06h, 08h
result dw ?
.code
mov ax, @data
mov ds, ax
mov al, num
mov bl, num+1
mul bl
aam
add ax, 3030h
mov result, ax
mov ah, 4ch
int 21h

;// Intialize
; the data segment\\
;al = 06h
;bl = 08h
;ax = al * bl = 30h
;ah = 04h, al = 08h
;convert to ascii value
;ax = 3438h
;// terminate the
;program\\

end

RESULT:
Input: num = 06h, num + 1 = 08h
Output: result = 38, result+1 = 34
OBSERVED RESULT:

Dept of ECE,

22

NMIT, Bangalore.

Girisha G K, Asst Professor


; 2:- PROGRAM 10d
; FILE NAME: aad.asm
; Program is tested and found OK
; //Assembly program using AAM instruction after addition\\
.model small
.stack
.data
num1 dw 0205h
num2 db 07h
result dw ?
.code
mov ax, @data
mov ds, ax
mov ax, num1
mov bh, num2
aad
div bh
mov result, ax
mov ah, 4ch
int 21h

;// Intialize
; the data segment\\
;ax = 0205h
;bh = 07h
;ax = 19h --> 25 decimal
;ax = 0403
;// terminate the
;program\\

end

RESULT:
Input: num1 = 0205h, num2 = 07h
Output: result = 03, result+1 = 04
OBSERVED RESULT:

Dept of ECE,

23

NMIT, Bangalore.

Girisha G K, Asst Professor

-3: PROGRAMS INVOLVING BIT MANIPULATION INSTRUCTIONS


; 3:- PROGRAM 01
; FILE NAME: posorneg.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO FIND THE GIVEN NUMBER IS POSITIVE OR
; NEGATIVE\\
.model small
.stack
.data
message1 db 'Number is Positive $'
message2 db 'Number is Negative $'
num db 0c0h
.code
mov ax, @data
mov ds, ax
mov al, num
and al, 80h
jnz negnum
mov ah, 09h
mov dx, offset message1
int 21h
jmp exit
negnum:mov ah, 09h
mov dx, offset message2
int 21h
exit:
mov ah, 4ch
int 21h
end

;//Intialize
; the data segment\\
;get the number in al to be checked
;retain only the MSB
;if ZF = 1, the number is negative
;//else the number is positive
; and display the message
; on console\\
;//display the negative number
; message
; on console\\
;// Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

24

NMIT, Bangalore.

Girisha G K, Asst Professor


; 3:- PROGRAM 02
; FILE NAME:oddorevn.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO FIND THE GIVEN NUMBER IS ODD OR EVEN\\
.model small
.stack
.data
message1 db 'Number is even $'
message2 db 'Number is odd $'
num db 40h
.code

even:
odd:

mov ax, @data


mov ds, ax
mov al, num
ror al,01h
jnc even
mov dx, offset message2
jmp odd
mov dx, offset message1
mov ah, 09h
int 21h
mov ah, 4ch
int 21h

;// Intialize
; the data segment\\
;al = num
;check for the LSB
;if CF = 0 --> even else odd number
;display message for odd
;display message for even
;// Terminate
;
the program\\

end
OBSERVED RESULT:

Dept of ECE,

25

NMIT, Bangalore.

Girisha G K, Asst Professor


; 3:- PROGRAM 03
; FILE NAME: onszro.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO FIND THE NUMBER OF ONES AND ZEROS IN A
; GIVEN NUMBER\\
.model small
.stack
.data
num db 40h
cnt_one db (0)
cnt_zero db (0)
.code
mov ax, @data
mov ds, ax
mov al, num
mov cx, 08h
repeat: rol al, 01h
jnc next
inc cnt_one
jmp skip
next:
inc cnt_zero
skip:
loop repeat
mov ah, 4ch
int 21h
end

;//Intialize
; the data segment\\
;al = 40h
;cx = count (no of bits in al)
;rotate each bit right
;if CF = 0, count as no of zeros
;else count as no of ones
;no of zeros
;repeat until all the bits are checked
;// Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

26

NMIT, Bangalore.

Girisha G K, Asst Professor


; 3:- PROGRAM 04
; FILE NAME: 2outof5.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO CHECK WHETHER A BYTE BELONGS TO 2 OUT
; OF 5 CODE\\
;** The code is valid if the number of ones in the least 5 bits are 2**
.model small
.stack
.data
data1 db 80h
cnt_one db (0)
message1 db 'valid 2 out of 5 code $'
message2 db 'invalid 2 out of 5 code $'
.code

back:
next:

skip:
end1:

mov ax, @data


mov ds, ax
mov al, data1
mov cx, 05h
ror al, 01h
jnc next
inc cnt_one
loop back
cmp cnt_one, 02h
jnz skip
mov ah, 09h
lea dx, message1
int 21h
jmp end1
lea dx, message2
mov ah, 09h
int 21h
mov ah, 4ch
int 21h

;//Intialize
; the data segment\\
;al = data1
;intialize the counter
;rotate right al bit by bit
;if CF = 0 jump to next
;if CF = 1, count no of ones
;repeat to count no of ones in the 1st 5-bits
;compare no of ones with 02h
;if no of ones is 2 then it is 2 out of 5 code
;//display the message
; as valid 2 out of 5 code\\
;//if no of ones is not 2 then
; display the message as
; invalid 2 out of 5 code\\
;// Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

27

NMIT, Bangalore.

Girisha G K, Asst Professor


; 3:- PROGRAM 05a
; FILE NAME: bitpali.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO FIND WHETHER A GIVEN BYTE IS A BITWISE
; PALINDROME OR NOT\\
.model small
.stack
.data
num db 18h
msg1 db 'number is a bitwise palindrome $'
msg2 db 'number is not a bitwise palindrome $'
.code
mov ax, @data
mov ds, ax
mov al, num
mov bl, al
mov cx, 08h
back:
ror al, 01h
jnc skip
rol bl, 01h
jc go_on
jmp term
skip:
rol bl, 01h
jc term
go_on: loop back
mov ah, 09h
lea dx, msg1
int 21h
jmp last
term: mov ah, 09h
lea dx, msg2
int 21h
last:
mov ah, 4ch
int 21h
end

;//Intialize
; the data segment\\
;get the number in al
;save a copy in bl
;intialze the counter to 8
;rotate al to right once
;if no carry jump to skip
;else rotate bl left once
;if carry jump to go_on
;else jump to term
;rotate bl left once
;if carrry jump to term
;else cx = cx-1 & if cx != 0, jump back
;// display msg1
; if given number is
;
a bitwise palindrome\\
;jump to last
;//display msg2
; if given number is
;
not a bitwise palindrome\\
;// Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

28

NMIT, Bangalore.

Girisha G K, Asst Professor


; 3:- PROGRAM 05b
; FILE NAME: nibpali.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO FIND WHETHER A GIVEN BYTE/WORD IS A
; NIBBLE WISE PALINDROME OR NOT\\
.model small
.stack
.data
num db 45h
msg1 db 'number is a nibble wise palindrome $'
msg2 db 'number is not a nibble wise palindrome $'
.code

succ:
last:

mov ax, @data


mov ds, ax
mov al, num
and al, 0fh
mov bl, al
mov al, num
and al, 0f0h
mov cl, 04h
rol al, cl
cmp al, bl
jz succ
mov dx, offset msg2
mov ah, 09h
int 21h
jmp last
lea dx, msg1
mov ah, 09h
int 21h
mov ah, 4ch
int 21h

;//Intialize
; the data segment\\
;al = num
;mask the higher nibble
;save al in bl
;again al = num
;mask the lower nibble
;intialize counter as 04
;//compare both lower
; and higher nibbles\\
;if both the nibbles are same jump to succ
;//else display the number
; is not a palindrome\\
;//display the message as
; given no is a palindrome\\
;//terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

29

NMIT, Bangalore.

Girisha G K, Asst Professor

-4: PROGRAMS INVOLVING BRANCH/LOOP INSTRUCTIONS

; 4:- PROGRAM 01a


; FILE NAME: addndata.asm
; Program is tested and found OK
; //ASSEMBLY LANGUAGE PROGRAM TO ADD N NUMBER OF DATA IN AN
; ARRAY\\
.model small
.stack
.data
array db 02h, 04h, 06h, 08h, 10h, 12h, 14h, 16h, 18h, 20h
ary_cnt equ 0ah
sum dw (0)
.code

back:

mov ax, @data


mov ds, ax
mov cx, ary_cnt
xor di, di
lea bx, array
mov al, [bx+di]
mov ah, 00h
add sum, ax
inc di
loop back
mov ah, 4ch
int 21h

;//Intialize
; the data segment\\
;intialize the counter
;clear di register
;bx = effective address of array
;al = first element of array
;clear higher byte of ax
;//add sum and array
; elements one by one\\
;add all the elements of the array
;// Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

30

NMIT, Bangalore.

Girisha G K, Asst Professor


; 4:- PROGRAM 01b
; FILE NAME: avgndata.asm
; Program is tested and found OK
; //ASSEMBLY LANGUAGE PROGRAM TO FIND AVERAGE OF N NUMBER
OF
; DATA IN AN ARRAY\\
.model small
.stack
.data
array db 02h, 04h, 06h, 08h, 10h, 12h, 14h, 16h, 18h, 20h
ary_cnt equ 0ah
sum dw (0)
avg db (0)
.code

back:

mov ax, @data


mov ds, ax
mov cx, ary_cnt
xor di, di
clc
lea bx, array
mov al, [bx+di]
mov ah, 00h
adc sum, ax
inc di
loop back
mov ax, sum
mov cl, ary_cnt
div cl
mov avg, al
mov ah, 4ch
int 21h

;//Intialize
; the data segment\\
;intialize the counter
;clear di
;CF = 0
;bx = effective address of array
;al = first element of array
;ah = 00h
;// add all the elements
; of the array
; and the result will be in sum\\
;ax = sum
;cl = no of elements in array
;al = ax/cl
;avg = al
;//Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

31

NMIT, Bangalore.

Girisha G K, Asst Professor


; 4:- PROGRAM 02a
; FILE NAME: lrgstnum.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO FIND THE LARGEST NUMBER IN THE ARRAY\\
.model small
.stack
.data
array db 10h, 06h, 04h, 08h, 12h
ary_cnt equ 05h
lrg_num db (0)
.code

back:

skip:

mov ax, @data


mov ds, ax
xor di, di
mov cx, ary_cnt
lea bx, array
mov al, lrg_num
cmp al, [bx+di]
jnc skip
mov dl, [bx+di]
mov al, dl
inc di
loop back
mov lrg_num, al
mov ah, 4ch
int 21h

;// Intialize
; the data segment\\
; di = 0
;cx = no of elements in the array
;bx = effective address of array
;assume lrgst no at present is 0
;compare lrge no to the first element
;// if CF= 0, the element is smaller
; else move large no into
; al using 3rd register\\
;repeat for all the elements of the array
;store largest no in mem loc
;//Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

32

NMIT, Bangalore.

Girisha G K, Asst Professor


; 4:- PROGRAM 02b
; FILE NAME: smlstnum.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO FIND THE SMALLEST NUMBER IN THE
ARRAY\\
.model small
.stack
.data
array db 02, 07, 06, 10, 05
ary_cnt equ 05h
sml_num db (0)
.code

back:

skip:

mov ax, @data


mov ds, ax
xor di, di
mov cx, ary_cnt
lea bx, array
mov al, [bx+di]
dec cx
inc di
cmp al, [bx+di]
jc skip
mov dl, [bx+di]
mov al, dl
inc di
loop back
mov sml_num, al
mov ah, 4ch
int 21h

;//Intialize
; the data segment\\
; di = 0
;cx = no of elements in array
;bx = effective address of array
;al <-- first element of array
;compare 1st and 2nd element of array
;//if al = small, skip
; else take the small no
; into al and then proceed
;
to check the next element\\
;store small number in mem loc
;//Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

33

NMIT, Bangalore.

Girisha G K, Asst Professor


; 4:- PROGRAM 03a
; FILE NAME: bubsorta.asm
; Program is tested and found OK
; //ASSEMBLY LANGUAGE PROGRAM TO SORT GIVEN DATA IN ASCENDING
; ORDER IN AN ARRAY USING BUBBLE SORT.\\
.model small
.stack
.data
array db 24h, 56h, 48h, 35h
ary_cnt equ 04
.code
mov ax, @data
mov ds, ax
mov bx, ary_cnt-1
nxtchk: mov cx, bx
mov si, 00h
nxtpas: mov al, array[si]
inc si
cmp al, array[si]
jbe nxtemt
xchg al, array[si]
mov array[si-1], al
nxtemt: loop nxtpas
dec bx
jnz nxtchk
mov ah, 4ch
int 21h
end

;//Intialize
; the data segment\\
;no of passes
;no of checks
; si = 0
;al = 1st element
;compare 1st and 2nd element
;//if below jump to
; nxtemt otherwise
; exchange the elements\\
;proceed with next pass
;dx = dx - 1
;continue with next check
;//Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

34

NMIT, Bangalore.

Girisha G K, Asst Professor


; 4:- PROGRAM 03b
; FILE NAME: bubsortd.asm
; Program is tested and found OK
; //ASSEMBLY LANGUAGE PROGRAM TO SORT GIVEN DATA IN
; DESCENDING ORDER IN AN ARRAY USING BUBBLE SORT.\\
.model small
.stack
.data
array db 24h, 56h, 48h, 35h
ary_cnt equ 04
.code
mov ax, @data
mov ds, ax
mov bx, ary_cnt-1
nxtchk: mov cx, bx
mov si, 00h
nxtpas: mov al, array[si]
inc si
cmp al, array[si]
jge nxtemt
xchg al, array[si]
mov array[si-1], al
nxtemt: loop nxtpas
dec bx
jnz nxtchk
mov ah, 4ch
int 21h
end

;//Intialize
; the data segment\\
;no of passes
;no of checks
; si = 0
;al = 1st element
;compare 1st and 2nd element
;//if greater jump to
; nxtemt otherwise
; exchange the elements\\
;proceed with next pass
;dx = dx - 1
;continue with next check
;//Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

35

NMIT, Bangalore.

Girisha G K, Asst Professor


; 4:- PROGRAM 04a
; FILE NAME: inssorta.asm
; Program is tested and found OK
; //ASSEMBLY LANGUAGE PROGRAM TO SORT GIVEN ARRAY IN
;
ASCENDING ORDER USING INSERTON SORT\\
.model small
.stack
.data
array dw 14h, 5h, 24h, 32h
count dw ($-array)/2
.code

loop1:

loop2:

loop3:

mov ax, @data


mov ds, ax
mov cx, 2
mov dx, cx
dec dx
mov si, dx
add si, si
mov ax, array[si]
cmp array[si-2], ax
jbe loop3
mov di, array[si-2]
mov array[si], di
dec si
dec si
dec dx
jnz loop2
mov array[si], ax
inc cx
cmp cx, count
jbe loop1
mov ah, 4ch
int 21h

;//Intialize
; the data segment\\
;cx = 2
;dx = cx
;dx = dx - 1
;si = dx
;si = si + si
;//compare the first
; and 2nd element\\
;if below leave as it is
;//else exchange the elements
;//decrement si
; two times\\

;//Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

36

NMIT, Bangalore.

Girisha G K, Asst Professor


; 4:- PROGRAM 04b
; FILE NAME: inssortd.asm
; Program is tested and found OK
; //ASSEMBLY LANGUAGE PROGRAM TO SORT GIVEN ARRAY IN
;
DESCENDING ORDER USING INSERTON SORT\\
.model small
.stack
.data
array dw 14h, 5h, 24h, 32h
count dw ($-array)/2
.code

loop1:

loop2:

loop3:

mov ax, @data


mov ds, ax
mov cx, 2
mov dx, cx
dec dx
mov si, dx
add si, si
mov ax, array[si]
cmp array[si-2], ax
jae loop3
mov di, array[si-2]
mov array[si], di
dec si
dec si
dec dx
jnz loop2
mov array[si], ax
inc cx
cmp cx, count
jbe loop1
mov ah, 4ch
int 21h

;//Intialize
; the data segment\\
;cx = 2
;dx = cx
;dx = dx - 1
;si = dx
;si = si + si
;//compare the first
; and 2nd element\\
;if above leave as it is
;//else exchange the elements
;//decrement si
; two times\\

;//Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

37

NMIT, Bangalore.

Girisha G K, Asst Professor

-5: PROGRAMS ON STRING MANIPULATION INSTRUCTIONS


; 5:- PROGRAM 01
; FILE NAME: strngtra.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO TRASFER A STRING FROM ONE LOCATION TO
; ANOTHER MEMORY LOCATION\\
.model small
.stack
.data
src db 'ELECTRONICS $'
dst db ?
str_cnt equ 0bh
.code
mov ax, @data
mov ds, ax
mov es, ax
mov cx, str_cnt
mov si, offset src
mov di, offset dst
cld
rep movsb

;//Intialize
; the data and
; extra segments\\
;intialize the counter for no of chars
;si = offset address of source string
;di = offset address of destination memory
;DF = 0; auto increment of si & di
;trnsfr the contents pointed by si to mem pointed by

mov ah, 4ch


int 21h

;//Terminate
; the program\\

di
end

OBSERVED RESULT:

Dept of ECE,

38

NMIT, Bangalore.

Girisha G K, Asst Professor


; 5:- PROGRAM 02
; FILE NAME: strngrev.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO REVERSE A GIVEN STRING\\
.model small
.stack
.data
string db 'COMMUNICATION $'
str_cnt equ 0ch
.code
mov ax, @data
mov ds, ax
mov cx, str_cnt
mov si, offset string
mov di, si
add di, str_cnt
repeat: cmp di, si
jbe exit
mov ah, [si]
xchg ah, [di]
mov [si], ah
dec di
inc si
jmp repeat
exit:
lea dx, string
mov ah, 09h
int 21h
mov ah, 4ch
int 21h
end

;//Intialize
; the data segment\\
;Intialize the array counter
;si = pointer to beginning of string
;//di = pointer to
; end of the string\\
;compare si & di
;if di <= si, exit
;// otherwise
; exchange the contents
;
of si & di\\
;decrement di
;increment si
;jump to repeat
;// display the
; reversed string\\
;// Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

39

NMIT, Bangalore.

Girisha G K, Asst Professor


; 5:- PROGRAM 03
; FILE NAME: chrsrch.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO SEARCH A CHARACTER IN A GIVEN STRING\\
.model small
.stack
.data
string db 'ELECTRONICS $'
str_cnt equ 0bh
msg1 db 'character found $'
msg2 db 'character not found $'
loc db ?
.code
mov ax, @data
mov ds, ax
mov es, ax
mov cx, str_cnt
lea di, string
mov al, 'T'
back: cmp al, byte ptr[di]
je found
inc di
loop back
lea dx, msg2
mov ah, 09h
int 21h
jmp last
found: mov ah, 09h
lea dx, msg1
int 21h
mov dx, str_cnt
sub dx, cx
mov loc, dx
last:
mov ah, 4ch
int 21h
end

;//Intialize
; data and
; extra segment\\
;intialize string counter
; di = offset of string
;al = character to be searched
;compare char with first element in the string
;if found jump to found
;//else proceed with
; comparing all chars in string\\
;// if not found display
;
msg2 using DOS services\\
;//if found display
; msg1 using DOS services\\
;// store the offset
; location of the desired
;
char in a string\\
;//Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

40

NMIT, Bangalore.

Girisha G K, Asst Professor


; 5:- PROGRAM 04
; FILE NAME: strpali.asm
; Program is tested and found OK
; //ASSEMBLY PROGRAM TO CHECK WHETHER A GIVEN STRING IS A
; PALINDROME OR NOT\\
.model small
.stack
.data
string db 'MALAYALAM $'
str_cnt equ 09h
msg1 db 'String is not a palindrome $'
msg2 db 'string is a palindrome $'
.code

back:

disp:

last:
exit:

mov ax, @data


mov ds, ax
mov es, ax
mov cx, str_cnt
dec cx
lea si, src
lea di, src
add si, cx
mov ah, [si]
mov al, [di]
cmp ah, al
jne last
dec si
inc di
cmp si, di
jbe disp
jmp back
lea dx, msg2
mov ah, 09h
int 21h
jmp exit
lea dx, msg1
mov ah, 09h
int 21h
mov ah, 4ch
int 21h

;//Intialize
; the data
; and extra segment\\
;intialize the counter
;decrement counter
;si = offset of source string
;source offset in di also
;si point to last location
;last char in ah
;first char in al
;//if they are not
; equal go to last\\
;//if they are equal
; decrement si and increment di\\
;//compare si & di if they are equal
; display msg2\\
;else continue to compare for nxt chars
;display msg2

;display msg1
;//Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

41

NMIT, Bangalore.

Girisha G K, Asst Professor

-6: PROGRAMS INVOLVING DOS SOFTWARE INTERRUPTS (INT


21H)

; 6:- PROGRAM 01
; FILE NAME: readkb.asm
; Program is tested and found OK
; // PROGRAM TO READ A CHARACTER FROM THE KEYBOARD\\
.model small
.stack
.data
.code
mov ax, @data
mov ds, ax
mov ah, 01h
int 21h
mov ah, 4ch
int 21h
end

;//Intialize
; the data segment\\
;//DOS function to get a
; character from keyboard\\
;//Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

42

NMIT, Bangalore.

Girisha G K, Asst Professor


; 6:- PROGRAM 02
; FILE NAME: readkbf.asm
; Program is tested and found OK
; // PROGRAM TO READ A CHARACTER FROM THE KEYBOARD WITH
; BUFFERED INPUT\\
.model small
.stack
.data
buff db 30
db 0
db 30 dup(0)
.code
mov ax, @data
mov ds, ax
mov ah, 0ah
lea dx, buff
int 21h
mov ah, 4ch
int 21h
end

;max length passed to the function


;actual length returned by function
;space for storing returned string from function
;//Intialize
; the data segment\\
;//DOS function to
; read string from the keyboard\\
;//Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

43

NMIT, Bangalore.

Girisha G K, Asst Professor


; 06:- PROGRAM 03
; FILE NAME: dispchr.asm
; Program is tested and found OK
; //PROGRAM TO DISPLAY A CHARACTER ON CONSOLE\\
.model small
.stack
.data
.code
mov ax, @data
mov ds, ax
mov ah, 02h
mov dl, 'E'
int 21h
mov ah, 4ch
int 21h
end

;//Intialize
; the data segment\\
;//call DOS service
; to display the char 'E'
; on console\\
;//Terminate
; the program\\

OBSERVED RESULT:

Dept of ECE,

44

NMIT, Bangalore.

Girisha G K, Asst Professor


; 06:- PROGRAM 04
; FILE NAME: dispstr.asm
; Program is tested and found OK
; //PROGRAM TO DISPLAY A STRING ON CONSOLE\\
.model small
.stack
.data
str db 'THURST TO LEARN$'
.code
mov ax, @data
mov ds, ax
lea dx, str
mov ah, 09h
int 21h
mov ah, 4ch
int 21h

;//Intialize
; the data segment\\
;dx = offset address of str
;//call DOS service to
; display the string\\
;//Terminate
; the program\\

end
OBSERVED RESULT:

Dept of ECE,

45

NMIT, Bangalore.

Girisha G K, Asst Professor


; 07:- PROGRAM 01
; FILE NAME: pcikey.asm
; Program is tested and found OK
; //PROGRAM TO DEMONSTRATE INTERFACING OF MATRIX KEYBOARD\\

Schematic Diagram of key board:

Dept of ECE,

46

NMIT, Bangalore.

Girisha G K, Asst Professor


.MODEL SMALL
.STACK 5000H

;Specify the model for the executable. Must for every program.

.DATA
; Any data declarations here.
Message1 DB 'DEMONSTRATION PROGRAM FOR KEYBOARD
INTERFACE',13,10,'$'
Message2 DB 'This program will display the key you pressed on the
Interface.',13,10,'$'
Message3 DB 'This program is running...',13,10,'Press any key to EXIT.',13,10,'$'
Message4 DB 13,'The Key You Pressed is : ','$'
Keys
DB '0 1 2 3 4 5 6 7 8 9 . + - X / % ACCECK= MCMRM M+','$'
Show
DB '01','$ '
CR EQU 0c003H
PA EQU 0c000H
PB EQU 0c001H
PC EQU 0c002H
.CODE

;Start your coding here.

MOV AX,@DATA
MOV DS, AX

; Initialize all segment registers as needed here.

MOV AH, 9h
; Display the message line1.
MOV DX, OFFSET Message1
INT 21h
MOV AH, 9h
; Display the message line2.
MOV DX, OFFSET Message2
INT 21h
MOV AH, 9h
; Display the message line3.
MOV DX, OFFSET Message3
INT 21h
MOV AX, 92h
MOV DX, CR
OUT DX, AX
GETKEY:
MOV BH, 1h
MOV BL, 00h
SCANLINES:
MOV AL, BH
MOV DX, PC
OUT DX, AL
MOV DX, PA
IN AL, DX
Dept of ECE,

; Initialize Port A - Input, CU & CL - Output


; Write to Control Register.
; Scan Lines
; Initialize a counter. It contains the no of the Key

; Send Line Number to Port CL


; Read from Port A

47

NMIT, Bangalore.

Girisha G K, Asst Professor


MOV CH, AL
MOV AL,00H
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

; CH has the value indicating the key pressed


; Initialize the counter
; Now repeatedly check which key was selected.
; CH is shifted to right
; If Equal Come out
; CH = CH >> 1
; All bits are compared
; Go back for next scan line
; Move to next scan line
; Repeat the SCAN Lines Loop (4 times)

DISPLAY:
; Display the selected key
MOV AH, 9h
; Display the message line3.
MOV DX, OFFSET Message4
INT 21h
MOV AX, 0000h
MOV AL, BL
MOV BL, 02h
MUL BL
MOV BX, AX
MOV DI, OFFSET Show
MOV AL, Keys[BX]
MOV Show[0h], AL
MOV AL, Keys[BX + 1h]
MOV Show[1h], AL
MOV AH, 9h
MOV DX, OFFSET Show
INT 21h
MOV CX, 0FFFFh

; Display the character pressed.

DELAY:
MOV AX, 0FFh
DELAY1: DEC AX
JNZ DELAY1
LOOP DELAY
LOOPOUT:
MOV AH, 01h
INT 16h
JZ GETKEY
Dept of ECE,

; Get the Key


; Check for the key
48

NMIT, Bangalore.

Girisha G K, Asst Professor

MOV AH, 4ch


INT 21h
END

Dept of ECE,

; Exit the program safely.


; This is the end of your program.

49

NMIT, Bangalore.

Girisha G K, Asst Professor


; 07:- PROGRAM 02
; FILE NAME: pcisev.asm
; Program is tested and found OK
; //PROGRAM TO DEMONSTRATE INTERFACING OF SEVEN SEGMENT
DISPLAY\\

Schematic Diagram of Seven Segment Display:

Dept of ECE,

50

NMIT, Bangalore.

Girisha G K, Asst Professor


.MODEL SMALL ;Specify the model for the executable. Must for every program.
.STACK 5000H
.DATA
;Any data declarations here.
Message1 DB 'DEMONSTRATION PROGRAM FOR SEVEN SEGMENT
DISPLAY',13,10,'$'
Message2 DB 'Check the Message < ELECTRO SYSTEMS > on Seven Segment
Display',13,10,'$'
Message3 DB 'This program is running...',13,10,'Press any key to EXIT.',13,10,'$'
DisplayData DB
0ffh,0ffh,0ffh,0ffh,0c6h,086h,0c7h,086h,0bfh,0c0h,0deh,087h,0bfh,092h,091h,092h,09
2h,0c8h,086h,087h
CR EQU 0c003H
PA EQU 0c000H
PB EQU 0c001H
PC EQU 0c002H
.CODE

;Start your coding here.

MOV AX,@DATA ;Initialize all segemnt regesters as needed here.


MOV DS,AX
MOV AH,9h
;Display the message line1.
MOV DX, OFFSET Message1
INT 21h
MOV AH,9h
;Display the message line2.
MOV DX, OFFSET Message2
INT 21h
MOV AH,9h
;Display the message line3.
MOV DX, OFFSET Message3
INT 21h
; Send the control word
MOV AL,80h
MOV DX,CR
OUT DX,AL
GETKEY:
MOV BX,0000h
LOOP1:
; Display the charecters 4 at a tine.
MOV AL,BL
AND AL,03h
CMP AL,00H
JNZ NO_DELAY
MOV CX,0FFFFh
DELAY : MOV AX,0FFFh
Dept of ECE,

51

NMIT, Bangalore.

Girisha G K, Asst Professor


DELAY1: DEC AX
JNZ DELAY1
LOOP DELAY
NO_DELAY:
MOV CL,00h
MOV CH,DisplayData[BX]
LOOP2:
MOV AH,01h
INT 16h
;Get the Key
JNZ END_PROGRAM
;MOV CL,01
MOV AH,CH
AND AH,80h
CMP AH,00h
JNZ CONTROL
MOV DX,PB
MOV AL,00h
OUT DX,AL
JMP END_CONTROL
CONTROL:
MOV DX,PB
MOV AL,01h
OUT DX,AL
END_CONTROL:
MOV DX,PC
MOV AL,01h
OUT DX,AL
MOV DX,PC
MOV AL,00h
OUT DX,AL
SHL CH,1
INC CL
CMP CL,08h
JNZ LOOP2 ;LOOP2 Repeats from here.
INC BX
CMP BX,20
JNZ LOOP1 ;LOOP1 Repeats from here.
MOV AH,01h
INT 16h
;Get the Key
JZ GETKEY
END_PROGRAM:
Dept of ECE,

52

NMIT, Bangalore.

Girisha G K, Asst Professor


MOV AH,4ch
; Exit the program safely.
INT 21h
END
;This is the end of your program.

Dept of ECE,

53

NMIT, Bangalore.

Girisha G K, Asst Professor


; 07:- PROGRAM 03
; FILE NAME: pcilogic.asm
; Program is tested and found OK
; //PROGRAM TO DEMONSTRATE INTERFACING OF LOGICAL CONTROLLER
INTERFACE\\

Schematic diagram of a Logic Controller:

Dept of ECE,

54

NMIT, Bangalore.

Girisha G K, Asst Professor


.MODEL TINY
.DATA
;;
;;
;;
;;
;;
;;

;Specify the model for the executable. Must for every program.

;Any data declarations here.

NOTE:
The follwing data declerations are common for every program.
The values of CR, PA, PB and PC will vary from PC to PC.
The user need to modify the values of CR, PA, PB, PC
for Assembling the program
CR EQU 0c003H
PA EQU 0c000H
PB EQU 0c001H
PC EQU 0c002H

Message1 DB 'DEMONSTRATION PROGRAM FOR LOGIC


CONTROLLER',13,10,'$'
Message2 DB 'This program will read input in Port B. and outputs',13,10,'$'
Message3 DB 'it's compliment in Port A.',13,10,13,10,'$'
Message4 DB 'This program is running...',13,10,'Press any key to EXIT.',13,10,'$'
.STACK
.CODE
; Start your coding here.
MOV AX,@DATA ; Initialize all segemnt regesters as needed here.
MOV DS, AX
MOV AH, 9h
; Display the message line1.
MOV DX, OFFSET Message1
INT 21h
MOV AH, 9h
; Display the message line2.
MOV DX, OFFSET Message2
INT 21h
MOV AH, 9h
; Display the message line3.
MOV DX, OFFSET Message3
INT 21h
MOV AH, 9h
; Display the message line4.
MOV DX, OFFSET Message4
INT 21h
MOV AL, 82H
MOV DX, CR
OUT DX, AL
GETKEY:
MOV DX, PB
IN AL, DX
Dept of ECE,

; Initialize A - Output B-Input Ports


; Write to Control Register.
; Get Data from Register B

55

NMIT, Bangalore.

Girisha G K, Asst Professor

NOT AL
MOV DX, PA
OUT DX, AL

; Compliment it.
; Put Data to Register A

MOV AH, 01h


INT 16h
;Get the Key
JZ GETKEY
MOV AH, 4ch
; Exit the program safely.
INT 21h
END
;This is the end of your program.

Dept of ECE,

56

NMIT, Bangalore.

Girisha G K, Asst Professor


; 07:- PROGRAM 04
; FILE NAME: pcistep.asm
; Program is tested and found OK
; //PROGRAM TO DEMONSTRATE INTERFACING OF STEPPER MOTOR
INTERFACE\\

Schematic Diagram of a Stepper Motor Interface:

Dept of ECE,

57

NMIT, Bangalore.

Girisha G K, Asst Professor


.MODEL TINY ;Specify the model for the executable. Must for every program.
.STACK
;100h
.DATA
;Any data declarations here.
;; NOTE:
;; The follwing data declerations are common for every program.
;; The values of CR, PA, PB and PC will vary from PC to PC.
;; The user need to modify the values of CR, PA, PB, PC
;; for Assembling the program
CR EQU 0c403H
PA EQU 0c400H
PB EQU 0c401H
PC EQU 0c402H
Message1 DB 'DEMONSTRATION PROGRAM FOR STEPPER
MOTOR',13,10,'$'
Message2 DB 'User following Keys for operating.',13,10,9,9,'C Clockwise.',13,10,9,9,'A - Anti-Clockwise.','$'
Message3 DB 13,10,'This program is running...',13,10,'Press ESC key to
EXIT.',13,10,'$'
Speed

DW 0FFH

.CODE

;Start your coding here.

CALL CLRSCR

;Clear Screen

MOV AX,@DATA ;Initialize all segemnt regesters as needed here.


MOV DS,AX
MOV AH, 9h
; Display the message line1.
MOV DX, OFFSET Message1
INT 21h
MOV AH, 9h
; Display the message line2.
MOV DX, OFFSET Message2
INT 21h
MOV AH, 9h
; Display the message line3.
MOV DX, OFFSET Message3
INT 21h
MOV DX, CR
MOV AL, 80h
OUT DX, AL
GETKEY:
MOV BL, AL
MOV AH, 01h
INT 16h
; Get the Key ZF = 1 (No Key), ZF = 0 (Key)
Dept of ECE,

58

NMIT, Bangalore.

Girisha G K, Asst Professor


MOV AL, BL
JZ COMPARE0 ; If No key pressed,
; Key was pressed, then get the key
MOV AH, 00h
INT 16h
MOV BL, AL
SUB BL, 1BH
; 27 -Esc Key
JZ EXIT_PROGRAM
COMPARE0:
CMP AL,'C'
; For Clockwise motion
JNZ COMPARE1
CALL CLOCK_WISE
JMP END_COMPARE
COMPARE1:
CMP AL,'A'
;For Anti-Clockwise motion
JNZ COMPARE2
CALL ANTI_CLOCK_WISE
JMP END_COMPARE
COMPARE2:
CMP AL,'c'
;For Anti-Clockwise motion
JNZ COMPARE3
CALL CLOCK_WISE
JMP END_COMPARE
COMPARE3:
CMP AL,'a'
;For Anti-Clockwise motion
JNZ END_COMPARE
CALL ANTI_CLOCK_WISE
JMP END_COMPARE
END_COMPARE:
JMP GETKEY
; If No valid key pressed,
EXIT_PROGRAM:
MOV AH, 4ch
INT 21h

; Exit the program safely.

; *********************************************************************
;
;
Procedures used by this program
;
; *********************************************************************
;
CLRSCR PROC
;Clears Display
PUSH AX
MOV AH,0Fh ;Get current mode
Dept of ECE,

59

NMIT, Bangalore.

Girisha G K, Asst Professor


INT 10h
MOV AH,00h ;Set to current mode
INT 10h
POP AX
RET
CLRSCR ENDP
ANTI_CLOCK_WISE PROC
PUSH AX
MOV AL, 11h
CALL OUT_A
CALL DELAY
MOV AL, 22h
CALL OUT_A
CALL DELAY
MOV AL, 44h
CALL OUT_A
CALL DELAY
MOV AL, 88h
CALL OUT_A
CALL DELAY
POP AX
RET
ANTI_CLOCK_WISE ENDP
CLOCK_WISE PROC
PUSH AX
MOV AL, 88h
CALL OUT_A
CALL DELAY
MOV AL,44h
CALL OUT_A
CALL DELAY
MOV AL,22h
CALL OUT_A
CALL DELAY
MOV AL,11h
CALL OUT_A
CALL DELAY
POP AX
RET
Dept of ECE,

60

NMIT, Bangalore.

Girisha G K, Asst Professor


CLOCK_WISE ENDP
OUT_A PROC
MOV DX,PA
OUT DX,AL
RET
OUT_A ENDP
DELAY PROC
MOV CX,01FFFh
LOOP1:
MOV AX,Speed
LOOP2: DEC AX
JNZ LOOP2
LOOP LOOP1
RET
DELAY ENDP
END

;This is the end of your program.

Dept of ECE,

61

NMIT, Bangalore.