Professional Documents
Culture Documents
1: Programs Involving Data Transfer Instructions: Example Result: Before Execution: After Execution
1: Programs Involving Data Transfer Instructions: Example Result: Before Execution: After Execution
.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:
.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:
; 1:- PROGRAM 02
.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:
.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:
.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:
.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:
.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:
.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:
.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:
.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:
.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:
.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:
.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:
.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:
.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:
; 2:- PROGRAM 06
.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:
; 2:- PROGRAM 07
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:
; 2:- PROGRAM 08
.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:
; 2:- PROGRAM 09
.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\\
.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
OBSERVED RESULT:
.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
OBSERVED RESULT:
.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
OBSERVED RESULT:
.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
OBSERVED RESULT:
; 3:- PROGRAM 01
.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:
; 3:- PROGRAM 02
; FILE NAME:oddorevn.asm
.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:
; 3:- PROGRAM 03
.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:
; 3:- PROGRAM 04
;** 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:
.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:
.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:
.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:
.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:
.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:
.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:
.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:
.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:
.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:
.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:
; 5:- PROGRAM 01
.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:
; 5:- PROGRAM 02
.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:
; 5:- PROGRAM 03
.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:
; 5:- PROGRAM 04
.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:
; 6:- PROGRAM 01
.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:
; 6:- PROGRAM 02
.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:
; 06:- PROGRAM 03
.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:
; 06:- PROGRAM 04
.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:
; 07:- PROGRAM 01
.MODEL SMALL ;Specify the model for the executable. Must for every program.
.STACK 5000H
CR EQU 0c003H
PA EQU 0c000H
PB EQU 0c001H
PC EQU 0c002H
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
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
; 07:- PROGRAM 02
.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
MOV AL,80h
MOV DX,CR
OUT DX,AL
GETKEY:
MOV BX,0000h
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
INC BX
CMP BX,20
JNZ LOOP1 ;LOOP1 Repeats from here.
MOV AH,01h
INT 16h ;Get the Key
JZ GETKEY
END_PROGRAM:
; 07:- PROGRAM 03
.MODEL TINY ;Specify the model for the executable. Must for every program.
;; 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
GETKEY:
MOV DX, PB ; Get Data from Register B
IN AL, DX
; 07:- PROGRAM 04
.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
Speed DW 0FFH
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)
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
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
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
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