Trying to convert this:
int sum = 0;
for(int i = 0; i <=25;i++)
{
sum = (i * 2) + sum;
}
cout << sum <<endl;
to assembly.
Here's What I got...the answer is 650 in c++ and my answer is 675. Can somebody tell me what I'm doing wrong or if I'm doing anything wrong. Thanks:D:D
INCLUDE Irvine32.inc
.data
sum DWORD 0
counter DWORD 0
.code
main PROC
mov ebx,0
mov ecx, 25
.while(counter <= 25)
mov eax,2
mov ebx,counter
mul ebx
add sum,eax
add ebx,sum
inc counter
.ENDW
mov eax,ebx
call WriteDec
look at:
add eax,ebx
...
mov eax,ebx
call WriteDec
here an variant without MUL:
xor ecx,ecx
xor ebx,ebx
.while SDWORD ptr ecx <= 25
lea ebx,[ecx*2+ebx]
lea ecx,[ecx+1]
.endw
;ebx = result
xor eax,eax
mov ecx,26
loop00: mov edx,ecx
dec edx
shl edx,1
add eax,edx
loop loop00
call WriteDec
So what's the xor for?
XOR EAX,EAX is the same as SUB EAX,EAX - it zero's the EAX register, but is smaller than MOV EAX,0
sometimes, you may want to preserve the flags, though
in that case
push 0
pop eax
Quote from: dedndave on February 26, 2011, 05:39:00 PM
sometimes, you may want to preserve the flags, though
in that case
push 0
pop eax
omg :-)
So why was my answer not 650?
Quote from: jwill22 on February 26, 2011, 05:02:19 PM
.while(counter <= 25)
mov eax,2
mov ebx,counter
mul ebx
add sum,eax
add ebx,sum ; ebx = counter*2 + counter*2 + sum
inc counter
.ENDW
mov eax,ebx ; <--- this should be mov eax,sum
call WriteDec
Quoteomg :-)
saves a byte :P
although, MOV EAX,0 might be faster
Quote from: dedndave on February 26, 2011, 05:27:29 PM
xor eax,eax
mov ecx,26
loop00: mov edx,ecx
dec edx
shl edx,1
add eax,edx
loop loop00
call WriteDec
Dave,
Wouldn't the loop count down to ECX=0? Wouldn't that make it 27 iterations instead of 26?
ECX is decremented at the loop instruction and the *result* is tested for zero.
That means the looped code is NOT executed when ECX is zero.
Thus it's 26-count.
xor eax,eax
mov ecx,26
loop00: lea edx,[ecx*2 - 2]
add eax,edx
loop loop00
call WriteDec
or
xor eax,eax
mov ecx,26
loop00: lea eax,[eax + ecx*2 - 2]
dec ecx
jnz loop00
call WriteDec
or
xor eax,eax
mov ecx,26
loop00: dec ecx
lea eax,[eax + ecx*2]
jnz loop00
call WriteDec
And of course the final iteration is pointless as it adds ZERO
Not very compact, but at least it's clear how it works:
.data
sum dd 0
.code
FOR i,<0,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>
mov eax, i
shl eax, 1
add sum, eax
ENDM
here is an even simpler version - small code, too :P
push 25
xor eax,eax
pop ecx
loop00: add eax,ecx
add eax,ecx
loop loop00
call WriteDec
"here is an even simpler version - small code, too"
It is smaller: :lol
mov eax, 28Ah
call WriteDec
So, why IS he getting 650 as an answer in his original sample?
Quote from: Shooter on February 27, 2011, 01:41:51 PM
So, why IS he getting 650 as an answer in his original sample?
Because the C/C++ code is correct, the first assembler version adds the sum to ebx which it doesn't need too, and then prints the value in ebx instead of the one in sum.
Or am I missing your point?
To extend Lingo's solution for different values of N, we could make a table. The point however is to understand the math.
Sum at each iteration :
0 : 0
1 : 2
2 : 6
3 : 12
4 : 20
5 : 30
6 : 42
7 : 56
8 : 72
9 : 90
10 : 110
11 : 132
12 : 156
13 : 182
14 : 210
15 : 240
16 : 272
17 : 306
18 : 342
19 : 380
20 : 420
21 : 462
22 : 506
23 : 552
24 : 600
25 : 650
Where he got 675 was from iteration ebx = 25, ebx += sum (650), ebx = 675
Sorry for being so late...but thanks you all..You really helped me out :dance: :dance: :dance: