Sunday, 29 January 2012

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

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More