The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: jwill22 on February 26, 2011, 05:02:19 PM

Title: For Loop To Assembly
Post by: jwill22 on February 26, 2011, 05:02:19 PM
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
Title: Re: For Loop To Assembly
Post by: qWord on February 26, 2011, 05:25:00 PM
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

Title: Re: For Loop To Assembly
Post by: 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
Title: Re: For Loop To Assembly
Post by: jwill22 on February 26, 2011, 05:37:04 PM
So what's the xor for?
Title: Re: For Loop To Assembly
Post by: dedndave on February 26, 2011, 05:39:00 PM
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
Title: Re: For Loop To Assembly
Post by: qWord on February 26, 2011, 05:48:11 PM
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 :-)
Title: Re: For Loop To Assembly
Post by: jwill22 on February 26, 2011, 06:02:51 PM
So why was my answer not 650?
Title: Re: For Loop To Assembly
Post by: qWord on February 26, 2011, 06:07:37 PM
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

Title: Re: For Loop To Assembly
Post by: dedndave on February 26, 2011, 07:47:05 PM
Quoteomg :-)
saves a byte   :P
although, MOV EAX,0 might be faster
Title: Re: For Loop To Assembly
Post by: Shooter on February 26, 2011, 08:32:28 PM
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?
Title: Re: For Loop To Assembly
Post by: tenkey on February 26, 2011, 09:00:03 PM
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.
Title: Re: For Loop To Assembly
Post by: clive on February 26, 2011, 10:09:40 PM
        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
Title: Re: For Loop To Assembly
Post by: MichaelW on February 26, 2011, 10:27:27 PM
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
Title: Re: For Loop To Assembly
Post by: dedndave on February 27, 2011, 01:07:14 PM
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
Title: Re: For Loop To Assembly
Post by: lingo on February 27, 2011, 01:39:47 PM
"here is an even simpler version - small code, too"

It is smaller: :lol
mov  eax, 28Ah
call WriteDec
Title: Re: For Loop To Assembly
Post by: Shooter on February 27, 2011, 01:41:51 PM
So, why IS he getting 650 as an answer in his original sample?
Title: Re: For Loop To Assembly
Post by: clive on February 27, 2011, 03:39:10 PM
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
Title: Re: For Loop To Assembly
Post by: jwill22 on March 07, 2011, 04:48:57 PM
Sorry for being so late...but thanks you all..You really helped me out :dance: :dance: :dance: