News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Understanding on what makes a mov instruction valid or invalid

Started by RedSkeleton007, July 12, 2015, 07:29:33 AM

Previous topic - Next topic

RedSkeleton007

Okay, I have the following arrays for us to work with:

var 1 SBYTE -4, -2, 3, 1
var2 WORD 1000h, 2000h, 3000h, 4000h
var3 SWORD -16, -42
var4 DWORD 1, 2, 3, 4, 5

Using those values, here are some answer-key statements:
mov ax,var1 ;not valid REASON: I don't know. The first array element in var1 is -4. Is it because -4 is not 16-bit like register ax?
mov ax,var2 ;valid REASON: 1000h and register ax are both 16-bit.
mov eax,var3 ;not valid REASON: I don't know. Is it because -16 is not 32-bit like register eax is?
mov var2,var3 ;not valid REASON: I don't know. Is it because the var3 SWORD and var2 WORD intrinsic data types don't match?
movzx ax,var2 ;not valid REASON: I don't know.
movzx var2,al ;not valid REASON: I don't know. Is it because no value is initially stored in al to copy to var2's first array element?
mov ds,ax ;valid REASON: ds and ax are both 16-bit registers.
mov ds,1000h ;invalid REASON: I don't know. Both 1000h and ds are 16-bit, aren't they?

mov ax,[var3 - 2] ;4000h REASON: What value (-16, -42) in var3's array does [var3 - 2] point to? And where did 4000h come from?

jj2007

\Masm32\help\opcodes.chm is a fascinating lecture, highly recommended.

Short version: register size (e.g. ax=16 bit) and mem size must be the same, except for movzx.

RedSkeleton007

Quote from: jj2007 on July 12, 2015, 08:02:18 AM
\Masm32\help\opcodes.chm is a fascinating lecture, highly recommended.
Could you please provide an actual link (not just a path), pretty please? :bgrin:

Quote from: jj2007 on July 12, 2015, 08:02:18 AM
Short version: register size (e.g. ax=16 bit) and mem size must be the same, except for movzx.
Thanks, that clears almost everything up. Just a couple more questions (I will provide the arrays again):
var 1 SBYTE -4, -2, 3, 1
var2 WORD 1000h, 2000h, 3000h, 4000h
var3 SWORD -16, -42
var4 DWORD 1, 2, 3, 4, 5

mov ds,1000h ;invalid Both the 16-bit segment register ds and the value 1000h are 16-bit, aren't they? The 4 digits with the h at the end makes 1000h a 16-bit hexadecimal number, don't they? So why is this instruction invalid?

mov ax,[var3 - 2] ;4000h I still don't understand what the hell happened here. What value (-16, -42) in var3's array does [var3 - 2] point to? And where did 4000h come from?

jj2007

Quote from: RedSkeleton007 on July 13, 2015, 05:40:34 AM
Quote from: jj2007 on July 12, 2015, 08:02:18 AM
\Masm32\help\opcodes.chm is a fascinating lecture, highly recommended.
Could you please provide an actual link (not just a path), pretty please? :bgrin:

RTFM

rrr314159

Red,

You can't move an immediate value into segment register, only a reg or mem. I didn't know that either - hey, who uses segment registers these days? - so I looked it up in the reference jj2007 gave you, highly recommended. You should refer to it constantly. He didn't give a link because it's a file on your hard disk.

BTW the size of an immediate value, like 1000h, is quite a different thing than the size of a register. A register has a fixed size like 16 or 32 bits. An immediate value is an actual number, all that counts is it can't be larger than the register you're putting it into. So for 16-bit register you can use 1000h, or 23, or 0, or 0ffffh, as long as it's not more than 16 bits.

var3-2 points at the 2 bytes preceding var3, on the line above, namely 4000h.

Read the manual.
I am NaN ;)

RedSkeleton007

Quote from: rrr314159 on July 13, 2015, 10:48:37 AM
so I looked it up in the reference jj2007 gave you, highly recommended. You should refer to it constantly. He didn't give a link because it's a file on your hard disk.
What do you mean it's on my hard disk? :icon_confused: I would love to thoroughly read and use this so-called manual, but I still don't understand HOW to find it.

I don't think it's here:
http://www.webalice.it/jj2006/Masm32_Tips_Tricks_and_Traps.htm

or here:
http://kipirvine.com/asm/index6th.htm

I'm waiting for you fellas to start making sense. WHERE IS IT? :dazzled:

