-
Notifications
You must be signed in to change notification settings - Fork 0
/
055.asm
76 lines (64 loc) · 1.81 KB
/
055.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
format ELF64 executable 9
segment readable writable
result: times 20 db 0 ;empty string for printing the result later
db 10, 0
segment readable executable
entry start
start:
xor rdi, rdi ;init n
mov rbx, 10 ;10 in rbx for reverse loop
xor r8d, r8d ;counter
jmp next_n
count:
pop rdi
inc r8d
next_n:
inc rdi ;next n
cmp rdi, 10000 ;finished?
je finished
push rdi ;on the stack to save it
call reverse ;reverse n, result in rcx
xor rsi, rsi ;loop counter
lychrel:
inc rsi
cmp rsi, 50 ;50 iterations done?
je count ;if yes, we can increase the counter
add rdi, rcx ;add reverse to n
call reverse ;reverse result
cmp rdi, rcx ;result = reverse (i.e. a palindrome)?
jne lychrel ;if not, repeat
pop rdi ;else reset rdi
jmp next_n ;and try next number
reverse:
mov rax, rdi ;reverse rdi, result in rcx
xor rcx, rcx
reverse_loop:
imul rcx, rbx
xor rdx, rdx
div rbx
add rcx, rdx
test rax, rax
jnz reverse_loop
ret
finished: ;convert count to string, print and exit
mov eax, r8d
mov ebx, 10
mov ecx, 19
convert_result:
xor edx, edx
div ebx
add edx, '0'
mov [result + rcx], dl
dec ecx
test eax, eax
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