News for hackers. Stuff that matters.
Re:Learning to Read X86 Assembly Language by JoeAltmaier on Sunday November 27, 2016 @04:46PM (#13051742)
When originally invented the x86 instruction set was efficient - the most-used instructions had shorter byte code sequences. But eventually some instructions got 'left behind' by the compilers. There are a whole host of single-byte instructions that are never, ever used by a compiler - the register exchange instructions for instance (xchg eax, ebx). Compilers just schedule destination registers carefully, never need to swap them around.

Also the whole set of exchange-register-with-itself were defined but never used. E.g. xchg ax,ax which does nothing in one byte. In fact that one was considered useful, its used as the 'no-op' instruction (0x90) right? But what about xchg bx,bx, xchg cx,cx and so on? Just wasted single-byte opcodes. Leaving actual common instructions to use longer bytecode sequences.

So maybe an executable should begin with an opcode-decode-table that is loaded with the code, that tells the hardware what byte sequences mean what instructions. So each executable code can be essentially compressed, using optimum coding for exactly the instructions that code uses most often. Just thinking out loud.

Re:Learning to Read X86 Assembly Language by wolfgke on Sunday November 27, 2016 @05:01PM (#13051829)
> E.g. xchg ax,ax which does nothing in one byte.

Luckily in x86-16

  xchg ax,ax
simply is encoded as 0x90, which is the same as nop (the same holds for

  xchg eax, eax
in x86-32).

> But what about xchg bx,bx, xchg cx,cx and so on?

This (cleverly?) cannot be encoded in one byte. Here you have to use at least two bytes (0x87 followed by the ModR/M byte in x86-16; the same holds for their 32 bit counterparts xchg ebx, ebx; xchg ecx, ecx etc. in x86-32):

> http://x86.renejeschke.de/html/file_module_x86_id_328.html

---

ADDITION:

> So maybe an executable should begin with an opcode-decode-table that is loaded with the code, that tells the hardware what byte sequences mean what instructions.

The engineers of the Transmeta Crusoe/Efficion processor tried something similar:

> https://en.wikipedia.org/w/index.php?title=Transmeta_Crusoe&...

"Crusoe was notable for its method of achieving x86 compatibility. Instead of the instruction set architecture being implemented in hardware, or translated by specialized hardware, the Crusoe runs a software abstraction layer, or a virtual machine, known as the Code Morphing Software (CMS). The CMS translates machine code instructions received from programs into native instructions for the microprocessor. In this way, the Crusoe can emulate other instruction set architectures (ISAs).

This is used to allow the microprocessors to emulate the Intel x86 instruction set. In theory, it is possible for the CMS to be modified to emulate other ISAs."

Re:Learning to Read X86 Assembly Language by userbinator on Sunday November 27, 2016 @07:28PM (#13052563)
It is still efficient for general-purpose code compared to other ISAs; the majority of register-register and register-memory ops are 2-3 bytes, while for something like MIPS no instruction is ever shorter than 4 bytes.

Compilers just schedule destination registers carefully, never need to swap them around.

Actually, it can happen with certain instructions that need fixed register constraints (multiply, divide, string ops) --- I've encountered a few cases where, had the compiler knew about the exchanges, it could've avoided using another register or spilling to memory. As far as I know, in modern x86 cores the reg-reg exchanges are handled in the register renamer, so they aren't slower than using an extra register and definitely faster than spilling to memory (which might happen anyway for something else if it needed the extra register.)

To witness, here is something no compiler (software) I know of can generate, even when given code that could generate it:

    theloop:
        xchg eax, edx
        add eax, edx
        loop theloop
Never say never ;-)
Re:Learning to Read X86 Assembly Language by spc476 on Sunday November 27, 2016 @06:43PM (#13052340)
Been done (somewhat). The PERQ (https://en.wikipedia.org/wiki/PERQ) had a writable instruction set. And I think Transmeta was trying to do something similar with translating x86 code to an internal format for execution.
Re:Learning to Read X86 Assembly Language by qwertyuiop924 on Sunday November 27, 2016 @04:50PM (#13051757)
That would be cool, but I would not want to hand-code assembly on such a platform. That makes memory segmentation look fun.
Re:Learning to Read X86 Assembly Language by JoeAltmaier on Sunday November 27, 2016 @04:51PM (#13051765)
The table could be created from your assembly automatically? I would never want to code the hex directly in any case.
Re:Learning to Read X86 Assembly Language by qwertyuiop924 on Sunday November 27, 2016 @05:02PM (#13051835)
That could work...

I thought it couldn't a second ago. I don't know why. Makes no sense to me now.

Re:Learning to Read X86 Assembly Language by honkhonkpants on Sunday November 27, 2016 @07:05PM (#13052461)
Are you using the past tense because you are going to tell us about another contemporary ISA that has more code per byte? X86 _is_ compact and this continues to be relevant to performance today.