-
Notifications
You must be signed in to change notification settings - Fork 0
/
003.asm
59 lines (50 loc) · 2.13 KB
/
003.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
section .data
msg db "%d", 10, 0 ;return string for printf (just the result)
section .text
extern printf
global main
main:
mov rbx, 600851475143 ;the number to factor
mov rcx, 2 ;initialize rcx for trial divisions
test rbx, 1 ;check if number is odd
jnz odd ;if it is, jump to odd
divide2:
mov rax, rbx ;put current number in rax as the dividend
xor rdx, rdx ;prepare rdx which will store the remainder
div rcx ;divide by 2
test rdx, rdx ;check if remainder is zero
jnz odd ;if not, continue with odd divisors
mov rbx, rax ;put result of last division in rbx
cmp rbx, 4 ;check if greater or equal to 4
jge divide2 ;if yes, continue divisions
cmovb rcx, rbx ;if not, the number has arrived at 2
;or the remainder is a prime greater than 2
jmp print ;and we are finished
odd:
mov rcx, 1 ;set rcx to 1 to prepare for odd divisors
incdiv:
add rcx, 2 ;increase by 2 (so we begin with 3)
divide:
mov rax, rbx ;put current number in rax as the dividend
xor rdx, rdx ;prepare rdx which will store the remainder
div rcx ;divide by current divisor
test rdx, rdx ;check if remainder is zero
jnz incdiv ;if not, jump to incdiv
mov rbx, rax ;put result of last division in rbx
mov rax, rcx ;put current divisor in rax
mul rax ;square of current divisor (rax * rax)
cmp rax, rbx ;check if less or equal to current dividend
jle divide ;if yes, continue divisions
cmp rcx, rbx ;else compare last factor with dividend
cmovb rcx, rbx ;if the factor is greater, print that
print: ;printing routine, differs slightly from OS to OS
push rbp
mov rdi, msg
mov rsi, rcx
call printf
pop rbp
exit: ;exit routine, dito
mov rax, 1
xor rdi, rdi
syscall
section .note.GNU-stack ;just for gcc