-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe47.s
150 lines (147 loc) · 3.74 KB
/
e47.s
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
.text
.globl isPrime
.type isPrime,@function
isPrime:
pushq %rbx
pushq %r12
pushq %r13
pushq %r14
pushq %r15
movl %edi,%ebx # ebx = n
cmpl $2,%ebx # n < 2
jl finif # return 0
movl %ebx,%eax # temp = 2
andl $1,%eax # temp&= 1
cmpl $0,%eax # n % 2 == 0
je finic # return n == 2
movl $3,%r12d # i = 3
loop:
movq $0,%rdx # clear rdx for mul
movl %r12d,%eax # put i in eax for mul
imull %r12d # eax = i * i
cmpl %ebx,%eax # i * i <= n
jg overloop
movq $0,%rdx # clear rdx for div
movl %ebx,%eax # put n in eax for div
idivl %r12d # edx = n % i
cmpl $0,%edx # n % i == 0
je finif # return 0
addl $2,%r12d # i+=2
jmp loop
overloop:
movq $1,%rax # return 1
jmp fini
finif:
movq $0,%rax # return 0
jmp fini
finic:
cmpl $2,%ebx # n == 2
jne finif
movq $1,%rax # return 1
fini:
popq %r15
popq %r14
popq %r13
popq %r12
popq %rbx
ret
.size isPrime,.-isPrime
.text
.globl pfc
.type pfc,@function
pfc:
pushq %rbx
pushq %r12
pushq %r13
pushq %r14
pushq %r15
movl %edi,%ebx # ebx = n
movl $0,%r13d # fc = 0
movl $2,%r12d # i = 2
loop2:
movl %ebx,%edi # n is param 1
call isPrime # isPrime(n)
cmpl $1,%eax # if true
je finish
cmpl $3,%r13d # if fc is 3 and not prime, return false
je finishFalse
movq $0,%rdx # clear rdx for div
movl %ebx,%eax # put n in eax for div
idivl %r12d # eax: n%i, edx: n/i
cmpl $0,%edx # n%i == 0
jne overif
movl %eax,%ebx # n /= i
incl %r13d # fc++
divloop: # perform extra divisions of the factor
movq $0,%rdx # clear rdx for division
movl %ebx,%eax # put n in eax for div
idivl %r12d # compute division again
cmpl $0,%edx # n%i == 0
jne overdivloop
movl %eax,%ebx # n /= i
movl %ebx,%edi # n is param 1
call isPrime # we must recheck primality here
cmpl $1,%eax # if isPrime
je finish
cmpl $1,%ebx # if 1
je finish
jmp divloop
overdivloop:
overif:
incl %r12d # i++
jmp loop2
overloop2:
finish:
cmpl $3,%r13d # fc == 3
je finishTrue
finishFalse:
movq $0,%rax
jmp veryend
finishTrue:
movq $1,%rax
veryend:
popq %r15
popq %r14
popq %r13
popq %r12
popq %rbx
ret
.size pfc,.-pfc
.text
.globl e47
.type e47,@function
e47:
pushq %rbx
pushq %r12
pushq %r13
pushq %r14
pushq %r15
movl $210,%ebx # N = 210 (lowest possible num)
movl $0,%r12d # count = 0
loop3:
movl %ebx,%edi # N is param 1
call pfc
cmpl $1,%eax # pfc(N) == 1
jne elsepart
incl %r12d # count++
cmpl $1,%r12d # count == 1
jne overif2
movl %ebx,%r13d # r13d = first in seq
overif2:
cmpl $4,%r12d # count == 4
je thefinish # we are done
jmp overif3
elsepart:
movl $0,%r12d # clear the count
overif3:
incl %ebx
jmp loop3
thefinish:
movl %r13d,%eax
popq %r15
popq %r14
popq %r13
popq %r12
popq %rbx
ret
.size e47,.-e47