News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

For Loop To Assembly

Started by jwill22, February 26, 2011, 05:02:19 PM

Previous topic - Next topic

jwill22

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

qWord

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

FPU in a trice: SmplMath
It's that simple!

dedndave

        xor     eax,eax
        mov     ecx,26

loop00: mov     edx,ecx
        dec     edx
        shl     edx,1
        add     eax,edx
        loop    loop00

        call    WriteDec

jwill22


dedndave

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

qWord

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 :-)
FPU in a trice: SmplMath
It's that simple!

jwill22

So why was my answer not 650?

qWord

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

FPU in a trice: SmplMath
It's that simple!

dedndave

Quoteomg :-)
saves a byte   :P
although, MOV EAX,0 might be faster

Shooter

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?
Never use direct references to anything ever. Bury everything in
macros. Bury the macros in include files. Reference those include
files indirectly from other include files. Use macros to reference
those include files.

tenkey

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.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

clive

        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
It could be a random act of randomness. Those happen a lot as well.

MichaelW

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
eschew obfuscation

dedndave

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

lingo

"here is an even simpler version - small code, too"

It is smaller: :lol
mov  eax, 28Ah
call WriteDec