Showing posts with label asm. Show all posts
Showing posts with label asm. Show all posts

Wednesday, 7 March 2012

Masm program to check a given year is leap year or not


printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro nu
mov dl,nu
add dl,30h
mov ah,02h
int 21h
endm

calcnum macro regx
mov ax,regx
mov bl,100
div bl
mov dh,ah
aam
mov bl,al
printnum ah
printnum bl
mov al,dh
aam
mov bl,al
printnum ah
printnum bl

endm

data segment
n db,0000
n1 db ,0000
msg1 db 0ah,0dh,"Enter a year$"
msg2 db 0ah,0dh,"Leap year$"
msg3 db 0ah,0dh,"Not leap year$"
msg4 db 0ah,0dh,"$"
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax

printmsg msg1
mov ah,01h
int 21h
sub al,30h
mov ah,00
mov cx,1000
mul cx
mov n,ax

mov ah,01h
int 21h
sub al,30h
mov ah,00
mov cx,100
mul cx
add ax,n
mov n,ax

mov ah,01h
int 21h
sub al,30h
mov ah,00
mov cl,10
mul cl
add ax,n
mov n,ax

mov ah,01h
int 21h
sub al,30h
mov ah,00
add ax,n
mov n,ax

mov cx,0004
mov dx,0000
mov ax,n
div cx

cmp dx,0000
je t1
jmp t3
t1:
mov cx,0100
mov dx,0000
mov ax,n
div cx

cmp dx,0000
jne t2
jmp t3
t2:
printmsg msg2
jmp st1
t3:
mov ax,n
mov bx,400
div bx
cmp dx,0000
je t2

printmsg msg3



st1:
mov ah,4ch
int 21h
code ends
end start

Monday, 6 February 2012

Masm program to sort n numbers


printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm

calcnum macro regx
mov ax,regx
mov dh,100
div dh
mov n,ah
aam
mov dh,al
printnum ah
printnum dh
mov al,n
aam
mov dh,al
printnum ah
printnum dh
endm


data segment
num db 0
array db 50 dup(0)
n db 0



cr equ 0dh
msg0 db "Enter a limit(Ex:05)$"
msg1 db "Enter the nos(Ex:06)$"
msg2 db 0ah,0dh,"After sorting$"
msg4 db 0ah,"$"
msg5 db 0ah,0dh,"Before sorting$"
data ends

code segment
assume cs:code,ds:data

start:
mov ax,data
mov ds,ax
mov cl,00
printmsg msg0
mov ah,01h
int 21h
sub al,30h
mov n,al
mov ah,00
mov dh,10
mul dh
mov n,ax
mov ah,01h
int 21h
sub al,30h
mov ah,00
add ax,n

mov cl,ax
mov num,cl
printmsg msg4
printmsg msg1
lea si,array
l1:

mov ah,01h
int 21h
sub al,30h
mov ah,00
mov ch,10
mul ch
mov n,ax
mov ah,01h
int 21h
sub al,30h
mov ah,00
add ax,n
mov [si],ax

inc si
dec cl
printmsg msg4
cmp cl,00
jne l1



printmsg msg4
mov cl,num
mov ch,00
printmsg msg5
printmsg msg5
lea si,array
t1:
printmsg msg4
cmp cl,ch
je t2

mov ah,00
mov al,[si]
calcnum ax

inc ch
inc si
jmp t1
t2:
printmsg msg4
mov cl,num
mov bh,00
mov bl,00
mov dl,num
dec dl
lea si,array
i1:

cmp bh,cl
je stop
mov di,si
inc di
mov bl,bh
inc bl
j1:
cmp bl,cl
je jstop
mov ch,[si]
cmp ch,[di]
jc k1
xchg ch,[di]
mov [si],ch
k1:
inc bl
inc di
jmp j1
jstop:
inc si
inc bh
jmp i1
stop:


mov cl,num
lea si,array
printmsg msg4
printmsg msg2
q1:
printmsg msg4
mov ah,00
mov al,[si]
calcnum ax
dec cl
inc si
cmp cl,00
jne q1
mov ah,4ch
int 21h
code ends
end start




Count number of words in a sentence-Masm program


printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm

data segment

str db 40 dup(0)

cr equ 0dh
lf equ 0ah
msg1 db "Enter a string:$"
msg4 db 0ah,0dh,"$"
msg5 db 0ah,0dh,"No: of words$"
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
lea si,str
mov cl,00

printmsg msg1
printmsg msg4
mov ch,'$'
mov [si],ch
l1:
mov ah,01h
int 21h
cmp al,32
je v1
jmp v2
v1:inc cl
jmp v2
v2:cmp al,cr
je p3
sub al,30h
mov [si],al
inc si
mov [si],ch
jmp l1

p3:
mov si,offset str
mov ch,[si]
cmp ch,'$'
je d1
inc cl
jmp d2
d1:mov cl,00



d2:printmsg msg4
printmsg msg5
printmsg msg4
printnum cl




mov ah,4ch
int 21h
code ends
end start



Friday, 3 February 2012

Masm program-Palindrome checking


printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm


data segment
n db 0
array db 40 dup(0)
revarray db 40 dup(0)
cr equ 0dh
msg1 db "Enter a string:$"
msg2 db 0ah,0dh,"Palindrome$"
msg3 db 0ah,0dh,"Not Palindrome$"

msg4 db 0ah,0dh,"$"
msg5 db 0ah,0dh,"Reversed string$"
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
lea si,array
mov cl,00
printmsg msg1
printmsg msg4
l1:
mov ah,01h
int 21h
cmp al,cr
je p2
sub al,30h
mov [si],al
inc si
inc cl
jmp l1
p2:
mov n,cl
mov ch,'$'
mov [si],ch




printmsg msg4
dec si
lea di,revarray
x2:
mov al,[si]
mov [di],al
inc di
dec si
dec cl

cmp cl,00
jne x2
mov cl,'$'
mov [di],cl
lea di,revarray
printmsg msg4
printmsg msg5
printmsg msg4
y2:

mov dl,[di]
add dl,30h
mov ah,02h
int 21h
inc di

mov ch,[di]
cmp ch,'$'
jne y2
printmsg msg4

mov si,offset array
mov di,offset revarray
mov cl,n
w2:
mov al,[si]
cmp al,[di]
jne w1
inc si
inc di
dec cl
cmp cl,00
jne w2

printmsg msg2
jmp w4
w1:
printmsg msg3

w4:mov ah,4ch
int 21h
code ends
end start



Binary to Hexadecimal converter-Masm program


printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm

calcnum macro regx
mov ax,regx
mov dh,100
div dh
mov num,ah
aam
mov dh,al
printnum ah
printnum dh
mov al,num
aam
mov dh,al
printnum ah
printnum dh
endm

multiply macro num1
mov ax,0001
mov dh,02
mov dl,00
l10:
cmp dl,num1
jz l9
mul dh
inc dl
jmp l10
l9:
endm

data segment
num dw ?
num1 db 0

array db 10 dup(0)

cr equ 0dh
msg1 db "Enter a binary number$"
msg2 db 0ah,0dh,"Hex Equivalant$"
a db "A$"
b db "B$"
c db "C$"
d db "D$"
e db "E$"
f db "F$"
msg4 db 0ah,"$"
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov cl,00


printmsg msg1
lea si,array
mov bx,si

printmsg msg4

l2:mov ah,01h
int 21h
cmp al,cr
je l1
sub al,30h
mov [si],al
inc si
inc cl
jmp l2

l1:
printmsg msg2
mov bx,0000
mov si,offset array

l5:

cmp cl,00
jne l6
jmp l7
l6:

multiply cl
mov dh,[si]
mul dh
add bx,ax
inc si
dec cl
jmp l5

l7:
printmsg msg4

mov ax,bx

mov dh,02
 div dh
mov ah,00

mov cl,00
mov si,offset array
mov dh,16
p1:

div dh

