-
Notifications
You must be signed in to change notification settings - Fork 0
/
092.asm
79 lines (68 loc) · 2.13 KB
/
092.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
format ELF64 executable 9
segment readable writable
result: times 10 db 0 ;empty string for printing the result later
db 10, 0
chains: rb 71 ;578 bits for "89 flags"
segment readable executable
entry start
start:
xor edi, edi ;init n
mov esi, 10 ;for divisions
xor ebx, ebx ;result
first567:
inc edi ;next n
cmp edi, 567 ;n > 567?
jg next ;if yes jump to next
mov eax, edi ;number in eax for dss
getchains:
xor ecx, ecx ;for the digit-square-sums
call dss
mov eax, ecx ;sum back in eax
cmp eax, 1 ;is it 1?
je first567 ;if yes, try next number
cmp eax, 89 ;is it 89?
jne getchains ;if not, continue chain
inc ebx ;if yes, increase count
bts [chains], edi ;set chains[n] to 1
jmp first567 ;and try next number
next:
cmp edi, 10000000 ;limit reached
je finished ;if yes, we are finished
xor ecx, ecx ;reset digit-square-sum
mov eax, edi ;number in eax for dss
call dss ;get dss of n
inc edi ;and increase n
bt [chains], ecx ;is it a "89 chain"?
jnc next ;if not, try next number
inc ebx ;else increase count
jmp next ;and try next number
dss:
xor edx, edx ;reset remainder
div esi ;divide by 10
imul edx, edx ;square remainder
add ecx, edx ;add to sum
test eax, eax ;repeat until eax is 0
jnz dss
ret
finished:
mov eax, ebx
mov ebx, 10
mov ecx, 9
convert_result:
xor edx, edx
div ebx
add edx, '0'
mov [result + ecx], dl
dec ecx
test eax, eax
jnz convert_result
print:
mov eax, 4
mov edi, 1
mov esi, result
mov edx, 12
syscall
exit:
mov eax, 1
xor edi, edi
syscall