Question

I have started learning masm assembly recently, iv been disassembling many of my programs just to have a look. I have noticed that when you use a __int16 (word) the value of it is 1st copied into eax then into the variable itself, but with int (dword/32) it is copied directly into the variable: heres the dissembled code for both

int y = 5;
0040101E  mov         dword ptr [y],5 

    y += 7;
00401025  mov         eax,dword ptr [y] 
00401028  add         eax,7 
0040102B  mov         dword ptr [y],eax 

    __int16 x = 3;
0040102E  mov         eax,3 
00401033  mov         word ptr [x],ax 

    x += 6;
00401037  movsx       eax,word ptr [x] 
0040103B  add         eax,6 
0040103E  mov         word ptr [x],ax 

I notice that the __int16 is of size 16... which is half the size of eax, is this why it is put into eax first then only ax into x?

Also, which is fastest?

Is there any good masm sites you would recommend?

Thanks for your input

Was it helpful?

Solution

The same results would indeed be given if the instruction were mov ax,3, but the compiler probably choose mov eax,3 because the machine code is shorter and in this particular case the effect is the same. mov ax,3 would have an extra prefix byte that tells the CPU it's a 16-bit operation. (The encoding for the two instructions is otherwise identical.)

If you're using Visual C++, you can switching on the display of the machine code bytes in the disassembly window, and see how large each instruction is.

At one point, 16-bit instructions (or, more precisely, instructions using the 'other' word size from the current CPU mode) were less efficient; this may or may not be the case any more, I'm not sure. (My guess would be that the effect of this would be fairly difficult to spot in general, and you'd have to contrive some code snippet that makes it obvious.)

OTHER TIPS

Word sizes that match your cpu architecture are always fastest. Anything else is forced to be converted back and forth for every operation.

TLDR: unless you have a very specific reason, use int.

I'm making this up, as I don't specifically know.

But it seems that on the modern processors, working with eax is more efficient than working with the smaller, ax register.

By moving in to eax first (in the first assignment), the CPU will sign extend the constant in to the 32 bit register.

Odds are also good that he compiler does all of its math in 32 bit (working with the registers), and transparently giving you the remains (i.e. ax).

That's what it looks like to me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top