mov [si],ah
inc si
inc cl
cmp al,00
je p2
mov ah,00
jmp p1
p2:
dec si

b1:
mov al,[si]
cmp al,10
je a1
jmp a2
a1:
printmsg a
dec si
dec cl
r4:cmp cl,00
jne b1

jmp t1

a2:cmp al,11
je c1
jmp a3
c1:
printmsg b
dec si
dec cl
r3:cmp cl,00
jne r4

jmp t1

a3:cmp al,12
je c2
jmp a4
c2:printmsg c
dec si
dec cl
r2:cmp cl,00
jne r3

jmp t1

a4:
cmp al,13
je c3
jmp a5
c3:printmsg d
dec si
dec cl
r1:cmp cl,00
jne r2

jmp t1

a5:cmp al,14
je c4
jmp a6
c4:printmsg e
dec si
dec cl
z2:cmp cl,00
jne r1

jmp t1

a6:cmp al,15
je c5
jmp a7
c5:printmsg f
dec si
dec cl
z1:cmp cl,00
jne z2

jmp t1

a7:
printnum al
dec si
dec cl
cmp cl,00
jne z1



jmp t1

t1:mov ah,4ch
int 21h
code ends
end start






Binary to Decimal converter- Masm program


printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm

calcnum macro regx
mov ax,regx
mov dh,100
div dh
mov num,ah
aam
mov dh,al
printnum ah
printnum dh
mov al,num
aam
mov dh,al
printnum ah
printnum dh
endm

multiply macro num1
mov ax,0001
mov dh,02
mov dl,00
l10:
cmp dl,num1
jz l9
mul dh
inc dl
jmp l10
l9:
endm

data segment
num dw ?
num1 db 0

array db 10 dup(0)

cr equ 0dh
msg1 db "Enter a binary number$"
msg2 db 0ah,0dh,"Decimal Equivalant$"

msg4 db 0ah,"$"
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov cl,00


printmsg msg1
lea si,array
mov bx,si

printmsg msg4

l2:mov ah,01h
int 21h
cmp al,cr
je l1
sub al,30h
mov [si],al
inc si
inc cl
jmp l2

l1:mov bx,0000
mov si,offset array

l5:

cmp cl,00
jne l6
jmp l7
l6:

multiply cl
mov dh,[si]
mul dh
add bx,ax
inc si
dec cl
jmp l5

l7:
printmsg msg4
printmsg msg2

printmsg msg4
mov ax,bx
mov dh,02
 div dh
mov ah,00
calcnum ax

mov ah,4ch
int 21h
code ends
end start






Thursday, 2 February 2012

Fibonacci series-Masm program


printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm

calcnum macro regx
mov ax,regx
mov dh,100
div dh
mov num,ah
aam
mov dh,al
printnum ah
printnum dh
mov al,num
aam
mov dh,al
printnum ah
printnum dh
endm


data segment
num dw 0
a dw 0000
b dw ?

regx dw ?
msg1 db "Enter a non zero limit(Ex: 05)$"
msg2 db 0ah,0dh,"Fibnocci series $"
msg3 db 0ah,"$"
msg5 db 0ah,0dh,"Try again$"
data ends

code segment
assume cs:code,ds:data

start:
mov ax,data
mov ds,ax
printmsg msg1
mov ah,01h
int 21h
sub al,30h
mov ah,00
mov dh,10
mul dh
mov num,ax
mov ah,01h
int 21h
sub al,30h
mov ah,00
add ax,num


mov cx,ax
cmp cx,0000
je p1
jmp p2
p1:
printmsg msg5
mov ah,4ch
int 21h
jmp l5
p2:cmp cx,0001
je l9
jmp l7
l9:
printmsg msg3
printmsg msg2
calcnum a
mov ah,4ch
int 21h
jmp l5



l7:
mov bx,0001
cmp cx,0002
je l6
jmp l4
l6:printmsg msg3
printmsg msg2
calcnum a
printmsg msg3
calcnum bx
mov ah,4ch
int 21h
jmp l5

