in ASM 32bit there is the EAX register from 0 until 31 bit. In the EAX there is an AX from 0 bit until 15bit. How called the register from 15bit until 31bit?
-
5There isn't one. The upper 16-bit of a 32-bit register don't have a corresponding register. – Michael Petch Oct 19 '17 at 07:26
-
Shift to lower bits, modify, then return to higher. Lower bits are accessible as 16/8 bit value to have 16 and 8 bit registers, and higher part is only extension to 32 bits. – i486 Oct 20 '17 at 08:36
2 Answers
You cannot address the top 16-bits of a 32-bit register directly.
If you want to fiddle with the top 16 bits without affecting the lower 16 bits, you'll have to use multiple instructions:
You either use masking (or/and/xor) or shifting (shr/shl).
All the below examples leave the lower 16 bits of eax unchanged.
for example:
and eax,0x0000FFFF ; zero out top 16 bits ;and with 1 does not change bit.
or eax,0xFFFF0000 ; set top 16 bits to 1; or with 0 does not change bit.
xor eax,0xFFFF0000 ; invert top 16 bits; xor with 0 does not change bit.
;copy ax into eax-high
mov ebx,eax ; copy eax to temp register
movzx eax,ax ; clear out top 16 bits of eax
shl ebx,16 ; move ax to ebx-high
or eax,ebx ; combine ax and ebx-high, cloning ax.
;exchange ax and eax-high
rol eax,16
You can lookup the descriptions of the instructions here: http://www.felixcloutier.com/x86/
- 74,508
- 24
- 191
- 319
-
Also useful: BMI2 `rorx ecx, eax, 16` to copy-and-rotate. Or `shld ecx, eax, 16` to double-shift into `ecx`, with a dependency on the old value of ecx. – Peter Cordes Oct 19 '17 at 08:35
While you cannot access the high order byte directly, there is an instruction that makes this much easier than the logic in the other answer.
Look into the BSWAP instruction. This instruction swaps the bytes in the 32 bit register, allowing you to then access the pieces using AH and AL or just AX. You must be aware that BSWAP reverses the order of the bytes, so it is not simply swapping the high order and low order words.
mov eax, [ebx]
bswap eax ; The highest order byte is now in AL
; The second highest order byte is now in AH
- 74,508
- 24
- 191
- 319
- 15,862
- 4
- 48
- 67
-
`bswap` also swaps the bytes around, how is this helpful? Now you've got your data in the wrong endianness. – Johan Oct 20 '17 at 08:09
-
If you read the answer you'll see that I pointed that out. If the ultimate goal is to access the individual bytes in the word, this is extremely useful. – David Hoelzer Oct 20 '17 at 11:23
-
1It's one bye shorter than `rol eax,16` ([embedding the register in the opcode](http://felixcloutier.com/x86/BSWAP.html)), and it's the same performance on most CPUs. Maybe running on a different port. It doesn't have an immediate operand, which could be an advantage for the uop cache depending on surrounding instructions having immediates + disp8 or disp32 addressing modes. – Peter Cordes Oct 20 '17 at 12:39
-
@Johan just do `xchg ah, al` after `bswap` to get higher 16-bit value you want – Blanket Fox Jun 28 '21 at 05:09