1

I am having trouble translating the below assembly in to c++

MOVZX EAX, DX

Where EDX is a 32bit register. I need to get the lowest 16 bits(DX).

I've tried the following:

unsigned edx = 0x123ABCDE;
unsigned dx = (edx>>16) & 0xff;

I expect to get an the value of BCDE stored in dx, but it's gone a bit wrong.

Any help would be much appreciated.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
1ntgr
  • 126
  • 2
  • 8

1 Answers1

2

Let's analyze your code step by step.
unsigned edx = 0x123ABCDE;
Nothing unusual.

Let's break up the next statement according to order of evaluation.
(edx >> 16) -- right shift by 16 bits.
This is right shifting by 2 bytes or 4 hex digits.
Answer should be 0x123A.

Now, let's keep the right most 8 bits (ANDing with 0xFF):
0x123A & 0xFF == 0x3A

The result should be 0x3A in the variable dx.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Thanks for your explanation. This explanation along with Michael's comment has helped me find where I went wrong and understand the solution. eax = edx & 0xffff gives me 0xBCDE where edx=0x123bcde – 1ntgr Aug 05 '16 at 20:09
  • 1
    If the answer is helpful, please click on the check mark. – Thomas Matthews Aug 05 '16 at 20:38