l4:
mov bx,0001
printmsg msg3
printmsg msg2
calcnum a
printmsg msg3
calcnum bx
sub cx,0002
l1:
printmsg msg3
mov dx,a
add dx,bx
mov b,dx
calcnum dx
mov dx,b
mov a,bx
mov bx,dx
dec cx
cmp cx,0000
jne l1


l5:
code ends
end start




Sunday, 29 January 2012

Check a number is Armstrong or not-Masm program

printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm



data segment
n dw ?
p dw 00
rem dw ?
z dw ?

cr db,0dh,"$"
msg1 db "Enter a number(Ex:0123)$"
msg2 db 0ah,0dh,"Armstrong$"
msg3 db 0ah,0dh,"Not Armstrong$"
msg4 db 0ah,0dh,"$"
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax


printmsg msg1

mov ah,01h
int 21h
mov ah,00
sub al,30h
mov dx,1000
mul dx
mov n,ax
mov ah,01h
int 21h
mov ah,00
sub al,30h
mov dh,100
mul dh
add ax,n
mov n,ax
mov ah,01h
int 21h
sub al,30h
mov ah,00
mov dh,10
mul dh
add ax,n
mov n,ax
mov ah,01
int 21h
sub al,30h
mov ah,00
add ax,n

mov n,ax
mov z,ax
mov cl,04

l2:
mov ax,n
mov dh,10
div dh
mov rem,al
mov al,ah
mov ch,ah
mov ah,00
mul ch
mul ch
add ax,p
mov p,ax
mov ax,rem
mov n,ax
dec cl
cmp cl,00
jne l2


mov dx,p
cmp z,dx
je l3
l4:
printmsg msg3
jmp skip
l3:
printmsg msg2








skip:mov ah,04h
int 21h
code ends
end start



Masm program to find prime numbers upto a limit

printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm

calcnum macro regx
mov ax,regx
mov dh,100
div dh
mov num,ah
aam
mov dh,al
printnum ah
printnum dh
mov al,num
aam
mov dh,al
printnum ah
printnum dh
endm

data segment
num dw ?
i dw 02
j dw ?
n dw ?

msg1 db "Enter a limit$"
msg2 db 0ah,0dh,"Prime nos:$"
msg3 db 0ah,0dh,"$"
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax

printmsg msg1

mov dh,02
mov ah,01h
int 21h
mov ah,00
sub al,30h

mov cl,10
mul cl
mov n,ax
mov ah,01h
int 21h
mov ah,00
sub al,30h

add ax,n
inc ax
mov n,ax

printmsg msg2


mov ah,00
mov cx,n
l3:
cmp cx,i
je l1

mov j,02
mov ax,i
mov dh,02
div dh
mov bl,al
l6:cmp j,bl
jnc l2
l7:mov ax,i
mov dl,j
div dl
cmp ah,00
je l9
inc j
jmp l6

l2:
jz l7
printmsg msg3
calcnum i
l9:inc i
jmp l3

l1:mov ah,04h
int 21h
code ends
end start

Factorial of a number-Masm program

printmsg macro msg
mov ah,09h
lea dx,msg
int 21h
endm

printnum macro reg
mov dl,reg
add dl,30h
mov ah,02h
int 21h
endm

calcnum macro regx
mov ax,regx
mov dh,100
div dh
mov num,ah
aam
mov dh,al
printnum ah
printnum dh
mov al,num
aam
mov dh,al
printnum ah
printnum dh
endm

data segment
num dw  ?
msg1 db "Enter a number$"
msg2 db 0ah,0dh,"Factorial:$"
msg3 db 0ah,0dh,"$"
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax

printmsg msg1

mov ah,01h
int 21h

sub al,30h
mov ah,00


mov cl,al
mov ch,00
mov ax,0001


l1:
mul cx

dec cl
cmp cl,01
jne l1


mov bx,ax
printmsg msg2
calcnum bx


mov ah,04h
int 21h
code ends
end start

Twitter Delicious Facebook Digg Stumbleupon Favorites More