Skip to content

Commit faeb1b8

Browse files
committed
Update
1 parent f8189fa commit faeb1b8

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

010.asm

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,38 @@ section .text
88

99
main:
1010
mov ebx, 1 ;array index for outer loop
11+
xor ecx, ecx ;sum
1112

1213
sieve_outer:
1314
inc ebx ;increase index
1415
mov eax, ebx ;copy to eax for squaring
1516
mul ebx ;square
1617
cmp eax, 2000000 ;check if square is > limit
17-
jg reset ;if it is, jump to reset
18+
jg sum ;if it is, sum rest of primes
1819
bt [primes], ebx ;check if ebx is no prime
1920
jnc sieve_outer ;if no prime, try next number
21+
add rcx, rbx ;if yes, add prime to sum
2022

2123
sieve_inner:
2224
btr [primes], eax ;set multiple to not prime
2325
add eax, ebx ;next multiple
2426
cmp eax, 2000000 ;check if multiple is <= limit
25-
jl sieve_inner ;if it is, continue with inner loop
27+
jle sieve_inner ;if it is, continue with inner loop
2628
jmp sieve_outer ;if not, continue with outer loop
2729

28-
reset:
29-
mov rbx, 1 ;reset rax and rbx for building the sum
30-
xor rax, rax
31-
3230
sum:
31+
xor eax, eax ;zero eax
3332
inc ebx ;increase array index
34-
cmp ebx, 2000000 ;check if index has arrived ad 2,000,000
35-
je print ;if yes, print result
3633
bt [primes], ebx ;check if primes[ebx] is 1
37-
jnc sum ;if not, continue
38-
add rax, rbx ;if yes, add prime to sum
39-
jmp sum ;back to sum
34+
cmovc eax, ebx ;if yes, put prime in eax
35+
add rcx, rax ;and add prime (or 0) to sum
36+
cmp ebx, 2000000 ;end of array?
37+
jl sum ;if not, repeat
4038
4139
print: ;printing routine, differs slightly from OS to OS
4240
push rbp
4341
mov edi, msg
44-
mov rsi, rax
42+
mov rsi, rcx
4543
call printf
4644
pop rbp
4745

0 commit comments

Comments
 (0)