-
Notifications
You must be signed in to change notification settings - Fork 1
/
SieveOfEratosthenes.asm
104 lines (83 loc) · 2.33 KB
/
SieveOfEratosthenes.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
TITLE SieveOfEratosthenes.asm
;// Author: Lee P
;// Date: November 2020
;// Description: This program presents a menu allowing the user to pick a menu option
;// which then prints all prime numbers from 2 up to n, which will be a user
;// input integer from 2-1000
;// 1. Print all prime numbers (2 - n <= 1000)
;// 2. Exit
;// ====================================================================================
include Irvine32.inc
PrintPrimes PROTO,
count: DWORD ;// number of values ? ? to display
FIRST_PRIME = 2
LAST_PRIME = 1000;
.data
commaStr BYTE ",", 0
sieve BYTE LAST_PRIME DUP(? )
.code
main PROC
;// Initialize the array to zeros
mov ecx, LAST_PRIME
mov edi, OFFSET sieve
mov al, 0
cld
rep stosb
mov esi, FIRST_PRIME
.WHILE Esi < LAST_PRIME
.IF Sieve[esi * TYPE sieve] == 0 ;// is current entry prime ?
call MarkMultiples ;// yes: mark all of its multiples
.ENDIF
inc esi ;// move to next table entry
.ENDW
INVOKE PrintPrimes, LAST_PRIME ;// display all primes found
exit
main ENDP
;// ------------------------------------------------ - -
MarkMultiples PROC
;// Mark all multiples of the value passed in ESI.
;// Notice we use ESI as the prime value, and
;// Take advantage of the "scaling" feature of indirect
;// Operands to locate the address of the indexed item :
;// [Esi * TYPE sieve]
;// ------------------------------------------------ - -
push eax
push esi
mov eax, esi ;// prime value
add esi, eax ;// start with first multiple
L1 :
cmp esi, LAST_PRIME; end of array ?
ja L2; yes
mov sieve[esi * TYPE sieve], 1 ;// no: insert a marker
add esi, eax
jmp L1; repeat the loop
L2 :
pop esi
pop eax
ret
MarkMultiples ENDP
;// ------------------------------------------------ - -
PrintPrimes PROC,
count: DWORD;// number of values ? ? to display
;//
;// Display the list of prime numbers
;// ------------------------------------------------ - -
.data
;// count DWORD ? ;// number of values ? ? to display
.code
mov esi, 1
mov eax, 0
mov ecx, count
L1 :
mov al, sieve[esi * TYPE sieve]
.IF Al == 0
mov eax, esi
call WriteDec
mov edx, OFFSET commaStr
call WriteString
.ENDIF
inc esi
loop L1
ret
PrintPrimes ENDP
END main