Quote from: rrr314159 on July 13, 2015, 10:48:37 AM
var3-2 points at the 2 bytes preceding var3, on the line above, namely 4000h.
var3 SWORD -16, -42

Well, the SWORD intrinsic data type makes the values in the array 16-bit. if we took out the -2 from var3-2, then var3 all by itself would point to the first element in the array, -16, by default. Of course, there's only two elements in the array, so -2 could only go backwards over -16 (two bytes back over a 16-bit or two-byte integer), leading the pointer to -42.

If all of that I just said is correct, is 4000h equivalent to -42 somehow?

hutch--

> Could you please provide an actual link (not just a path), pretty please?

No as a matter of fact, it is copyright software that is part of the MASM32 SDK. Download the SDK and you will find it in the HELP folder.

rrr314159

Quote from: RedSkeleton007What do you mean it's on my hard disk?

- Apparently you haven't gotten the MASM32 package yet? Go to masm32.com, download and install the package, then it will be on your hard disk, as specified.

4000h is on the line above var3 (var2) at the end. That's the 2 bytes in front of var3, referred to by var3-2
I am NaN ;)



Tedd

Quote from: RedSkeleton007 on July 12, 2015, 07:29:33 AM
mov ax,var1 ;not valid REASON: I don't know. The first array element in var1 is -4. Is it because -4 is not 16-bit like register ax?
ax is 16 bits, sbyte is 8 bits.

Quote
mov ax,var2 ;valid REASON: 1000h and register ax are both 16-bit.
ax is 16 bits, var2 is WORD (also 16 bits).

Quote
mov eax,var3 ;not valid REASON: I don't know. Is it because -16 is not 32-bit like register eax is?
eax is 32 bits, var3 is SWORD (16 bits).

Quote
mov var2,var3 ;not valid REASON: I don't know. Is it because the var3 SWORD and var2 WORD intrinsic data types don't match?
There is no "MOV mem,mem" instruction (only one memory operand is allowed per instruction -- why? that's just how they're encoded.)

Quote
movzx ax,var2 ;not valid REASON: I don't know.
ax is 16 bits, var2 is WORD (also 16 bits) -- what would be zero-extended?

Quote
movzx var2,al ;not valid REASON: I don't know. Is it because no value is initially stored in al to copy to var2's first array element?
(Registers always have some value, even if you may not explicitly know what it is.)
There is no "MOVZX mem,reg" version, only "MOZX reg,reg" and "MOVZX reg,mem"

Quote
mov ds,ax ;valid REASON: ds and ax are both 16-bit registers.
DS is 16 bit, ax is also 16 bit.

Quote
mov ds,1000h ;invalid REASON: I don't know. Both 1000h and ds are 16-bit, aren't they?
DS is 16 bit, 1000h fits into 16 bits, but there is no "MOV sreg,immediate" encoding for this instruction (only "MOV sreg,reg/mem")

Quote
mov ax,[var3 - 2] ;4000h REASON: What value (-16, -42) in var3's array does [var3 - 2] point to? And where did 4000h come from?
var3 is a symbol to refer to a location, (var3-2) is the location 2 bytes before wherever var3 is (bytes, not elements -- the cpu knows nothing of your arrays or their types.) So, the byte before location var3 is part of the 4000h in the var2 'array' -- each of those elements is a WORD, so they take two bytes in memory -- two bytes before var3 is the first byte of 4000h. (Four bytes before would be the start of 3000h.)



"\Masm32\help\opcodes.chm" will be C:\Masm32\help\opcodes.chm if you installed Masm32 on C: (if not, replace with the appropriate partition letter.)
If you didn't install Masm32, why not? :badgrin:
Potato2

RedSkeleton007

Quote from: Tedd on July 14, 2015, 02:36:13 AM
If you didn't install Masm32, why not? :badgrin:
Masm32? That's kind of vague, because lots of things pertain to 32-bit Masm. Do you mean a Masm IDE or compiler? If so, then I guess I haven't installed it yet, because I've been using this book:
http://kipirvine.com/asm/index6th.htm
And the book works with Microsoft Visual Studio. I believe that the book chooses MVS, because it allows you to mix Assembly, C, and C++ syntax instructions in the same code or source file, which is a remarkable learning transition from high level language to the assembly.

dedndave

in the upper right corner of the forum page is a link for the masm32 package download
you will find that masm32 is much more complete, and much more powerful, than Irvine32   :t