Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 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
mov ax, @data ;// Intialize
mov ds, ax ;data segment\\
mov cx, 05h ;intialize block counter
lea si, memloc1 ;load the offset address of source memory location into SI
lea di, memloc2 ;load the offset address of destination mem location into DI
back: mov al, [si+4] ;load the element pointed by SI+4 to AL
mov [di+2], al ;transfer the content of AL to mem location pointed by DI+2
dec si ;decrement SI to point to next element of the block
dec di ;decrement Destination pointer
loop back ;if counter is not zero continue the transfer
mov ah, 4ch ;// exit to
int 21h ; DOS\\
end ;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, 1 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
mov ax, @data ;// intialize
mov ds, ax ;data segment\\
mov cx, 05h ;intialize block counter
lea si, memloc1 ;load the offset address of src mem loc into SI
lea di, memloc2 ;load the offset address of dest mem loc into DI
back: mov al, [si] ;load the element pointed by SI to AL
mov [di], al ;transfer the content of AL to mem loc pointed by
DI
inc si ;increment SI to point to next element of the block
inc di ;increment Destination pointer
loop back ;if counter is not zero continue the transfer
mov ah, 4ch ;// exit to
int 21h ; DOS\\
end ;end the program

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, 2 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
mov ax, @data ;// Intialize
mov ds, ax ; the data segment\\
mov cx, 05h ;intialize the counter
lea si, memloc1 ;effective address of mem loc1 in si
lea di, memloc2 ;effective address of mem loc1 in di
mov es, ax
back: mov bl, [si] ;transfer the contents of si into bl
xchg bl, [di] ;exchange the contents of di and bl
mov [si], bl ;load the contents of bl into si
inc si ;increment si
inc di ;increment di
loop back ;continue the transfer till count = 0
mov ah, 4ch ;// Terminate
int 21h ; 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, 3 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 ;//Intialize the
mov ds, ax ; data segment\\
mov al, num1 ;get first number in al
add al, num2 ;add al(first number) with 2nd number
mov result, al ; store the sum in result loc
mov ah, 4ch ;// terminate
int 21h ; the program\\
end

OBSERVED RESULT:

Dept of ECE, 4 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 ;//Intialize the
mov ds, ax ; data segment\\
mov ax, num ;get first num word in ax
clc ;CF = 0
add ax, num+2 ;add first num word with 2nd word
mov result, ax ;save the sum in result
adc carry, 00h ;save the carry if any
mov ah, 4ch ;// terminate
int 21h ; the program\\
end

OBSERVED RESULT:

Dept of ECE, 5 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 ;// two 32-bit numbers
num2 dd 89abcdefh ; to be added\\
sum dd (0)
carry db (0)

.code
mov ax, @data ;//Intialize the
mov ds, ax ; data segment\\
mov ax, word ptr num1 ;ax = LSBs of 1st num
mov bx, word ptr num2 ;bx = LSBs of 2nd num
clc
add ax, bx ;ax = ax + bx
mov bx, word ptr [num1+2] ;bx = MSBs of 1st num
mov cx, word ptr [num2+2] ;cx = MSBs of 2nd num
adc bx, cx ;bx = bx + cx + CF
mov word ptr [sum+2], bx ;// store the result
mov word ptr sum, ax ; in the memory\\
adc carry, 00h ;save carry if any
mov ah, 4ch ;// Terminate
int 21h ; the program\\
end

OBSERVED RESULT:

Dept of ECE, 6 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 ;//the two numbers
num2 db 88h ; to be added\\
result db (0)
borrow db (0)

.code
mov ax, @data ;//Intialize the
mov ds, ax ; data segment\\
clc ;CF = 0
mov al, num1 ;1st num in al
sub al, num2 ;subtract 2nd num from 1st num
mov result, al ;store the result
sbb borrow,00h ;save borrow if any
mov ah, 4ch ;// terminate
int 21h ; the program\\
end

OBSERVED RESULT:

Dept of ECE, 7 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 ;two words for subtraction
result dw 2 dup(0)
borrow db (0)

.code
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
clc
mov ax, num ;minuend in ax
sub ax, num+2 ;subtract subtrahend from minuend
mov result, ax ;store the result in memory
sbb borrow, 00h ;save the borrow if any
mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

