-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11._FactorialOfNumber(Proc).asm
More file actions
76 lines (55 loc) · 2.87 KB
/
11._FactorialOfNumber(Proc).asm
File metadata and controls
76 lines (55 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
TITLE FactorialOfNumber
;Program Desctiption: This program receives a number and displays its factorial (16bit)
;Author: Liul Alemayehu
include 'emu8086.inc'
.MODEL SMALL ; com program, Memory Descriptor (Tiny | Small | Medium | Large)
.STACK 100 ; stack size
.DATA ; Beginning of data segment where data is stored
msg db "Enter a number: ",0 ; Declare variable msg and assigned it a value
newline db 13,10,'$' ; Creates newline
factorialMsg db "The factorial is:- ",0 ; Factorial message
.CODE ; Beginning of code segment
main PROC
; Start
MOV Ax,@DATA ; Moving address of data (data segment) to Ax register
MOV Ds,Ax ; Initializing of Data segment register (Ds) from Accumilator register (Ax)
; Prompt
LEA Si,msg ; Loading the effective address of our msg to the Source index register (Si)
CALL PRINT_STRING ; Calling the PRINT_STRING procedure
MOV Di,01 ; Used to loop the main code (Di=1)
MOV Bx,01 ; Stores the factorial of the number (initially 1,Bx=1)
; Read number
CALL SCAN_NUM ; Calls the SCAN_NUM procedure and reads a multi-digit number from the keyboard
; Enter Pressed
Enter:
; Print factorial number
; Newline
LEA Dx,newline
MOV Ah,09h
INT 21h
; Print factorial message
LEA Si,factorialMsg ; Loading the effective address of our msg to the Source index register (Si)
CALL PRINT_STRING ; Calling the PRINT_STRING procedure
L1:
CMP Cx,Di ; IF Cx(number) >= Di(01)
JGE L2 ; Goto L2
JMP Print ; Goto Print
L2:
MOV Ax,Cx ; Move Cx(number) to Ax
MUL Bx ; Multiply Bx(factorial) by Ax(number)
MOV Bx,Ax ; Update factorial
DEC Cx ; Decrease number
JMP L1 ; Goto L1
; Print value
Print:
MOV Ax,Bx ; Move factorial value to Ax
CALL PRINT_NUM ; Call the PRINT_NUM procedure
Exit:
MOV Ah,4Ch ; Exit
INT 21h ; Does it!
main ENDP
DEFINE_SCAN_NUM
DEFINE_PRINT_NUM_UNS
DEFINE_PRINT_NUM
DEFINE_PRINT_STRING
END main