So I'm trying to sum 1 + 11 + 111 given a number, n, which determines how many values of the series I add together. i.e n = 2, I will add 1 + 11, or n = 3, I will add 1 + 11 + 111. I have written the function in C already, but I am trying to translate it into x86 assembly and I am having trouble. Here is the C function:
int summation(int n)
{
int sum = 0, j = 1;
for (int i = 1; i <= n; i++)
{
sum = sum + j;
// Appending a 1 at the end
j = (j * 10) + 1;
}
return sum;
Here is my x86 assembly code:
unsigned int seriesSum(int n)
{
unsigned int sum=0;
__asm
{
mov ebx, n //input n
mov eax, 1
mov ecx, 10
mov edx, 0
Begin:
cmp ebx, 0 // determines end of loop or not
je EndOfWhile
add edx, edx
add edx, eax
mul ecx
add eax, eax
inc eax
dec ebx
jmp Begin //Loop back
EndOfWhile:
mov sum, edx
}
return sum;
I thought I have it translated correctly but I seem to be getting 0s as my sum.