0% found this document useful (0 votes)
10 views3 pages

Practical No 13 MP

This assembly language program calculates the factorial of a number provided as a command-line argument. It converts the ASCII input to an integer, computes the factorial recursively, and then displays the result. The program uses DOS interrupts for output and exits cleanly after execution.

Uploaded by

Kiran janjal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Practical No 13 MP

This assembly language program calculates the factorial of a number provided as a command-line argument. It converts the ASCII input to an integer, computes the factorial recursively, and then displays the result. The program uses DOS interrupts for output and exits cleanly after execution.

Uploaded by

Kiran janjal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No 13

.MODEL SMALL
.STACK 100h

.DATA
msg1 DB "Factorial is: $"
result DW 1

.CODE

main PROC
MOV AX, @DATA
MOV DS, AX
MOV ES, AX

; Get command-line argument from PSP (Program Segment Prefix)


MOV SI, 80h ; PSP: Offset 80h stores command-line length
MOV CL, [SI] ; Get length of argument
INC SI ; Move to first char
MOV CH, 0 ; CX = length

; Convert ASCII to integer


XOR AX, AX
XOR BX, BX

NextDigit:
MOV BL, [SI]
CMP BL, ' '
JE SkipSpace
SUB BL, '0'
MUL CX ; AX = AX * 10
ADD AX, BX ; AX += digit
SkipSpace:
INC SI
LOOP NextDigit

; AX = input number
PUSH AX
CALL Factorial
MOV result, AX

; Display message
MOV AH, 09h
LEA DX, msg1
INT 21h

; Print result
MOV AX, result
CALL PrintNumber
; Exit
MOV AH, 4Ch
INT 21h
main ENDP

; ----------------------------
; Recursive Factorial Function
; Input: number on stack
; Output: AX = factorial
; ----------------------------
Factorial PROC
PUSH BP
MOV BP, SP

MOV AX, [BP+2] ; Get parameter


CMP AX, 1
JBE EndRecursion

DEC AX
PUSH AX ; Push (n - 1)
CALL Factorial
MOV BX, AX ; Store result of (n - 1)!

MOV AX, [BP+2] ; n


MUL BX ; AX = n * (n - 1)!

EndRecursion:
POP BP
RET
Factorial ENDP

; ----------------------------
; PrintNumber: prints number in AX
; ----------------------------
PrintNumber PROC
PUSH AX
PUSH BX
PUSH CX
PUSH DX

MOV CX, 0
MOV BX, 10

NextDigitPrint:
XOR DX, DX
DIV BX
PUSH DX
INC CX
OR AX, AX
JNZ NextDigitPrint
PrintLoop:

POP DX
ADD DL, '0'
MOV AH, 02h
INT 21h
LOOP PrintLoop

POP DX
POP CX
POP BX
POP AX
RET
PrintNumber ENDP

END main
Output:
factorial 4
Factorial is: 24

You might also like