-
Notifications
You must be signed in to change notification settings - Fork 0
/
048.asm
48 lines (40 loc) · 1.27 KB
/
048.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
section .data
msg db "%lld", 10, 0 ;return string for printf (just the result)
section .text
extern printf
global main
main:
mov rbx, 10000000000 ;for mod 1e10
xor rdi, rdi ;number
xor rcx, rcx ;sum
next:
inc rdi ;next number
cmp rdi, 1000 ;finished?
jg finished ;if yes, jump to finished
mov rax, rdi ;copy number in rax
mov rsi, 1 ;exponent count
power:
mul rdi ;multiply
xor rdx, rdx ;reset remainder
div rbx ;divide by 1e10
mov rax, rdx ;move remainder in rax
inc rsi ;next iteration
cmp rsi, rdi ;power finished?
jl power ;if not, continue
add rcx, rax ;else add result to sum
jmp next ;and jump to next
finished:
mov rax, rcx ;sum in rax
xor rdx, rdx ;reset remainder
div rbx ;divide by 1e10
print: ;printing routine, differs slightly from OS to OS
push rbp
mov edi, msg
mov rsi, rdx
call printf
pop rbp
exit: ;exit routine, dito
mov eax, 1
xor edi, edi
syscall
section .note.GNU-stack ;just for gcc