-
Notifications
You must be signed in to change notification settings - Fork 0
/
016.asm
66 lines (56 loc) · 2 KB
/
016.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
section .data
msg db "%d", 10, 0 ;return string for printf (just the result)
num times 17 dq 0 ;1088 bits for 2^1000 + extra
section .bss
e18 equ 1000000000000000000 ;1e18 for splitting the number
section .text
extern printf
global main
main:
xor rbx, rbx ;carry
mov edi, 16 ;array index
mov rcx, e18 ;for divisions
mov esi, 1000 ;power counter
mov qword [num + 8 * edi], 1 ;initial state
power:
dec esi ;decrease counter
mov edi, 17 ;reset index
multiply:
dec edi
xor rdx, rdx ;reset remainder
mov rax, [num + 8 * edi] ;put current digit in eax
shl rax, 1 ;double
add rax, rbx ;add carry
div rcx ;divide by 1e18
mov rbx, rax ;carry = result
mov [num + 8 * edi], rdx ;num @ edi = remainder
test edi, edi ;check if index >= 0
jnz multiply ;if yes, repeat
test esi, esi ;check if completed
jnz power ;if not, jump to power
xor rax, rax ;else reset registers
xor ecx, ecx
mov rbx, 10
mov edi, 17
sum: ;sum all digits
dec edi
mov rax, [num + 8 * edi]
sumloop:
xor rdx, rdx
div rbx
add ecx, edx
test rax, rax
jnz sumloop
test edi, edi
jnz sum
print: ;printing routine, differs slightly from OS to OS
push rbp
mov edi, msg
mov esi, ecx
call printf
pop rbp
exit: ;exit routine, dito
mov eax, 1
xor edi, edi
syscall
section .note.GNU-stack ;just for gcc