LockerGnomeTech news and nostalgia for today's technology professionals
Subscribe

Search

Retro

x86 Is an Octal Machine: Read Its Opcodes in Base 8 and 48 of Them Collapse Into One Pattern

Mark Hopkins's old Usenet explainer, preserved in a GitHub gist, traces the layout to the 8080 and says even Intel's manual writers missed it.

Seen from above, an astonished Chris leaps across an eight-by-eight floor of colored tiles whose columns light up in matching patterns.
Illustration: LockerGnome

Hopkins posted this to the alt.lang.asm newsgroup as a revision of their own 1992 article, and they did not tiptoe in: “The only proper way to understand 80x86 coding is to realize that ALL 80x86 OPCODES ARE CODED IN OCTAL.” The all-caps are theirs. The gist keeps them, along with a summary of instructions up through the 80486.

The trick is small. A byte fits in three octal digits, 000 through 377. The first digit takes two bits and the other two take three each. Three bits can name eight things, which happens to be how many registers you pick from in these encodings.

Six forms, eight operations

Here is where hex starts looking guilty. The basic arithmetic and logic instructions all follow 0P0 through 0P5, where the middle digit picks the operation:

P:  0 add   1 or    2 adc   3 sbb
    4 and   5 sub   6 xor   7 cmp

The last digit picks the form: byte or word, which direction the data goes, or a constant applied to the accumulator. Eight operations times six forms gives you 48 opcodes from one small rule. Hopkins’s verdict: “That’s a good fraction of your reference table right there.”

Try it on 31 C0, the familiar register-zeroing pair (hex’s greatest hit). In octal it reads 061 300. The 6 means XOR, the 1 means word-sized with a register as the source, and 300 says both operands are registers, number 0 and number 0. That’s AX against itself in 16-bit code, or EAX in 32-bit.

The same operation digit shows up again when the other operand is a constant. Opcodes 200, 201 and 203 move it into the middle of the following byte. Learn the list of eight once and it pays rent twice.

The byte after the opcode

That following byte, usually called ModRM, is written xrm in the post. The x digit sets the addressing mode, r usually names a register, and m names the other register or a way to reach memory.

Take the bytes 210 135 375. Opcode 210 is a byte-sized MOV from a register. In 135, register 3 is BL, while mode 1 and slot 5 mean “DI plus a one-byte offset.” The final 375 is a signed -3. You get mov [DI-3], BL, decoded without hunting through a 256-square chart.

Or take 216 332: move into a segment register, number 3 (DS), from register 2 (DX). That’s mov DS, DX.

Hopkins traces these habits to the 8080, 8085 and Z80, then turns to the manuals: “For some reason absolutely everybody misses all of this, even the Intel people who wrote the reference.” Hopkins calls the usual presentation “an almost grotesque complication.” Intel, your feedback form has arrived.

The pattern has limits. Conditional jumps run from octal 160 to 177, but there are 16 conditions, so they read better as hex 70 through 7F. Hopkins names this the one major exception. The 386 also adds FS and GS, 32-bit words, and a new addressing scheme with a scaled-index byte. That extra byte again breaks into three small fields.

One catch if you copy the tables: the byte-register row prints DL twice. Slot 6 should be DH, as the post’s own expanded address table shows (even octal needs a proofreader).

Keep an octal view handy

A fair bet: hex will keep its job. It packs a byte into two characters, and your debugger isn’t about to develop a sudden fondness for eights. A three-digit view beside it would make a great teaching toggle, especially for anyone who thinks machine code is 256 unrelated facts in a trench coat.

The likely payoff is faster recognition. You still need the manual for the odd corners, but you can walk in knowing which drawer to open. Hopkins went further: “people claim that it’s almost impossible to know 8086 binary by heart, whereas in fact I know most of it by memory.”

They even supplied an expanded address table “for the expansion impaired. :)”

Your hex dump has been mumbling octal.

More Retro

Every Retro story →