OBSERVED RESULT:

Dept of ECE, 8 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 ;minuend
num2 dd 01234567h ;subtrahend
result dd (0)
borrow db(0)

.code
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov ax, word ptr num1 ;LSBs of minuend
mov bx, word ptr num2 ;LSBs of subtrahend
clc ;CF = 0
sub ax, bx ;ax = ax - bx
mov bx, word ptr num1+2 ;MSBs of minuend
mov cx, word ptr num2+2 ;MSBs of subtrahend
sbb bx, cx ;bx = bx - cx
mov word ptr result+2, bx ;// store the
mov word ptr result, ax ; result in memory locations\\
sbb borrow, 00h ;save borrow if any
mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

OBSERVED RESULT:

Dept of ECE, 9 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 ;two unsigned nos for multiplication
result db ?
.code
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov al, num ;get the first no
mov bl, num+1 ;get the second no
imul bl ;multiply two signed nos
mov result, al ;store the result in mem loc
mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

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 ;two unsigned nos for multiplication
result db ?
.code
mov ax, @data ;// Intialize
mov ds, ax ; the data segment\\
mov al, num ;get the first no
mov bl, num+1 ;get the second no
mul bl ;multiply two unsigned nos
mov result, al ;store the result in mem loc
mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

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 ;dividend
divisor db -05h ;divisor
result db ?
.code
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov ax, dividend ;get the first no
mov bl, divisor ;get the second no
idiv bl ;divide 1st num by 2nd num
mov result, al ;store the result in mem loc
mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

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 ;dividend
divisor db 05h ;divisor
result db ?
.code
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov ax, dividend ;get the first no
mov bl, divisor ;get the second no
div bl ;divide 1st num by 2nd num
mov result, al ;store the result in mem loc
mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

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 ;// Intialize
mov ds, ax ; the data segment\\
mov al, num ;get the num to be converted
mov cl, 04 ;intialize rotation count
and al, 0fh ;mask higher number
mov bl, al ;save ax in bx
mov al, num ;restore ax
and al, 0f0h ;mask lower nibble
rcr al, cl ;rotate ax, cl times
mov dl, 0ah ;dl = 0ah
mul dl ;al * 10
add al, bl ;al = 60h + 07h, --> 43h
mov result, al ;store the result
mov ah, 4ch ;// Terminate
int 21h ; 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 ;number to be converted
.code
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov ax, num ;ax = num
mov bx, 0ah ;bx = 10h
mov cx, 00h ;cx = 00h ; clear counter
repeat: mov dx, 00h ;dx = 00h
div bx ;ax = ax/bx
push dx ;save remainder
inc cx ;increment counter
cmp ax, 00h ;test if quotient = 0
jnz repeat ;if not divide again
skip: pop dx ;get remainder
add dl, 30h ;convert to ASCII
mov ah, 06h ;display digit
int 21h
loop skip
mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

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 ;// Intialize
mov ds, ax ; the data segment\\
mov al, num ;al = 05h
mul al ;al = al * al
mov squ, al ;squ = al = 10h
mul num ;al = al(squ) * num
mov cube, al ;cube = al = 40h
mov ah, 4ch ;// Terminate
int 21h ; 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 ;// two numbers
lcm dw (0)

.code
mov ax, @data ;// Intialize
mov ds, ax ; the data segment\\
mov ax, 00h
mov dx, 00h
mov bx, 00h
mov al, num1 ;store num1 into al
mov bl, num1+1 ;store num1+1 into bl
back: push ax
div bx ;divide ax by bx
cmp dx, 00h ;compare if remainder = 0
je exit ;if yes, ax is LCM otherwise not
pop ax ;retrieve ax
add al, num1 ;add ax with num1
mov dx, 00 ;clear dx
jmp back ;jump to back
exit: pop lcm ;get the result (LCM)
mov ah, 4ch ;// Terminate
int 21h ; 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 ;two numbers
num2 dw 20
gcd dw ?

