-
Notifications
You must be signed in to change notification settings - Fork 0
/
058.asm
105 lines (87 loc) · 2.15 KB
/
058.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
102
103
104
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:
mov eax, 1 ;number on the diagonal
xor ebx, ebx ;counter
xor edi, edi ;difference between numbers on the diagonal
next_layer:
add edi, 2 ;increase difference by 2
mov esi, 4 ;loop counter (4 corners)
layer_loop:
dec esi ;decrease loop counter
add eax, edi ;add difference to eax
test esi, esi
jz done ;skip last number (is always square)
push rax
call is_prime ;is it prime
pop rax
test ecx, ecx
jz no_prime
inc ebx ;if yes, increase count
no_prime:
test esi, esi ;loop counter > 0?
jnz layer_loop ;if yes, repeat
done:
push rax ;else save rax and rbx on the stack
push rbx
get_ratio:
mov eax, ebx ;get ratio
imul eax, 100
mov ebx, edi
shl ebx, 1
inc ebx
xor edx, edx
div ebx
cmp eax, 10 ;ratio >= 10?
pop rbx
pop rax
jge next_layer ;if yes, continue with next layer
inc edi ;else get side length and print that
finished:
mov eax, edi
mov ebx, 10
mov ecx, 19
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, 22
syscall
exit:
mov eax, 1
xor edi, edi
syscall
is_prime:
mov ecx, 1
test eax, 1
jz not_prime
mov r8d, eax
mov r9d, 1
inc_div:
add r9d, 2
mov eax, r9d
mul r9d
cmp eax, r8d
jg back
divide:
mov eax, r8d
xor edx, edx
div r9d
test edx, edx
jnz inc_div
not_prime:
xor ecx, ecx
back:
ret