RosettaCodeData/Task/Iterated-digits-squaring/X86-Assembly/iterated-digits-squaring.x86

53 lines
1.2 KiB
Plaintext

section .data
count dd 0
section .text
global _main
_main:
mov ecx, 1
looping:
mov eax, ecx ;pass parameter in eax
push ecx
call doMath
pop ecx
add [count], eax ;doMath returns 0 or 1 in eax
inc ecx
cmp ecx, 100000001
jl looping
mov eax, count ;returns memory address of count
ret
addSquaredDigits:
push ebx
mov ebx, 0
mov esi, 10
looping2:
xor edx, edx ;clear edx for division
div esi ;div by 10 to get last digit in edx
mov ecx, eax ;save parameter
mov eax, edx ; get last digit
mul eax ;square last digit
add ebx, eax ;add the square to the result
jecxz aSDend ;if the parameter is 0 we have all digits
mov eax, ecx ;restore parameter before looping
jmp looping2
aSDend:
mov eax, ebx ;move result to return register
pop ebx
ret
doMath:
looping3:
call addSquaredDigits ;do until eax is 89 or 1
cmp eax, 89
je ret1
cmp eax, 1
je ret0
jmp looping3
ret1: ;if eax == 89 we return 1 -> inc count
mov eax, 1
ret
ret0: ;if eax == 1 we return 0 -> don't inc count
mov eax, 0
ret