The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: frktons on September 06, 2010, 10:18:15 PM

Title: MOV and expression source
Post by: frktons on September 06, 2010, 10:18:15 PM
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
Title: Re: MOV and expression source
Post by: 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.
Title: Re: MOV and expression source
Post by: 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, [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
Title: Re: MOV and expression source
Post by: zemtex on September 06, 2010, 10:58:33 PM
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.
Title: Re: MOV and expression source
Post by: Antariy on September 06, 2010, 11:00:03 PM
Frank this is link to thread which contain both documents: "http://www.asmcommunity.net/board/index.php?topic=25770.0"

This is link to part2 directly: "http://www.asmcommunity.net/board/index.php?action=dlattach;topic=25770.0;attach=2257"



Alex
Title: Re: MOV and expression source
Post by: 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
Title: Re: MOV and expression source
Post by: frktons on September 06, 2010, 11:03:14 PM
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?
Title: Re: MOV and expression source
Post by: Antariy on September 06, 2010, 11:04:58 PM
Frank, see 2 posts above - I give link to document which you request :P



Alex
Title: Re: MOV and expression source
Post by: 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
Title: Re: MOV and expression source
Post by: Antariy on September 06, 2010, 11:07:43 PM
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
Title: Re: MOV and expression source
Post by: 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



Title: Re: MOV and expression source
Post by: frktons on September 06, 2010, 11:21:34 PM
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
Title: Re: MOV and expression source
Post by: Antariy on September 06, 2010, 11:22:13 PM
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
Title: Re: MOV and expression source
Post by: 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
Title: Re: MOV and expression source
Post by: frktons on September 06, 2010, 11:38:46 PM
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
Title: Re: MOV and expression source
Post by: Magnum on September 07, 2010, 02:03:34 AM
Quote from: zemtex on September 06, 2010, 10:58:33 PM
Quote from: frktons on September 06, 2010, 10:43:45 PM
Quote from: zemtex on September 06, 2010, 10:24:51 PM


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.

Lea is more versatile than that.
LEA can multiply a register by 2,3,4,5,8, and 9.


lea          ebx,[ebx+ebx*8]

Title: Re: MOV and expression source
Post by: zemtex on September 07, 2010, 12:26:49 PM
Quote from: Magnum on September 07, 2010, 02:03:34 AM
Quote from: zemtex on September 06, 2010, 10:58:33 PM
Quote from: frktons on September 06, 2010, 10:43:45 PM
Quote from: zemtex on September 06, 2010, 10:24:51 PM


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.

Lea is more versatile than that.
LEA can multiply a register by 2,3,4,5,8, and 9.


lea          ebx,[ebx+ebx*8]


The use of constants when multiplying must be 2, 4 or 8.
Title: Re: MOV and expression source
Post by: frktons on September 07, 2010, 12:32:48 PM
Quote from: zemtex on September 06, 2010, 10:24:51 PM
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.

Lea is more versatile than that.
LEA can multiply a register by 2,4 or 8.

lea          ebx,[ebx+ebx*8]

The use of constants when multiplying must be 2, 4 or 8.

:lol Magnum is the name on an icecream and he was so sweet and cold to add
some odd numbers to yours.  :lol

Now I've make the sentence in bold characters, just to remember it.  :U

Frank
Title: Re: MOV and expression source
Post by: Tedd on September 08, 2010, 02:04:57 PM
The 'scale' value can only 2, 4, or 8, but you may also use lea to get the effect of multiplying by 3, 5, and 9.

lea eax,[2*eax + eax]  { (2*n + n) = 3*n }
lea eax,[4*eax + eax]  { (4*n + n) = 5*n }
lea eax,[8*eax + eax]  { (8*n + n) = 9*n }


With a little extra work, you can also get 7 by copying the value, negating it, and then using that in the 'base' part.




Addressing form is [scale*index + base + displacement] -- all complex forms must be reducible to this form for the assembler to allow it. (Complex constant expressions are allowed only if they can be reduced to a single constant at assembly time, i.e. all values are known.)