News:

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

MOV and expression source

Started by frktons, September 06, 2010, 10:18:15 PM

Previous topic - Next topic

frktons

What possibility offers MASM to have simple or complex source expression operands
in MOV instruction?

Can I do:


mov eax, 13 * 127 + 12


or something more complex, or the source operand can never be an expession ?

I've seen we can do:


mov eax, [ebx + 16]


and I wonder if it can be done for operands different than registers, and to what
extent. And the syntax that we should use in case it is possible?

Thanks

Frank
Mind is like a parachute. You know what to do in order to use it :-)

zemtex

You can use a various combination of constants, the assembler will calculate it for you and put it into the executable. But you cant use registers, that requires the use of special instructions like MUL or DIV.

An optimization/simplification trick is to use LEA to calculate different number variations.

Read http://www.asmcommunity.net/board/index.php?action=dlattach;topic=25770.0;attach=2256 this for further information.
I have been puzzling with lego bricks all my life. I know how to do this. When Peter, at age 6 is competing with me, I find it extremely neccessary to show him that I can puzzle bricks better than him, because he is so damn talented that all that is called rational has gone haywire.

frktons

Quote from: zemtex on September 06, 2010, 10:24:51 PM
You can use a various combination of constants, the assembler will calculate it for you and put it into the executable. But you cant use registers, that requires the use of special instructions like MUL or DIV.

An optimization/simplification trick is to use LEA to calculate different number variations.

Read http://www.asmcommunity.net/board/index.php?action=dlattach;topic=25770.0;attach=2256 this for further information.

Very interesting, thanks.

So I could do something like:

mov ebx, 12
lea    eax, [ebx +13 * 127]
?


This document is part 1 for using lea, do you know if there is also a part 2/...?

Well, I found it. NO problem.

Frank
Mind is like a parachute. You know what to do in order to use it :-)

zemtex

Quote from: frktons on September 06, 2010, 10:43:45 PM
Quote from: zemtex on September 06, 2010, 10:24:51 PM
You can use a various combination of constants, the assembler will calculate it for you and put it into the executable. But you cant use registers, that requires the use of special instructions like MUL or DIV.

An optimization/simplification trick is to use LEA to calculate different number variations.

Read http://www.asmcommunity.net/board/index.php?action=dlattach;topic=25770.0;attach=2256 this for further information.

Very interesting, thanks.

So I could do something like:

mov ebx, 12
lea    eax, 13 * 127
add   eax, ebx
?


This document is part 1 for using lea, do you know if there is also a part 2/...?

Frank

You could replace "lea eax, 13 * 127" with an ordinary move like:
mov eax, 13 * 127... Thats not a problem at all. As long as you use known constants that the assembler can precalculate at assembly time, it does not produce a problem. Since registers are unknown in assembly time you cannot use registers in replacement of constants in the example above.

About the latter document, I do not know (Sorry about that).

Consider LEA as a "trickster", it is not meant to be used regularly for that purpose. You can only multiply registers with 2, 4 and 8 using lea. It is just an option if you want to simplify things every now and then.

Here are a few examples.

These are perfectly legal:

mov eax, 10 + 10
mov eax, 10 * 20 * 30 * 40 * 50 / 100
mov eax, 100 - 20 / 40

These are not legal:

mov eax, 10 + ecx
mov eax, ecx - 100 / 20
mov eax, ecx + 1

And yes, both examples in the first post of yours are legal.
I have been puzzling with lego bricks all my life. I know how to do this. When Peter, at age 6 is competing with me, I find it extremely neccessary to show him that I can puzzle bricks better than him, because he is so damn talented that all that is called rational has gone haywire.

Antariy


redskull

One of the most subtle yet important distinctions to make when learning ASM is to differentiate between what the assembler is doing for you at build time and what the CPU is doing at run-time.  I spent many nights banging my head against the wall trying to figure out why something wouldn't work.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

frktons

Quote from: redskull on September 06, 2010, 11:01:15 PM
One of the most subtle yet important distinctions to make when learning ASM is to differentiate between what the assembler is doing for you at build time and what the CPU is doing at run-time.  I spent many nights banging my head against the wall trying to figure out why something wouldn't work.

-r

I suppose you mean these expressions are calculated at assemble time, not at runtime?

They become constant numbers for the entire program, don't they?
Mind is like a parachute. You know what to do in order to use it :-)

Antariy

Frank, see 2 posts above - I give link to document which you request :P



Alex

frktons

Quote from: Antariy on September 06, 2010, 11:04:58 PM
Frank, see 2 posts above - I give link to document which you request :P
Alex


Thanks Alex, I had already found it searching with Google.  :P
Frank
Mind is like a parachute. You know what to do in order to use it :-)

Antariy

Quote from: frktons on September 06, 2010, 11:05:50 PM
Quote from: Antariy on September 06, 2010, 11:04:58 PM
Frank, see 2 posts above - I give link to document which you request :P
Alex


Thanks Alex, I had already found it searching with Google.  :P
Frank

But I'm found it not in Google :P


Alex

redskull

Quote from: frktons on September 06, 2010, 11:03:14 PM
They become constant numbers for the entire program, don't they?

Yes, but sometimes it's not as clear cut.  For example, the following is a completly legal statment:

MOV EAX, [EBX+30+ECX*(2*2+4)+100*10-60]

-r



Strange women, lying in ponds, distributing swords, is no basis for a system of government

frktons

Quote from: redskull on September 06, 2010, 11:14:48 PM

Yes, but sometimes it's not as clear cut.  For example, the following is a completly legal statment:

MOV EAX, [EBX+30+ECX*(2*2+4)+100*10-60]

-r

This brings another question: precedence of operations in expressions.
this: (2*2+4) and this 100*10-60 are calculated from left to right anyway?
what happens if the operands are switched: (4+2*2) is the same of (2*2+4)?

What are the rules?

Frank
Mind is like a parachute. You know what to do in order to use it :-)

Antariy

Quote from: redskull on September 06, 2010, 11:14:48 PM
Quote from: frktons on September 06, 2010, 11:03:14 PM
They become constant numbers for the entire program, don't they?

Yes, but sometimes it's not as clear cut.  For example, the following is a completly legal statment:

MOV EAX, [EBX+30+ECX*(2*2+4)+100*10-60]

-r





Frank, dont swoon :) this is:

MOV EAX, [EBX+ECX*8+970]



Try make simple parsing, and you see what this is not very hard.


Alex

redskull

Quote from: frktons on September 06, 2010, 11:21:34 PM
What are the rules?

parenthesis, multiplecation/division, addition/subtraction.  So, 2*2+4 is the same as 4+2*2.  However, (2*2)+4 is NOT the same as 2*(2+4)

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

frktons

Quote from: redskull on September 06, 2010, 11:26:05 PM
Quote from: frktons on September 06, 2010, 11:21:34 PM
What are the rules?

parenthesis, multiplecation/division, addition/subtraction.  So, 2*2+4 is the same as 4+2*2.  However, (2*2)+4 is NOT the same as 2*(2+4)

-r

Good, usual priority as other languages :U

Thanks
Mind is like a parachute. You know what to do in order to use it :-)