News:

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

Adding 2 or more variable

Started by Loner, May 13, 2012, 04:18:09 PM

Previous topic - Next topic

Loner

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.

qWord

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

Loner

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] ?

qWord

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

Loner

Do you have any suggestion of tutorial? I can't get some good tutorial or notes.

qWord

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

dedndave

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

Loner