X86 embedded assembly, Accessing specific parts of a register
I have a re开发者_运维技巧gister ecx with value 0x01ABCDEF (hex value) I want to acces just BYTE 2 (AB) in order to perform operations on it. I have tried using cl or ch but those do not acces the right byte. I tried doing:
mov bh, [ecx+2]
but it just errors out... Thank you in advanced for your time and help!
Yes -- CL will give you EF
and CH will give you CD
in the value you've given above. Since you don't want those, you'll have to do a shift to get the bytes in the right places:
mov ebx, ecx
shr ebx, 8
Now BH will have AB
, and BL will have CD
.
Edit: From your comment, you apparently don't really need the value in BH -- rather, you just want to manipulate that particular byte without affecting the rest of ECX. In that case, it's probably easiest to do something like this:
ror ecx, 16
not cl // placeholder for the manipulation
rol ecx, 16
This just rotates ECX so the byte we care about is in CL, then manipulates CL, then rotates ECX back so the bytes are where they started. I should add that while this is simple, on some processors it will be pretty slow. The Pentium IV didn't have a barrel shifter, so rotates take time proportional to the number of bits you move by. Worse, manipulating CL followed by using ECX can (and in this case probably will) cause a Partial Register Stall. Whether this matters to you will depend on what you're trying to accomplish with this, and whether it'll be surrounded by other instructions that can be executed during the PRS.
精彩评论