Sean Heiss
Sean Heiss

Reputation: 780

"inc ecx" Instruction operands must be the same size?

I am trying to write a little program in MASM. On one line, when I try increasing ECX, I get this error... It doesn't seem to make any sense, since there is only one operand!

Here is the relevant code:

mov ecx, eax
lea eax, DWORD PTR [ecx]
lea ecx, BYTE PTR [eax+4]
inc ecx

In the beginning, EAX is just the length of a string, in this 0x05.

So, does anyone know why this is happening? Thanks!

Upvotes: 0

Views: 1210

Answers (2)

Michael
Michael

Reputation: 58467

BYTE PTR makes little sense in this context, and can safely be removed (it doesn't cause any errors for me though). LEA computes effective addresses, which can also be used to perform some general arithmetic.

What the code snippet does is this:

ecx = eax
eax = ecx
ecx = eax + 4
ecx++

Which could have been replaced by a single instruction:

lea ecx,[eax+5]

Upvotes: 0

Drew McGowen
Drew McGowen

Reputation: 11706

You can actually replace the last two lines (that you gave) with:

lea ecx, BYTE PTR [eax+5]

LEA gets the address of an operand, so for BYTE PTR [eax+4] that would just be the value eax+4, which gets stored in ecx. Since you're incrementing afterwards, you can just combine the two additions into one, so you can use BYTE PTR [eax+5] instead.

Upvotes: 1

Related Questions