Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are x86 opcodes arbitrary?

Are x86 op codes arbitrary?

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

like image 932
Roblox Man225 Avatar asked Aug 28 '16 04:08

Roblox Man225


People also ask

What is x86 opcode?

The x86 opcode bytes are 8-bit equivalents of iii field that we discussed in simplified encoding. This provides for up to 512 different instruction classes, although the x86 does not yet use them all.

Is opcode always 8 bit?

Opcodes are not always 8 bits but yes, it is hardcoded/wired in the logic to isolate the opcode and then send you down a course of action based on that.

What size in bytes are opcodes on an x86 processor?

x86 opcodes are 1 byte for most common instructions, especially instructions which have existed since 8086. Instructions added later (e.g. like bsf and movsx in 386) often use 2-byte opcodes with a 0f escape byte.

How many opcodes are there?

Usually an opcode will fit into a single memory access, and then the answer is 2^12. But a processor could implement a multi-cycle opcode decoding process to extend the number of possible opcodes beyond 2^12. The maximum number of instructions (containing opcodes) that the processor can directly address.


1 Answers

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.

like image 189
Ira Baxter Avatar answered Sep 23 '22 23:09

Ira Baxter