Electrical – Invalid opcodes

8085microprocessor

What happens when a microprocessor like the 8085 fetches an invalid opcode, i.e. not part of its instruction set, from memory and attempts to decode and run it? I found, in some forum, that invalid opcodes are treated as NOPs, but that's no good since it doesnt inform the programmer that there's a problem. Is this really what happens or is some interrupt (TRAP?) triggered? What if invalid opcodes somehow made their way down to a modern processor? How different is the behaviour, with respect to an 8085's?

I'm asking because I'm building an 8085 trainer which will require students to enter opcodes in hex directly into RAM from where they'll be executed, so I'd like to know what would happen if they enter incorrect code. Also, how likely is it that the one wrong opcode will ruin the rest of the program? Thanks.

Best Answer

With something like an 8085 processor, the result is probably "undefined behavior". Those 1970s devices had limited logic available for instruction decoding, and they designed the opcodes to require minimal decoding effort. For example, maybe every op-code that had a '1' in the 4th bit would result in an update to the accumulator.

These devices wouldn't inform the programmer of anything because they couldn't spare the resources to detect a wrong op-code. It also wouldn't necessarily behave as a NOP, because the fields of the opcode might actually pass through the decode logic and produce some behavior that changed the state of the processor.

It would be the job of the programmer or the compiler to not generate invalid opcodes, which isn't really very difficult when you think about it. A compiler is not going to produce any opcode that it hasn't been programmed to produce.

It seems likely that newer processors, with vastly more resources, can spare some to detect invalid opcodes and produce some defined behavior, but I'm not familiar enough with them to comment on that.

Related Topic