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
|
; What is the largest prime factor of the number 600851475143 ?
extern printf
SECTION .data
flag: db "%d",10,0 ; "%d\n\0"
SECTION .text
global main
main:
push rbp ; set up stack
mov rbp, rsp
mov rcx, 2 ; smallest prime factor: 2
mov rax, 0x8BE589EAC7 ; target = 600851475143
.loop xor rdx, rdx ; clear rdx (remainder)
push rax
div rcx ; divide rdx:rax/rcx
cmp rdx, 0 ; compare only remainder
jnz .next ; if r = 0 , its a factor
; we already divided the number so
; we just need to save the value in rax
; as the new number to factorize
add rsp, 8 ; delete the old
push rax
mov rcx, 1 ; reset the counter (normally would start at 2,
; but we increment anyway
.next pop rax ; refresh value
inc rcx ; factor ++
cmp rax, rcx ; while rax > rcx number > factor
; i.e. not a prime factor
jg .loop
mov rdi, flag ; arg 1 (format)
mov rsi, rcx ; arg 2 (value)
mov rax, 0 ; no xmm registers
call printf wrt ..plt
mov rsp, rbp ; reset to the top of the stack
pop rbp
mov rax, 0
ret
|