-
Notifications
You must be signed in to change notification settings - Fork 0
/
094.asm
101 lines (91 loc) · 2.47 KB
/
094.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
format ELF64 executable 9
segment readable writable
result: times 10 db 0 ;empty string for printing the result later
db 10, 0
segment readable executable
entry start
start:
mov rdi, 2 ;x
mov rsi, 1 ;y
xor r8, r8 ;result
mov r9, 3 ;for mod 3
getresult: ;start with the plus one case
mov rbx, rdi ;get (2 * x) - 1
shl rbx, 1
dec rbx
cmp rbx, 1000000000 ;limit reached?
jge finished ;if yes, we are finished
mov rcx, rdi ;get y * (x - 2)
sub rcx, 2
imul rcx, rsi
cmp rbx, 0 ;(2 * x) - 1 > 0?
jle minus_1 ;if not, try minus one case
cmp rcx, 0 ;y * (x - 2) > 0?
jle minus_1 ;if not, try minus one case
mov rax, rcx
xor rdx, rdx
div r9
test rdx, rdx ;y * (x - 2) mod 3 = 0?
jnz minus_1 ;if not, try minus one case
mov rax, rbx
xor rdx, rdx
div r9
test rdx, rdx ;(2 * x) - 1 mod 3 = 0?
jnz minus_1 ;if not, try minus one case
inc rbx ;add (2 * x) to result
add r8, rbx
minus_1:
mov rbx, rdi ;get (2 * x) + 1
shl rbx, 1
inc rbx
mov rcx, rdi ;get y * (x + 2) and continue like above
add rcx, 2
imul rcx, rsi
cmp rbx, 0
jle next_xy
cmp rcx, 0
jle next_xy
mov rax, rcx
xor rdx, rdx
div r9
test rdx, rdx
jnz next_xy
mov rax, rbx
xor rdx, rdx
div r9
test rdx, rdx
jnz next_xy
dec rbx
add r8, rbx
next_xy:
push rdi
shl rdi, 1 ;next x = (2 * x) + (3 * y)
mov rax, rsi
imul rax, 3
add rdi, rax
pop rax ;next y = (2 * y) + x
shl rsi, 1
add rsi, rax
jmp getresult
finished:
mov rax, r8
mov rbx, 10
mov ecx, 9
convert_result:
xor rdx, rdx
div rbx
add rdx, '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, 12
syscall
exit:
mov eax, 1
xor edi, edi
syscall