Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't the higher 16-bits in EAX accessible by name (like AX, AH and AL)? [duplicate]

Tags:

x86

assembly

Why don't have a specific register to access this other part of register ( 16-32 )?

Like ah or al to access a 8-bits part of ax register.

enter image description here

like image 449
Alexandre Avatar asked Feb 10 '15 10:02

Alexandre


People also ask

What is the difference between the EAX AX AH and AL registers?

ax is the 16-bit, "short" size register. It was added in 1979 with the 8086 CPU, but is used in DOS or BIOS code to this day. al and ah are the 8-bit, "char" size registers. al is the low 8 bits, ah is the high 8 bits.

What is the name of the higher 16 bits of the EDX register?

Stack Pointer (SP) − The 16-bit SP register provides the offset value within the program stack.

How could I get the high order 16 bits of a 32-bit register?

There isn't one. The upper 16-bit of a 32-bit register don't have a corresponding register. 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.

How many bytes does EAX hold?

The idiv instruction divides the contents of the 64 bit integer EDX:EAX (constructed by viewing EDX as the most significant four bytes and EAX as the least significant four bytes) by the specified operand value.


2 Answers

The idea was to extend the registers to 32 bit, not to create a machine with twice as many 16 or 8 bit registers because you already had enough of those. To keep the encoding and the hardware simpler, they decided not to give direct access to the top 16 bits. Everything comes at a cost. Fun fact: the 64 bit extension did bring r8-r15 with it, but you can't access the top 32 bit of those directly either.

like image 51
Jester Avatar answered Nov 12 '22 22:11

Jester


Because it is unnecessary. Most of the time you work with the full register. Even small low registers like AL and AX are not commonly used, let alone some arbitrary values in the middle of a register like AH or the high bytes of EAX which are hardly found in practice. Byte registers are mainly used with SETcc and MOVcc instructions. Word registers are even rarer and almost never used because the instructions are longer (need a prefix byte) and likely slower. Narrow values are usually sign or zero extended to the full register immediately

Allowing access to those high parts require the introduction of new opcodes which is difficult to find in the current tight free space, and that will pollute the x86 opcode space. Moreover, separate access to different parts of the registers will introduce more complex dependencies due to partial register update which may cause a stall

  • Why doesn't GCC use partial registers?
  • How exactly do partial registers on Haswell/Skylake perform? Writing AL seems to have a false dependency on RAX, and AH is inconsistent
  • Understanding partial-register slowdowns from mov instead of movzx instruction

That's one of the reasons why all instructions with a 32-bit destination in x86-64 zero the upper part of the result instead of preserving it. Same to the high bytes of XMM in AVX. INC is also not preferred compared to ADD 1 nowadays because it introduces a partial flag update

  • What is a Partial Flag Stall?

See also

  • Why can I access lower dword/word/byte in a register but not higher?
  • If I have an 8-bit value, is there any advantage to using an 8-bit register instead of say, 16, 32, or 64-bit?
  • Any way to move 2 bytes in 32-bit x86 using MOV without causing a mode switch or cpu stall?
like image 37
phuclv Avatar answered Nov 12 '22 21:11

phuclv