The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Loner on May 13, 2012, 04:18:09 PM

Title: Adding 2 or more variable
Post by: Loner on May 13, 2012, 04:18:09 PM
I'm new to assembly language, and i need help on adding 2 or more variable together.

First, we capture user input by >>>invoke crt_printf, addr displayData, temp, Data1
Then, as we capture finish all value, we going to add all value
My problem is, is there any way to add up all value as in C programming, like total = a + b+ c + d?

This is my source code

.data
    data db "Insert value for input %d:", 0
    tempData db "%d", 0
   
    Data1 dd  0
    Data2 dd  0
    Data3 dd  0
    Data4 dd  0
    total sdword  0
    count dd  1
   
.code

start:
    invoke crt_printf, addr data, count       
    invoke crt_scanf, addr tempData, addr Data1
    inc count
    invoke crt_printf, addr data, count       
    invoke crt_scanf, addr tempData, addr Data2
    inc count
    invoke crt_printf, addr data, count       
    invoke crt_scanf, addr tempData, addr Data3
    inc count
    invoke crt_printf, addr data, count       
    invoke crt_scanf, addr tempData, addr Data4


Thank you.
Title: Re: Adding 2 or more variable
Post by: qWord on May 13, 2012, 04:31:37 PM
the common way would be:
mov eax,Data1
add eax,Data2
add eax,Data3
add eax,Data4
mov total,eax


or more dynamically:

mov eax,Data1[0]
xor ecx,ecx
.while ecx < count
    add eax,Data1[ecx*4]
    lea ecx,[ecx+1]
.endw

(count should be initialized with 0 and not 1)
Title: Re: Adding 2 or more variable
Post by: Loner on May 13, 2012, 04:43:58 PM
1. Can you please explain why we need to move data1 into eax first? Because i try:
    add total,Data1
    add total,Data2
    add total,Data3
    add total,Data4
But it fail.

2.If i change eax to other variable, it fail to process, is eax a special operand?

3.Is the inc ecx same as lea ecx,[ecx+1] ?
Title: Re: Adding 2 or more variable
Post by: qWord on May 13, 2012, 05:05:46 PM
Quote from: Loner on May 13, 2012, 04:43:58 PM
1. Can you please explain why we need to move data1 into eax first?
In the x86 Architecture, instruction can only have one memory operand**

Quote from: Loner on May 13, 2012, 04:43:58 PM2.If i change eax to other variable, it fail to process, is eax a special operand?
EAX is a (genreal purpose) 32bit register. Other registers are (f.e.) ECX,EDX,EBX,ESI,EDI,EBP,ESP.

Quote from: Loner on May 13, 2012, 04:43:58 PM
3.Is the inc ecx same as lea ecx,[ecx+1] ?
LEA does not modify the flags. INC would also work here.

You should read a tutorial!


** there are some exception: e.g. the stack intructions, which have an implicit memory operand
Title: Re: Adding 2 or more variable
Post by: Loner on May 13, 2012, 05:19:06 PM
Do you have any suggestion of tutorial? I can't get some good tutorial or notes.
Title: Re: Adding 2 or more variable
Post by: qWord on May 13, 2012, 05:24:21 PM
MadWizard's tutorial: win32asmtutorial (http://www.madwizard.org/programming/tutorials/)
Title: Re: Adding 2 or more variable
Post by: dedndave on May 13, 2012, 06:51:24 PM
makes no sense
1) INC does affect flags - just not the carry flag   :P
2) the carry flag doesn't matter, because you are using ADD
3) and - you have no place to carry it to - lol
4) Data1[0] seems to be added twice, as it is used as an initializer

if you want to allow for big numbers, maybe this would work

xor ecx,ecx
xor eax,eax
xor edx,edx
.while ecx < count
    add eax,Data1[ecx*4]
    inc ecx
    adc edx,0
.endw
Title: Re: Adding 2 or more variable
Post by: Loner on May 14, 2012, 09:42:44 AM
Thank for your sharing