-
Notifications
You must be signed in to change notification settings - Fork 0
/
029.asm
52 lines (43 loc) · 1.51 KB
/
029.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
section .data
msg db "%d", 10, 0 ;return string for printf (just the result)
section .text
extern printf
global main
main:
xor ecx, ecx ;sum of duplicates
mov esi, 9801 ;99^2 (all terms incl. duplicates)
mov eax, 1 ;first base - 1
nextbase:
inc eax
cmp eax, 11
je result
mov r9d, eax ;copy base
mov edi, 1 ;power
powers:
mul r9d ;next power
cmp eax, 100 ;result > 100
jg finished ;if yes, finished with current base
inc edi ;else increase power
push rax ;current result on the stack
mov eax, 100 ;100 in eax for division
div edi ;divide by power (gives duplicates + 1)
dec eax ;result - 1
add ecx, eax ;add to total
pop rax ;back from the stack
jmp powers ;and continue with next power
finished:
mov eax, r9d ;reset base
jmp nextbase ;jump to nextbase
result:
dec ecx ;subtract 1 from total, need to find out why
sub esi, ecx ;subtract total from all terms
print: ;printing routine, differs slightly from OS to OS
push rbp
mov edi, msg
call printf
pop rbp
exit: ;exit routine, dito
mov eax, 1
xor edi, edi
syscall
section .note.GNU-stack ;just for gcc