forked from miguelmota/x86-assembly-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactorial.asm
executable file
·89 lines (65 loc) · 1.42 KB
/
factorial.asm
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
; Description: Find factorial of a number.
INCLUDE Irvine32.inc
.data
msgInput BYTE "Enter the value of n to calculate "
BYTE "the factorial (-1 to quit): ",0
msgOutput BYTE "The factorial is: ",0
.code
main PROC
L1:
mov edx, OFFSET msgInput
call WriteString
call ReadInt
call Crlf
cmp eax, 0
jl quit
call calcFactorial
jc failed
push eax
mov edx, OFFSET msgOutput
call WriteString
pop eax
call WriteDec
failed:
call Crlf
call Crlf
loop L1
quit: exit
main ENDP
calcFactorial PROC USES ecx edx
.data
factorialError BYTE "Error: Calculated value cannot "
BYTE "fit into 32 bits",0
.code
.IF eax == 0 || eax == 1
mov eax, 1
jmp end_factorial
.ENDIF
mov ecx, eax
Factorial_loop:
dec ecx
mul ecx
jc error
cmp ecx, 1
ja Factorial_loop
jmp end_factorial
error:
mov edx, OFFSET factorialError
call WriteString
stc
end_factorial:
ret
calcFactorial ENDP
END main
; =======================================
; OUTPUT
; =======================================
COMMENT !
Enter the value of n to calculate the factorial (-1 to quit): 5
The factorial is: 120
Enter the value of n to calculate the factorial (-1 to quit): 7
The factorial is: 5040
Enter the value of n to calculate the factorial (-1 to quit): -1
Press any key to continue . . .
!
; =======================================