; PROGRAM: TO EVALUATE nCr
read macro num
mov ah, 01h
int 21h
sub al, 30h
mov num, al
endm
print macro s
mov ah, 09h
mov dx, offset s
int 21h
endm
data segment
msgn db 'Enter n: $'
msgr db 'Enter r: $'
numn db 00h
numr db 00h
fnr dw 00h
fn dw 00h
fr dw 00h
valis db 'nCr is: $'
nl db 0ah,0dh,'$'
value db 10 dup('$')
data ends
code segment
assume cs:code, ds:data
start:
mov ax, data
mov ds, ax
print msgn
read numn
print nl
print msgr
read numr
mov bl, numr
call factorial
mov fr, ax ;save r! into fr
mov bl, numn
mov al, numr
sub bl, al
call factorial
mov fnr, ax ;save (n-r)! into fnr
mov bl, numn
call factorial
mov fn, ax ;save n! into fn
mov ax, fn ;ax = n!
mov dx, 0000h
mov bx, fnr ;bx = (n-r)!
div bx ;ax = dx-ax/bx
mov dx, 00h
mov bx, fr ;bx = r!
div bx ;ax = dx-ax/bx
mov si, offset value
call hex2ascii ;converts hex in ax
print nl ;to ascii into si loc
print valis
print value
mov ah, 4ch
int 21h
factorial proc near ;find factorial of
mov bh, 00h ;value in bl and push
mov ax, 0001h
mov cx, ax
REP_MUL:
mul cx
cmp cx, bx
jge STOPMUL
inc cx
jmp REP_MUL
STOPMUL:
ret
factorial endp
hex2ascii proc near
mov bx, 000ah
mov cx, 0000h
REP_DIV:
mov dx, 0000h
div bx
add dl, 30h
push dx
inc cx
cmp al, 0ah
jge REP_DIV
cmp al, 00h
je SKIP_0
add al, 30h
mov [si], al
inc si
SKIP_0:
SAVE_ASCII:
pop dx
mov [si], dl
inc si
loop SAVE_ASCII
mov [si], '$'
ret
hex2ascii endp
code ends
end start
**************************
OUTPUT
Z:\S5IT\masm>NCR
Enter n: 4
Enter r: 2
nCr is: 6
**************************
0 comments:
Post a Comment