-
Notifications
You must be signed in to change notification settings - Fork 0
/
072.asm
71 lines (59 loc) · 1.98 KB
/
072.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
format ELF64 executable 9
segment readable
limit equ 1000000 ;limit
segment readable writable
result: times 20 db 0 ;empty string for printing the result later
db 10, 0
phi: rd limit ;for phi(n)
segment readable executable
entry start
start:
xor edi, edi ;init n
xor rcx, rcx ;result
init_phi:
inc edi ;next n
mov [phi + 4 * edi], edi ;phi[n] = n
cmp edi, limit ;limit reached?
jl init_phi ;if not, repeat
mov edi, 1 ;index for outer loop
phi_outer:
inc edi ;next index outer loop
cmp edi, limit ;limit reached?
jg finished ;if yes, we are done
cmp [phi + 4 * edi], edi ;phi[edi] = edi?
jne sum ;if not, just add phi[edi] to result
mov esi, edi ;init inner index
phi_inner:
mov eax, [phi + 4 * esi] ;eax = phi[esi]
xor edx, edx ;reset remainder
div edi ;eax = phi[esi] / edi
sub [phi + 4 * esi], eax ;phi[esi] -= eax
add esi, edi ;next
cmp esi, limit ;limit reached?
jle phi_inner ;if not, repeat
sum:
mov ebx, [phi + 4 * edi] ;add phi[edi] to result
add rcx, rbx
jmp phi_outer ;continue with outer loop
finished: ;convert to string, print and exit
mov rax, rcx
mov ebx, 10
mov ecx, 19
convert_result:
xor rdx, rdx
div rbx
add dl, '0'
mov [result + ecx], dl
dec ecx
test rax, rax
jnz convert_result
print:
mov eax, 4
mov edi, 1
mov esi, result
mov edx, 22
syscall
exit:
mov eax, 1
xor edi, edi
syscall