-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorial.asm
110 lines (87 loc) · 1.85 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
;Program to print Fibonacci series
%include 'functions.asm'
section .data
input db "Enter range:",0h
fiboseries db "Fibonacci series of range ",0h
line db "-----------------------------------",0h
section .bss
;reservered variables spaces
first resb 2
second resb 2
next resb 2
range resb 2
section .text
global _start
_start:
;------------------------------------
;User prompt
mov eax,input
call sprint
;------------------------------------
;user input range
call getInput
;------------------------------------
;range display
mov eax,fiboseries
call sprint
;------------------------------------
;display user range
mov eax,range
call sprintLF
;------------------------------------
;formatting line
mov eax,line
call sprintLF
;------------------------------------
;conver range to integer and re-assign
mov eax,[range]
sub eax,48
mov [range],eax
;assign prime values
mov ecx,0
mov byte[first],0
mov byte[second],1
main:
;compare if we have reached the range.
cmp ecx,[range]
je exit ;if equal exit program
;if not, lets loop making fibonnacci series :-)
;assign values of the first,second to the next
mov ebx,[first]
mov edx,[second]
;save the values on the stack for future assignment
push ebx
push edx
;get the next value by adding first+second=next
add edx,ebx
mov [next],edx
;print
mov eax,[next]
;save the "next" value on the stack for future assigning
push eax
;iprintLF only prints integers not string chars
call iprintLF
;restore all the values
;first,second,next for assinging
pop eax
pop edx
pop ebx
;re-assign the values interchanging
;first=second
;second=next
;loop!
mov [first],edx ;first=second
mov [second],eax;second=next
inc ecx ;increment counter by one
jmp main ;loop again for comparison
;end the program
exit:
call quit
;function to get user input for the range
getInput:
mov eax,3
mov ebx,2
mov ecx,range
mov edx,2
int 80h
ret