1ntgr - 3 months ago 10

C++ Question

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.

Answer

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`

.

Source (Stackoverflow)

Comments