Are x86 opcodes arbitrary?
Asked Answered
O

1

7

Are x86 op codes arbitrary?

Is there a reason that hlt is 0xf4 and the nop is 0x90?

Oriental answered 28/8, 2016 at 4:7 Comment(2)
They're not completely arbitrary, but for most purposes you might as well assume they are. Note that the NOP instruction is actually the XCHG AX,AX instruction. It also has nothing to do the HLT instruction There's no particular reason to think the HLT opcode would somehow be related to the NOP opcode.Aplite
There are patterns in groups of 3 bits for various reasons. This leads to more patterns if you write them in octal. There are lots of other patterns if you look at an opcode map table.Dunbar
C
14

Very early versions of this architecture (8008,8080) were implemented using extremely small numbers of transistors/logic gates.

At that time, I'm sure the designers chose opcodes and instruction formats in ways that were easy to decode (e.g., smallest number of gates). The instructions tended to be simple, and regular in format.

(I know I did this designing a 16 bit CPU back in the 1970s. Intel might have done a better job but they were always in a hurry. My CPUs insisted that 4 bit opcodes 0000 and 1111 were invalid and caused a trap; this prevented data from being executed by accident in most cases because integral values tend to be all zeros or ones in their top bits).

Success and competition demands evolution and addition of new features. There is constant pressure to invent/add new instructions that enable the CPUs to do things faster and better: floating point, 32 bit data, more registers, SIMD operations, encryption, ...

As new generations of processors came, with larger transistor budgets, two things happened as designers tried to add new instructions:

  • The existing instruction set already defined a bunch of patterns, and these patterns could not be changed. So, designing new opcode, instruction formats and operands, had to be shoehorned into the "holes" in the instruction set. This forced peculiar combinations of bits for various instructions
  • The availability of more transistors meant that decoding complex bit patterns was not such an issue, so choosing strange bit combinations and decoding them was straightforward.

Repeat this process through a dozen generations of CPU until you reach modern times, with many-billion transistor chips. Now the instructions added get more exotic, and the patterns get more complex. It can look arbitrary; it isn't, but it isn't cleanly designed either.

What modern x86 chips do with instruction formats is pretty stunning, yet it works.

Corliss answered 28/8, 2016 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.