Anon
Anon

Reputation: 1354

Moving variable of type short into EAX using __asm in c++(VS 2010)

When using asm insertions in c++ (Visual Studio 2010), I get an "incompatible types" error when i try to move 16 bit variable of type short into the 32bit EAX register. However everything works fine if i use the 16 bit AX register. What is the logic behind this error? Thank you!

#include <iostream>

int main()
{
   short sVar;

   std::cout << "sVar=";
   std::cin >> sVar;

   __asm
   {
      MOV AX, sVar;
      SHL AX, 1;
      MOV sVar, AX;
   }
   std::cout << sVar << "\n";

   return 0;
}

Upvotes: 1

Views: 752

Answers (2)

dpiskyulev
dpiskyulev

Reputation: 336

EAX is 32 bits and sVar (short) is 16 bits. In this case you need to look into MOVSX (move with sign extend) or MOVZX (move with zero extend).

Upvotes: 0

sharptooth
sharptooth

Reputation: 170499

The logic is simple - eax is 32-bit and short is 16-bit, so you can't copy one into another because they are of different sizes. ax is 16-bit, so short can be copied there no problem. You can then use movzx or movsx to widen the 16-bit value to 32 bits with either zero or sign extension.

Upvotes: 4

Related Questions