assembler – Processor execution of non-existent instructions


Hello! First, I want to clarify how I understand the work of the processor, correct it if something is wrong. Rough: The processor is given a command to read the instruction, the instruction itself is entered, its code (well, from 0 and 1), then the processor compares the entered instruction with the instructions written in it, receives operands (if any), and does what this should do instruction.


Is it possible to somehow compile a program with an invalid instruction, force the processor to switch to it. If so, what happens? Will he just check, will not find a match and that's it? Is such a situation envisaged?

Architecture is not important, ARM, X86


In the case of a non-existent instruction, an exception (interrupt) with a specific number (int 6) occurs. The return address is stored on the stack. At this address, you can "manually" programmatically execute the instruction, shift the return ip-address, and return iret (for x86) to the next instruction. In this way, for example, coprocessor emulation was done. In this case, you need to know exactly how to follow the instructions. For an ARM processor, an exception mode (trap) for a non-existent instruction is also possible. For ARM7 vector 0x4

What happens if the following instruction is encountered:

  1. If you make an empty handler, the processor will hang.

  2. Alternatively, you can write "the program has executed an invalid instruction and will be closed" (many versions of Windows do this).

  3. If there is emulation, the command will be executed by the emulator

Now about why the emulation of commands. Case with x86 486. There is a wonderful command bswap eax; which is not supported by 386. Knowing in advance that this command needs to be executed, we write a program, hang it up for an interrupt by a non-existent command, where if data [(e) ip] = code_bswap then we exchange within eax. Then we shift (e) ip by the length of the command and make a return. Alternatively, you can emulate the mmx extension. It is possible to emulate amd commands on intel and vice versa. But this will only apply to this platform.

Now about the AWP. There is a program for ARM7, theoretically it can be executed on ARM6 if you know all the missing commands in this platform and implement their execution in the form of procedures.

About cross-platform. The x86 instructions and the ARM instructions will never overlap in code. Therefore, the processing of non-existent commands will be different for each processor. Yes, you can use #define delimit and sharpen C ++ on two three different platforms by implementing the solution in different files – yes, and it is possible to somehow combine everything in proc1(PTR * addr, REGS * reg) but even the processors have different registers … Therefore, there is no particular sense in this. For each processor platform, you will have to write your own unique emulation, which takes into account the peculiarities of this particular architecture.

Is it possible to somehow compile a program with an invalid instruction? The answer is yes, if the processor has such (there are processors where the entire instruction space is full and there are none, as a rule of RISC architecture). this will be only when the software is built for one version of architecture, but is executed on another (for example, it was assembled under 486 and executed on 286, or assembled under Pentium-III, but executed on Pentium-II), or we intentionally generate code for the wrong platform under which it is assembled (using __emit__ or execute commands in an array). But this is not usually done. In order for the program to work, emulation is done at the OS kernel level. And the last thing – emulation slows down the execution significantly :).

In the context of processor exceptions, three concepts are used, which often in the literature can be both "synonyms" and different concepts (which sometimes causes confusion): interrupt (int for short) – interrupt (both by exception and not), trap – trap ( means an exceptional situation), exception – an exception.

Scroll to Top