.code
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov bx, num1 ;get num1 in ax
mov cx, num2 ;get num2 in bx
cmp bx, cx ;compare for the biggest no
jb divide ;//if cx is big then proceed
xchg bx, cx ; else exchange the nos\\
divide: mov dx, 00h
mov ax, cx
div bx ;//dividend/divisor
cmp dx, 00h ;//if rem = 0, the
je over ; divisor is the GCD\\
mov cx, bx ;//else divisor = dividend
mov bx, dx ; divisor = remainder
jmp divide
over: mov gcd, bx ;store the GCD in mem loc
mov ah, 4ch ;//Terminate
int 21h ; the main program\\
end

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 ;// return the


ret ; factorial for one\\
end
OBSERVED RESULT:

Dept of ECE, 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 ;// Intialize
mov ds, ax ; the data segment\\
mov ax, num ;ax = 38h
mov bx, num+2 ;bx = 37h
add ax, bx ;ax = ax + bx = 6fh
aaa ;ah = 01h, al = 05h
add ax, 3030h ;convert to ascii value
mov result, ax ;ax = 3135h
mov ah, 4ch ;// terminate the
int 21h ; 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 ;// Intialize
mov ds, ax ; the data segment\\
mov ax, num ;ax = 39h
mov bx, num+2 ;bx = 33h
sub ax, bx ;ax = ax - bx = 06h
aas ;ah = 00h, al = 06h
add ax, 3030h ;convert to ascii value
mov result, ax ;ax = 3036h
mov ah, 4ch ;// terminate the
int 21h ;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 ;// Intialize
mov ds, ax ; the data segment\\
mov al, num ;al = 06h
mov bl, num+1 ;bl = 08h
mul bl ;ax = al * bl = 30h
aam ;ah = 04h, al = 08h
add ax, 3030h ;convert to ascii value
mov result, ax ;ax = 3438h
mov ah, 4ch ;// terminate the
int 21h ;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 ;// Intialize
mov ds, ax ; the data segment\\
mov ax, num1 ;ax = 0205h
mov bh, num2 ;bh = 07h
aad ;ax = 19h --> 25 decimal
div bh ;ax = 0403
mov result, ax
mov ah, 4ch ;// terminate the
int 21h ;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 ;//Intialize
mov ds, ax ; the data segment\\
mov al, num ;get the number in al to be checked
and al, 80h ;retain only the MSB
jnz negnum ;if ZF = 1, the number is negative
mov ah, 09h ;//else the number is positive
mov dx, offset message1 ; and display the message
int 21h ; on console\\
jmp exit
negnum:mov ah, 09h ;//display the negative number
mov dx, offset message2 ; message
int 21h ; on console\\
exit: mov ah, 4ch ;// Terminate
int 21h ; the program\\
end

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
mov ax, @data ;// Intialize
mov ds, ax ; the data segment\\
mov al, num ;al = num
ror al,01h ;check for the LSB
jnc even ;if CF = 0 --> even else odd number
mov dx, offset message2 ;display message for odd
jmp odd
even: mov dx, offset message1 ;display message for even
odd: mov ah, 09h
int 21h
mov ah, 4ch ;// Terminate
int 21h ; 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 ;//Intialize
mov ds, ax ; the data segment\\
mov al, num ;al = 40h
mov cx, 08h ;cx = count (no of bits in al)
repeat: rol al, 01h ;rotate each bit right
jnc next ;if CF = 0, count as no of zeros
inc cnt_one ;else count as no of ones
jmp skip
next: inc cnt_zero ;no of zeros
skip: loop repeat ;repeat until all the bits are checked
mov ah, 4ch ;// Terminate
int 21h ; the program\\
end

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
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov al, data1 ;al = data1
mov cx, 05h ;intialize the counter
back: ror al, 01h ;rotate right al bit by bit
jnc next ;if CF = 0 jump to next
inc cnt_one ;if CF = 1, count no of ones
next: loop back ;repeat to count no of ones in the 1st 5-bits
cmp cnt_one, 02h ;compare no of ones with 02h
jnz skip ;if no of ones is 2 then it is 2 out of 5 code
mov ah, 09h ;//display the message
lea dx, message1 ; as valid 2 out of 5 code\\
int 21h
jmp end1
skip: lea dx, message2 ;//if no of ones is not 2 then
mov ah, 09h ; display the message as
int 21h ; invalid 2 out of 5 code\\
end1: mov ah, 4ch ;// Terminate
int 21h ; 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 ;//Intialize
mov ds, ax ; the data segment\\
mov al, num ;get the number in al
mov bl, al ;save a copy in bl
mov cx, 08h ;intialze the counter to 8
back: ror al, 01h ;rotate al to right once
jnc skip ;if no carry jump to skip
rol bl, 01h ;else rotate bl left once
jc go_on ;if carry jump to go_on
jmp term ;else jump to term
skip: rol bl, 01h ;rotate bl left once
jc term ;if carrry jump to term
go_on: loop back ;else cx = cx-1 & if cx != 0, jump back
mov ah, 09h ;// display msg1
lea dx, msg1 ; if given number is
int 21h ; a bitwise palindrome\\
jmp last ;jump to last
term: mov ah, 09h ;//display msg2
lea dx, msg2 ; if given number is
int 21h ; not a bitwise palindrome\\
last: mov ah, 4ch ;// Terminate
int 21h ; the program\\
end

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
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov al, num ;al = num
and al, 0fh ;mask the higher nibble
mov bl, al ;save al in bl
mov al, num ;again al = num
and al, 0f0h ;mask the lower nibble
mov cl, 04h ;intialize counter as 04
rol al, cl ;//compare both lower
cmp al, bl ; and higher nibbles\\
jz succ ;if both the nibbles are same jump to succ
mov dx, offset msg2 ;//else display the number
mov ah, 09h ; is not a palindrome\\
int 21h
jmp last
succ: lea dx, msg1 ;//display the message as
mov ah, 09h ; given no is a palindrome\\
int 21h
last: mov ah, 4ch ;//terminate
int 21h ; 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
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov cx, ary_cnt ;intialize the counter
xor di, di ;clear di register
lea bx, array ;bx = effective address of array
back: mov al, [bx+di] ;al = first element of array
mov ah, 00h ;clear higher byte of ax
add sum, ax ;//add sum and array
inc di ; elements one by one\\
loop back ;add all the elements of the array
mov ah, 4ch ;// Terminate
int 21h ; 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
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov cx, ary_cnt ;intialize the counter
xor di, di ;clear di
clc ;CF = 0
lea bx, array ;bx = effective address of array
back: mov al, [bx+di] ;al = first element of array
mov ah, 00h ;ah = 00h
adc sum, ax ;// add all the elements
inc di ; of the array
loop back ; and the result will be in sum\\
mov ax, sum ;ax = sum
mov cl, ary_cnt ;cl = no of elements in array
div cl ;al = ax/cl
mov avg, al ;avg = al
mov ah, 4ch ;//Terminate
int 21h ; 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
mov ax, @data ;// Intialize
mov ds, ax ; the data segment\\
xor di, di ; di = 0
mov cx, ary_cnt ;cx = no of elements in the array
lea bx, array ;bx = effective address of array
mov al, lrg_num ;assume lrgst no at present is 0
back: cmp al, [bx+di] ;compare lrge no to the first element
jnc skip ;// if CF= 0, the element is smaller
mov dl, [bx+di] ; else move large no into
mov al, dl ; al using 3rd register\\
skip: inc di
loop back ;repeat for all the elements of the array
mov lrg_num, al ;store largest no in mem loc
mov ah, 4ch ;//Terminate
int 21h ; 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
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
xor di, di ; di = 0
mov cx, ary_cnt ;cx = no of elements in array
lea bx, array ;bx = effective address of array
mov al, [bx+di] ;al <-- first element of array
dec cx
inc di
back: cmp al, [bx+di] ;compare 1st and 2nd element of array
jc skip ;//if al = small, skip
mov dl, [bx+di] ; else take the small no
mov al, dl ; into al and then proceed
skip: inc di ; to check the next element\\
loop back
mov sml_num, al ;store small number in mem loc
mov ah, 4ch ;//Terminate
int 21h ; 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 ;//Intialize
mov ds, ax ; the data segment\\
mov bx, ary_cnt-1 ;no of passes
nxtchk: mov cx, bx ;no of checks
mov si, 00h ; si = 0
nxtpas: mov al, array[si] ;al = 1st element
inc si
cmp al, array[si] ;compare 1st and 2nd element
jbe nxtemt ;//if below jump to
xchg al, array[si] ; nxtemt otherwise
mov array[si-1], al ; exchange the elements\\
nxtemt: loop nxtpas ;proceed with next pass
dec bx ;dx = dx - 1
jnz nxtchk ;continue with next check
mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

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 ;//Intialize
mov ds, ax ; the data segment\\
mov bx, ary_cnt-1 ;no of passes
nxtchk: mov cx, bx ;no of checks
mov si, 00h ; si = 0
nxtpas: mov al, array[si] ;al = 1st element
inc si
cmp al, array[si] ;compare 1st and 2nd element
jge nxtemt ;//if greater jump to
xchg al, array[si] ; nxtemt otherwise
mov array[si-1], al ; exchange the elements\\
nxtemt: loop nxtpas ;proceed with next pass
dec bx ;dx = dx - 1
jnz nxtchk ;continue with next check
mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

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
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov cx, 2 ;cx = 2
loop1: mov dx, cx ;dx = cx
dec dx ;dx = dx - 1
mov si, dx ;si = dx
add si, si ;si = si + si
mov ax, array[si] ;//compare the first
loop2: cmp array[si-2], ax ; and 2nd element\\
jbe loop3 ;if below leave as it is
mov di, array[si-2] ;//else exchange the elements
mov array[si], di
dec si ;//decrement si
dec si ; two times\\
dec dx
jnz loop2
loop3: mov array[si], ax
inc cx
cmp cx, count
jbe loop1
mov ah, 4ch ;//Terminate
int 21h ; 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
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov cx, 2 ;cx = 2
loop1: mov dx, cx ;dx = cx
dec dx ;dx = dx - 1
mov si, dx ;si = dx
add si, si ;si = si + si
mov ax, array[si] ;//compare the first
loop2: cmp array[si-2], ax ; and 2nd element\\
jae loop3 ;if above leave as it is
mov di, array[si-2] ;//else exchange the elements
mov array[si], di
dec si ;//decrement si
dec si ; two times\\
dec dx
jnz loop2
loop3: mov array[si], ax
inc cx
cmp cx, count
jbe loop1
mov ah, 4ch ;//Terminate
int 21h ; 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 ;//Intialize
mov ds, ax ; the data and
mov es, ax ; extra segments\\
mov cx, str_cnt ;intialize the counter for no of chars
mov si, offset src ;si = offset address of source string
mov di, offset dst ;di = offset address of destination memory
cld ;DF = 0; auto increment of si & di
rep movsb ;trnsfr the contents pointed by si to mem pointed by
di
mov ah, 4ch ;//Terminate
int 21h ; the program\\
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 ;//Intialize
mov ds, ax ; the data segment\\
mov cx, str_cnt ;Intialize the array counter
mov si, offset string ;si = pointer to beginning of string
mov di, si ;//di = pointer to
add di, str_cnt ; end of the string\\
repeat: cmp di, si ;compare si & di
jbe exit ;if di <= si, exit
mov ah, [si] ;// otherwise
xchg ah, [di] ; exchange the contents
mov [si], ah ; of si & di\\
dec di ;decrement di
inc si ;increment si
jmp repeat ;jump to repeat
exit: lea dx, string ;// display the
mov ah, 09h ; reversed string\\
int 21h
mov ah, 4ch ;// Terminate
int 21h ; the program\\
end

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 ;//Intialize
mov ds, ax ; data and
mov es, ax ; extra segment\\
mov cx, str_cnt ;intialize string counter
lea di, string ; di = offset of string
mov al, 'T' ;al = character to be searched
back: cmp al, byte ptr[di] ;compare char with first element in the string
je found ;if found jump to found
inc di ;//else proceed with
loop back ; comparing all chars in string\\
lea dx, msg2 ;// if not found display
mov ah, 09h ; msg2 using DOS services\\
int 21h
jmp last
found: mov ah, 09h ;//if found display
lea dx, msg1 ; msg1 using DOS services\\
int 21h
mov dx, str_cnt ;// store the offset
sub dx, cx ; location of the desired
mov loc, dx ; char in a string\\
last: mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

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
mov ax, @data ;//Intialize
mov ds, ax ; the data
mov es, ax ; and extra segment\\
mov cx, str_cnt ;intialize the counter
dec cx ;decrement counter
lea si, src ;si = offset of source string
lea di, src ;source offset in di also
add si, cx ;si point to last location
back: mov ah, [si] ;last char in ah
mov al, [di] ;first char in al
cmp ah, al ;//if they are not
jne last ; equal go to last\\
dec si ;//if they are equal
inc di ; decrement si and increment di\\
cmp si, di ;//compare si & di if they are equal
jbe disp ; display msg2\\
jmp back ;else continue to compare for nxt chars
disp: lea dx, msg2 ;display msg2
mov ah, 09h
int 21h
jmp exit
last: lea dx, msg1 ;display msg1
mov ah, 09h
int 21h
exit: mov ah, 4ch ;//Terminate
int 21h ; 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 ;//Intialize
mov ds, ax ; the data segment\\
mov ah, 01h ;//DOS function to get a
int 21h ; character from keyboard\\
mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

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 ;max length passed to the function
db 0 ;actual length returned by function
db 30 dup(0) ;space for storing returned string from function
.code
mov ax, @data ;//Intialize
mov ds, ax ; the data segment\\
mov ah, 0ah ;//DOS function to
lea dx, buff ; read string from the keyboard\\
int 21h
mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

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 ;//Intialize
mov ds, ax ; the data segment\\
mov ah, 02h ;//call DOS service
mov dl, 'E' ; to display the char 'E'
int 21h ; on console\\
mov ah, 4ch ;//Terminate
int 21h ; the program\\
end

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 ;//Intialize
mov ds, ax ; the data segment\\
lea dx, str ;dx = offset address of str
mov ah, 09h ;//call DOS service to
int 21h ; display the string\\
mov ah, 4ch ;//Terminate
int 21h ; 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 ;Specify the model for the executable. Must for every program.
.STACK 5000H

