How to copy the higher 16hits in the EAX to CX register?

Does anyone know how to copy the higher 16hits in the EAX to CX register?
if EAX has 12345678h and does the CX register suppose to have 1234h or ?
can anyone give me an example on this?
any help would be appreciate. Thanks.
[236 byte] By [darker] at [2007-11-19 9:22:53]
# 1 Re: How to copy the higher 16hits in the EAX to CX register?
You can't access the top 16-bits of an extended register directly.

So you must have a few lines of code.

push eax
shr eax, 16
mov cx, ax
pop eax

It's probably not the most efficient way of doing it, and it's certainly not the only way but it'll work.

Darwen.
darwen at 2007-11-10 3:55:50 >
# 2 Re: How to copy the higher 16hits in the EAX to CX register?
Instead of pushing, shifting and then popping, can't one just rotate right by 16, copy, and then rotate it back?

rtr eax, 16
mov cx, ax
rtl eax, 16

Will that work? I'm not that good at assembly either, I'm kind of a beginner.
Fatboy at 2007-11-10 3:56:50 >
# 3 Re: How to copy the higher 16hits in the EAX to CX register?
True you could use rotate :

ror eax, 16
mov cx, ax
rol eax, 16

But I'm unsure if this is more or less efficient than using a push and a pop -

Darwen.
darwen at 2007-11-10 3:57:50 >
# 4 Re: How to copy the higher 16hits in the EAX to CX register?
Sorry, meant to write ro, wrote rt, my bad.
Fatboy at 2007-11-10 3:58:56 >
# 5 Re: How to copy the higher 16hits in the EAX to CX register?
The "ror" solution seems to be fast, but is very slow with pentium pro, k6-2 and probably athlon, and P4.
Why?
Because it accesses a partial register after modifying the full register (it loses about 9 CPU cycles on pentium pro because of register renaming invalidation).
This code is better:

mov ecx,eax
shr ecx,16

Of course it destroy the high part of ecx, but it is probably better.
Moreover, the high part of ecx being zero, you can use ecx to do some operations (like additions, multiplications), that are faster than operations with cx, which need a prefix and can use a CPU cycle.
SuperKoko at 2007-11-10 3:59:55 >
# 6 Re: How to copy the higher 16hits in the EAX to CX register?
Instead of pushing, shifting and then popping, can't one just rotate right by 16, copy, and then rotate it back?

I think that rotating of shifting will destroy the higher 16-bit of the register if u wanna to preserve it i suggest using the pop and push code it might me better i guess
assembly_boy at 2007-11-10 4:00:51 >