2

I am trying to create a program which generates an illegal instruction exception for demonstration purposes.

There seems to be a simple solution to my problem:

cmpxchgl %edx, %ecx

But rather than giving me an exception at runtime, the compiler (FASM) doesn't want to compile the code. It says "Illegal instruction" (and that's probably right, actually nice to warn me before).

Any solution for getting an illegal instruction exception is fine for me, so you could

a) point me to another compiler which simply compiles the code

b) apply a workaround for my command

c) propose a completely different command (should be simple, oneliners preferred)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
  • @harold: sorry, I just wanted to prevent ARM answers. I have retagged to x86, also 64 bit is ok. Feel free to retag again if my tags are still not correct. – Thomas Weller Feb 24 '14 at 11:56

2 Answers2

9

Simple: ud2 (0F 0B)

Its entire purpose is to raise an Invalid Opcode Exception.

harold
  • 61,398
  • 6
  • 86
  • 164
1

You could have looked up the machine code yourself from the instruction set reference. I have done it for you, cmpxchgl %edx, %ecx is 0x0F 0xB1 0xCA. Just stick those into your code as DB or .byte or whatever your compiler supports.

By the way, GAS assembles it without a word.

Jester
  • 56,577
  • 4
  • 81
  • 125
  • It seems FASM is just too smart. Even `.byte` gives the compiler error. – Thomas Weller Feb 24 '14 at 12:00
  • 2
    Just checked, fasm uses `DB` directive, and it assembles fine. But see harold's solution. – Jester Feb 24 '14 at 12:03
  • I don't fully understand the Intel instruction set reference, so I didn't figure out which bytes to use. Your byte sequence works well, there's no illegal instruction for me. – Thomas Weller Feb 24 '14 at 12:43