.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 ; Initialize all segment registers 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 AX, 92h ; Initialize Port A - Input, CU & CL - Output


MOV DX, CR
OUT DX, AX ; Write to Control Register.

GETKEY:
MOV BH, 1h ; Scan Lines
MOV BL, 00h ; Initialize a counter. It contains the no of the Key

SCANLINES:
MOV AL, BH
MOV DX, PC ; Send Line Number to Port CL
OUT DX, AL

MOV DX, PA ; Read from Port A


IN AL, DX

Dept of ECE, 47 NMIT, Bangalore.


Girisha G K, Asst Professor

MOV CH, AL ; CH has the value indicating the key pressed


MOV AL,00H
CHECK: ; Initialize the counter
; Now repeatedly check which key was selected.
MOV CL, CH
AND CL, 01h ; CH is shifted to right
CMP CL, 01h
JZ DISPLAY ; If Equal Come out
INC BL
SHR CH, 01h ; CH = CH >> 1
INC AL
CMP AL, 08h ; All bits are compared
JNZ CHECK ; Go back for next scan line
SHL BH, 01h ; Move to next scan line
CMP BH, 10h
JNZ SCANLINES ; Repeat the SCAN Lines Loop (4 times)
JMP LOOPOUT

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 ; Display the character pressed.
MOV DX, OFFSET Show
INT 21h
MOV CX, 0FFFFh

DELAY:
MOV AX, 0FFh
DELAY1: DEC AX
JNZ DELAY1
LOOP DELAY

LOOPOUT:
MOV AH, 01h
INT 16h ; Get the Key
JZ GETKEY ; Check for the key

Dept of ECE, 48 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, 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 ;Specify the model for the executable. Must for every program.

.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 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 ; Initialize A - Output B-Input Ports


MOV DX, CR
OUT DX, AL ; Write to Control Register.

GETKEY:
MOV DX, PB ; Get Data from Register B
IN AL, DX

Dept of ECE, 55 NMIT, Bangalore.


Girisha G K, Asst Professor

NOT AL ; Compliment it.

MOV DX, PA ; Put Data to Register A


OUT DX, AL

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 ; Exit the program safely.
INT 21h

; *********************************************************************
;
; 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.

You might also like