Lobsters retrocomputing - 27 Nov 2026
Page 1 of 3
Last active
October 6, 2026 22:35
-
Star
(168)
You must be signed in to star a gist
-
Fork
(30)
You must be signed in to fork a gist
- Save seanjensengrey/f971c20d05d4d0efc0781f2f3c0353da to your computer and use it in GitHub Desktop.
x86 is an octal machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| | # source:http://geocities.com/SiliconValley/heights/7052/opcode.txt |
| | From: mark@omnifest.uwm.edu (Mark Hopkins) |
| | Newsgroups: alt.lang.asm |
| | Subject: A Summary of the 80486 Opcodes and Instructions |
| | (1) The 80x86 is an Octal Machine |
| | This is a follow-up and revision of an article posted in alt.lang.asm on |
| | 7-5-92 concerning the 80x86 instruction encoding. |
| | The only proper way to understand 80x86 coding is to realize that ALL 80x86 |
| | OPCODES ARE CODED IN OCTAL. A byte has 3 octal digits, ranging from 000 to |
| | 377. In fact, each octal group (000-077, 100-177, etc.) tends to encode a |
| | specific variety of operation. All of these are features inherited from the |
| | 8080/8085/Z80. |
| | For some reason absolutely everybody misses all of this, even the Intel |
| | people who wrote the reference on the 8086 (and even the 8080). The opcode |
| | scheme outlined briefly below is expanded starting in the 80386, but |
| | consistently with the overall scheme here. |
| | As an example to see how this works, the mov instructions in octal are: |
| | 210 xrm mov Eb, Rb |
| | 211 xrm mov Ew, Rw |
| | 212 xrm mov Rb, Eb |
| | 213 xrm mov Rw, Ew |
| | 214 xsm mov Ew, SR |
| | 216 xsm mov SR, Ew |
| | The meanings of the octal digits (x, m, r, s) and their correspondence to the |
| | operands (Eb, Ew, Rb, Rw, SR) are the following: |
| | The digit r (0-7) encodes the register operand as follows: |
| | REGISTER (r): 0 1 2 3 4 5 6 7 |
| | Rb = Byte-sized register AL CL DL BL AH CH DL BH |
| | Rw = Word-sized register AX CX DX BX SP BP SI DI |
| | The segment register digit s (0-7) encodes the segment register as follows: |
| | SEGMENT REGISTER (s): 0 1 2 3 4 5 6 7 |
| | SR = Segment register ES CS SS DS <Reserved> |
| | The digits x (0-3), and m (0-7) encode the address mode according to |
| | the following scheme. One or more bytes (labeled: Disp) may immediately |
| | follow xrm as described below. |
| | TABLE 1: 16-BIT ADDRESSING MODE (x, m): |
| | Eb = Address of byte-sized object in memory or register |
| | Ew = Address of word-sized object in memory or register |
| | Dw = Unsigned word |
| | Dc = Signed byte ("character"), range: -128 to +127 (decimal). |
| | Db = Unsigned byte |
| | x m Disp Eb Ew |
| | ------------------ |
| | 3 r Rb Rw |
| | 0 6 Dw DS:[Dw] |
| | 0 m Base:[0] (except for xm = 06). |
| | 1 m Dc Base:[Dc] |
| | 2 m Dw Base:[Dw] |
| | x 0 Disp DS:[BX + SI + Disp] |
| | x 1 Disp DS:[BX + DI + Disp] |
| | x 2 Disp SS:[BP + SI + Disp] |
| | x 3 Disp SS:[BP + DI + Disp] |
| | x 4 Disp DS:[SI + Disp] |
| | x 5 Disp SS:[DI + Disp] |
| | x 6 Disp DS:[BP + Disp] (except for xm = 06) |
| | x 7 Disp DS:[BX + Disp] |
| | This expands into the following table: |
| | TABLE 1a: 16-BIT ADDRESSING MODE (x, m) for the expansion impaired. :) |
| | xm Eb/Ew xm Eb/Ew xm Eb/Ew xm Eb/Ew |
| | 00 DS:[BX + SI] 10 Dc DS:[BX + SI + Dc] 20 Dw DS:[BX + SI + Dw] 30 AL/AX |
| | 01 DS:[BX + DI] 11 Dc DS:[BX + BI + Dc] 21 Dw DS:[BX + DI + Dw] 31 CL/CX |
| | 02 SS:[BX + SI] 12 Dc SS:[BP + SI + Dc] 22 Dw SS:[BP + SI + Dw] 32 DL/DX |
| | 03 SS:[BX + DI] 13 Dc SS:[BP + DI + Dc] 23 Dw SS:[BP + DI + Dw] 33 BL/BX |
| | 04 DS:[SI] 14 Dc DS:[SI + Dc] 24 Dw DS:[SI + Dw] 34 AH/SP |
| | 05 DS:[DI] 15 Dc DS:[DI + Dc] 25 Dw DS:[DI + Dw] 35 CH/BP |
| | 06 Dw DS:[Dw] 16 Dc SS:[BP + Dc] 26 Dw SS:[BP + Dw] 36 DH/SI |
| | 07 DS:[BX] 17 Dc DS:[BX + Dc] 27 Dw DS:[BX + Dw] 37 BH/DI |
| | Operands where x is 0, 1, or 2 are all pointers. If the instruction is a WORD |
| | instruction (211, 213, 214, 216 are), then this pointer addresses a |
| | word-sized object. The format of the object at the indicated address will |
| | always be low-order byte first, and high-order byte second. Otherwise the |
| | instruction is a BYTE instruction (210, 212) and the pointer addresses |
| | byte-sized object at the indicated address. |
| | The default segments (DS:, SS:) can be overridden with a segment prefix. In |
| | all cases it's understood that everything has the default segment DS, except |
| | for the two stack/frame pointers (BP and SP) whose default segment is SS. |
| | That will be explained below. |
| | Modes where x = 1, or 2 will require displacement bytes (Dc or Dw) to follow |
| | the opcode as explained above. |
| | When x = 3, WORD sized instructions address the word registers (AX, CX, ...) |
| | and the BYTE size instructions the byte registers (AL, CL, ...). |
| | EXAMPLE 1: The instruction opcode: 210 135 375 |
| | Here, xm = 15, and r = 3, so the operands are: |
| | mov Eb, Rb |
| | => |
| | mov byte ptr DS:[DI + Dc], BL |
| | The displacement, Dc, is 375 (or fd in hexadecimal), which is the signed byte |
| | -3. So the instruction reads: |
| | mov byte ptr DS:[DI - 3], BL |
| | or just: |
| | mov [DI - 3], BL |
| | In C-like notation, the meaning of this operation would be: |
| | ((byte *)DS) [DI - 3] = BL; |
| | EXAMPLE 2: The instruction opcode: 216 332 |
| | Here, xm = 32, and s = 3, so the operands are: |
| | mov SR, Ew |
| | => |
| | mov DS, DX |
| | A move to CS is not possible (because the far jump instruction already does |
| | that) so that the opcode sequence: |
| | 216 x2m |
| | is free to be used for encoding something else. |
| | EXAMPLE 3: As an illustration of why it's better to think in octal, just look |
| | at the opcodes for the binary arithmetic instructions: |
| | 0P0 xrm Op Eb, Rb |
| | 0P1 xrm Op Ew, Rw |
| | 0P2 xrm Op Rb, Eb |
| | 0P3 xrm Op Rw, Ew |
| | 0P4 Db Op AL, Db |
| | 0P5 Dw Op AX, Dw |
| | They all have the same form, with a single digit encoding the operator as |
| | follows: |
| | P Op P Op |
| | 0 add 1 or |
| | 2 adc 3 sbb |
| | 4 and 5 sub |
| | 6 xor 7 cmp |
| | That's a good fraction of your reference table right there. |
| | EXAMPLE 4: The same mapping is used in the immediate to memory/register form |
| | of these operations: |
| | 200 xPm Db Op Eb, Db |
| | 201 xPm Dw Op Ew, Dw |
| | 203 xPm Dc Op Ew, Dc |
| | (2) An Outline of 80x86 Instructions and Encoding |
| | The authors of 8080 and 8086 references (including Intel's own references) |
| | are apparently not aware of the octal nature of their own machines, and the |
| | result is an almost grotesque complication and bungling up in the presentation |
| | of something that is actually fairly simple. Thus, people claim that it's |
| | almost impossible to know 8086 binary by heart, whereas in fact I know most of |
| | it by memory. I'll straighten out the mess for you here. |
| | As alluded to above, instructions are encoded as follows: |
| | op xrm Const |
| | where * op is a 1 or 2 byte opcode, |
| | * xrm (if present) constitutes 3 octal digits whose normal uses are: |
| | r = Register operand, xm = Memory or Register operand. |
| | It may be followed immediately by a "displacement" byte or word, |
| | depending solely on the digits x and m. |
| | * Const (if present) denotes a byte or word value whose presence and |
| | format depends solely on what op (and sometimes xrm) is. |
| | In some cases, the opcode itself may be separated out into octal digits, e.g. |
| | 0s6 = push (Segment Register #s). |
| | The one major exception to the coding scheme are all the conditional code |
| | operations. Since there are 16 distinct conditional codes, they are |
| | represented as a hexadecimal digit. The conditional jump in octal ranges |
| | from 160 to 177, which is 7x in hexadecimal, where x is a hex digit encoding |
| | the jump's condition. I'll represent them by the format: 160+CC. |
| | The register and address encoding was described above. The '386 expands on |
| | this a little with the addition of two segment registers: |
| | SEGMENT REGISTER (s): 0 1 2 3 4 5 6 7 |
| | SR = Segment register ES CS SS DS FS GS <Reserved> |
| | In TABLE 1, note that the addresses encoded on modes 0m, 1m, 2m are the same |
| | regardless of whether you're referring to Eb or Ew. What distinguishes them |
| | is the size of the object being pointed to and this can be explicitly |
| | indicated in traditional '86 assemblers like the following examples: |
| | byte ptr [BP] |
| | word ptr [BX + DI] |
| | As explained before, all addresses, except those involving BP refer to the |
| | data segment, DS. All the BP's refer to the stack segment, SS. This |
| | is about to be explained. |
| | (3) Segmentation and Registers |
| | The 80x86 was designed with more or less specific uses for its registers. |
| | In fact, the names are supposed to reflect their main uses: |
| | AX (AH:AL) = Accumulator |
| | BX (BH:BL) = Base Register |
| | CX (CH:CL) = Counting Register |
| | DX (DH:DL) = Data Register |
| | CS = Code Segment -- where constants and programs lie. |
| | DS = Data Segment -- where static variables lie. |
| | SS = Stack Segment -- where auto variables and function parameters lie. |
| | SP, BP = Stack and Frame Pointers, used to segment out the |
| | local variables and function parameters. |
| | ES = Extra Segment -- used in combination with the index registers for |
| | string operations as follows: |
| | DS:[SI] -- points to the Source of the string operation. |
| | ES:[DI] -- points to the Destinction of the string operation. |
| | The typical setup for the stack is as follows: |
| | High Addresses FUNCTION DEFINITION: FUNCTION CALL: |
| | ... mov BP, SP push Parameters |
| | Parameters push BP call Function |
| | Return Address sub SP, Locals |
| | BP -> Old BP ... function ... |
| | Local Variables mov SP, BP |
| | SP -> ... pop BP |
| | Low Addresses ret Parameters |
| | this dictates a certain protocol in calling functions with parameters and |
| | returning from them, as shown above. In fact, this is so much so that the |
| | opening and closing sequences above have all been defined as single operations |
| | starting with the 80286 so that the function definition above can be rewritten |
| | as: |
| | FUNCTION DEFINITION: |
| | enter Locals, 0 |
| | ... function ... |
| | leave |
| | ret Parameters |
| | (4) Word and Address Size on the 80386 and Above |
| | Starting with the 80386, operations can be done with not just 16-bit words |
| | but also 32 bit words. Generally the same operation is defined for both sets |
| | and context is used to determine which is which in the following two ways: |
| | * Which mode the machine is running in |
| | Protected mode -- both word sizes and address sizes are 32-bits |
| | Real & Virtual modes -- 16-bits. |
| | * The presence of certain prefixes to override either the default |
| | word size, address size or both on an instruction-by-instruction |
| | basis. |
| | (a) Word Size |
| | When the word size for the current operation is 32-bits, everything listed |
| | above as "word" is interpreted as 32-bits, including registers. The register |
| | numbering corresponding to this word size is: |
| | REGISTER (r): 0 1 2 3 4 5 6 7 |
| | Rb = Byte-sized register AL CL DL BL AH CH DL BH |
| | Rd = Dword-sized register EAX ECX EDX EBX ESP EBP ESI EDI |
| | (b) Address Size |
| | When the address size is switched to 32-bits, the address scheme listed in |
| | TABLE 1 is altered in its entirety. |
| | TABLE 2: 32-BIT ADDRESSING MODE (x, m): Encoding of scaled index SI: |
| | x m Disp Eb Ew si SI |
| | -------------------- --------------- |
| | 0 6 Dw DS:[Dw] s0 EAX * 2^s |
| | 0 4 sir [Rd + SI + 0] s1 ECX * 2^s |
| | 1 4 sir Dc [Rd + SI + Dc] s2 EDX * 2^s |
| | 2 4 sir Dw [Rd + SI + Dw] s3 EBX * 2^s |
| | 0 r [Rd + 0] (except r = 4, 6) 04 0 |
| | 1 r Dc [Rd + Dc] (except r = 4) s5 EBP * 2^s |
| | 2 r Dw [Rd + Dw] (except r = 4) s6 ESI * 2^s |
| | 3 r Rb Rw s7 EDI * 2^s |
| | The encodings si = 14, 24 and 34 remain undefined. |
| | This alteration is INDEPENDENT of the word size setting. That means that |
| | even the "Dw"'s, "Rw"'s in the chart above will vary in interpretation as |
| | 16-bit or 32-bit objects depending on the word size setting. That leads to |
| | 4 possible combinations, not just 2. |
| | EXAMPLE 5: The opcode sequence 211 135 375 |
| | This is the operation |
| | mov Ew, Rw |
| | where xm = 15, r = 3 and Disp = -3. The 4 combinations are: |
| | Addr-Size Word-Size Operation |
| | 16 16 mov word ptr [DI - 3], BX |
| | 16 32 mov dword ptr [DI - 3], EBX |
| | 32 16 mov word ptr [EBP - 3], BX |
| | 32 32 mov dword ptr [EBP - 3], EBX |
| | EXAMPLE 6: The opcode sequence 211 134 302 375 with 32-bit addressing. |
| | This is the move instruction where xm = 14 and r = 3. |
| | mov Ew, [E]BX ([E]BX since r = 3) |
| | It uses the indexed register addressing. The address, Ew, may be derived |
| | as follows: |
| | x m sir Disp Ew Comments |
| | 1 4 sir Dc [EDX + SI + Dc] |
| | 1 4 si2 375 [EDX + SI - 3] (Rd = EDX for r = 2) |
| | 1 4 302 375 [EDX + 8*EAX - 3] (SI = 8*EAX for si = 30) |
| | Therefore, this instruction represents one of the following: |
| | Word-Size Operation: 211 134 302 375 |
| | 16 mov word ptr [EDX + 8*EAX - 3], BX |
| | 32 mov dword ptr [EDX + 8*EAX - 3], EBX |
| | (5) The Opcode Summary |
| | The chart below summarises all the machine instructions. The following |
| | abbreviations are used: |
| | Registers: Immediate Data Constant: |
| | Rb (byte sized) Db (byte sized) |
| | Rw (word sized) Dw (word sized) |
| | Rd (dword sized) Dc (signed byte) |
| | Register/Memory Address: Relative Code Address: |
| | Eb (byte sized) Cb (byte sized) |
| | Ew (word sized) Cw (word sized) |
| | Memory Address: Code Address: |
| | Es (16 bit selector) Af (32/48 bit absolute far code address) |
| | En (near 16/32 bit pointer) |
| | Ef (far i32/48 bit pointer) |
| | Ep (pointer to 6-byte object) |
| | Ea (generic address) |
| | Processor Extensions: |
| | * = 80186 extension |
| | $ = 80286 extension |
| | # = 80386 extension |
| | @ = 80486 extension |
| | The switch between 16 and 32 bit word size affects all operands labeled |
| | Rw, Ew, Dw, Cw, En and even Af and Ef. The latter two objects refer to |
| | far code addresses which are 4 bytes when the word size is 16 bits, and |
| | 6 bytes else. |
| | The only such operands not actually affected by the word-size switch are |
| | those whose size a consequence of the operation's meaning. These include |
| | the following: RET, BOUND, ARPL, SMSW, LMSW, LAR and LSL. |
| | The switch between 16 and 32 bit address size affects all the operands |
| | labeled Eb, Ew, Es, En, Ef, Ep, and Ea. Each of these is interpreted |
| | according to the xm digts in the opcode according to either the 16-bit |
| | addres table described near the start of the article or the 32-bit address |
| | table just described above. |
| | NOTE: In the following presentation everything is in octal. |
| | ARITHMETIC & LOGIC |
| | ------------------ |
| | Comments: |
| | * All of these operations affect all 6 arithmetic flags, except NOT (which |
| | affects no flags), and INC and DEC (which don't affect CF). |
| | * IMUL and MUL only affect CF and OF predictably. |
| | * IDIV and DIV affect no flags predictably. |
| | * AND, OR, XOR, and TEST all set CF and OF to 0 and alter AF unpredictably. |
| | * CMP and TEST have no affect on any operands. They're used for setting |
| | flags. CMP is used for doing relational operators (< > <= >= == !=), and |
| | TEST for doing bit-testing. |
| | * CMP and TEST can have their operands listed in either order. |
| | P Op Description |
| | 0 ADD L, E L += E |
| | 2 ADC L, E L += E + CF |
| | 5 SUB L, E L -= E |
| | 3 SBB L, E L -= E + CF |
| | 7 CMP L, E (void)(L - E) |
| | 1 OR L, E L \|= E |
| | 4 AND L, E L &= E |
| | 6 XOR L, E L ^= E |
| | 0P0 xrm Op Eb, Rb |
| | 0P1 xrm Op Ew, Rw |
| | 0P2 xrm Op Rb, Eb |
| | 0P3 xrm Op Rw, Ew |
| | 0P4 Db Op AL, Db |
| | 0P5 Dw Op AX, Dw |
| | 200 xPm Db Op Eb, Db |
| | 201 xPm Dw Op Ew, Dw |
| | 203 xPm Dc Op Ew, Dc |
| | NOT L L = ~L |
| | 366 x2m not Eb |
| | 367 x2m not Ew |
| | NEG L L = -L |
| | 366 x3m neg Eb |
| | 367 x3m neg Ew |
| | INC L L++ |
| | 10r inc Rw |
| | 376 x0m inc Eb |
| | 377 x0m inc Ew |
| | DEC L L-- |
| | 11r dec Rw |
| | 376 x1m dec Eb |
| | 377 x1m dec Ew |
| | TEST L, E (void)(L&E) |
| | 204 xrm test Rb, Eb |
| | 205 xrm test Rw, Ew |
| | 250 Db test AL, Db |
| | 251 Dw test AX, Dw |
| | 366 x0m Db test Eb, Db |
| | 367 x0m Dw test Ew, Dw |
| | IMUL L, E, D L = (signed)E*D |
| | IMUL L, E L = (signed)L*E |
| | # 017 257 xrm Dw imul Rw, Ew |
| | * 151 xrm Dw imul Rw, Ew, Dw |
| | * 153 xrm Db imul Rw, Ew, Dc |
| | In the following operations: |
| | Operand Size ACC' ACC |
| | 1 AX AL |
| | 2 DX:AX AX |
| | 4 EDX:EAX EAX |
| | P Op Description |
| | 4 MUL E ACC' = (unsigned) ACC*E |
| | 5 IMUL E ACC' = (signed) ACC*E |
| | 6 DIV E ACC' = (unsigned) ACC%E : ACC/E |
| | 7 IDIV E ACC' = (signed) ACC%E : ACC/E |
Next page | More Lobsters retrocomputing | Headlines
Original: https://gist.github.com/seanjensengrey/f971c20d05d4d0efc0781f2f3c0353da