-
Notifications
You must be signed in to change notification settings - Fork 0
/
030.asm
51 lines (44 loc) · 1.63 KB
/
030.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
section .data
msg db "%d", 10, 0 ;return string for printf (just the result)
section .text
extern printf
global main
main:
xor ecx, ecx ;total
mov edi, 10 ;for divisions by 10 to get the digits
mov ebx, 354295 ;6 * 9^5 as upper limit
nextnumber:
dec ebx
cmp ebx, 1 ;arrived at 1?
je print ;if yes, print result
xor esi, esi ;reset sum of digits
mov eax, ebx ;copy number in eax
getsum:
xor edx, edx ;reset remainder
div edi ;divide by 10
push rax ;reduced number on the stack
mov eax, edx ;remainder (last digit) in eax
push rdx ;digit on the stack
mul eax ;digit^2
mul eax ;digit^4
pop rdx ;digit back from the stack
mul edx ;digit^5
add esi, eax ;add to sum
pop rax ;partial number back from the stack
test eax, eax ;check if number was reduced to 0
jnz getsum ;if not, repeat
cmp ebx, esi ;sum = number
jne nextnumber ;if not, try next number
add ecx, esi ;else add sum to toal
jmp nextnumber ;and try next number
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