Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.

Custom instruction opcode inside code

morca
Beginner
2,223 Views

I have question in mind and have tested that with GCC without success. So, I would like to know if it is *indeed* possible to do that with GCC or ICC. Actually the question is about machine instructions fetched by CPU.

I have written an assembly byte (0x00) as a code inside the main function. But after the compilation, according to the GDB, the 00 opcode is followed by the next instructions. In other words, the compiler (or gdb, I don't know) assumes that 00 is the add instructions so, it automatically appends other byte sequences to that. I don't want that!

 

(gdb) set disassembly-flavor intel
(gdb) list
1       void main()
2       {
3         __asm__(".byte 0x00");
4       }
(gdb) disass /r main
Dump of assembler code for function main:
   0x00000000004004ed <+0>:     55      push   rbp
   0x00000000004004ee <+1>:     48 89 e5        mov    rbp,rsp
   0x00000000004004f1 <+4>:     00 5d c3        add    BYTE PTR [rbp-0x3d],bl
End of assembler dump. 

So, I want to see

  0x00000000004004ed <+0>:     55      push   rbp
  0x00000000004004ee <+1>:     48 89 e5        mov    rbp,rsp  
    ==========>                00            SOMETHING
  0x00000000004004f1 <+4>:     5d      pop    rbp    
  0x00000000004004f2 <+5>:     c3      ret

 

Actually I want to get an invalid opcode exception by the processor itself and not segmentation fault raised by software.

Any comment? Can i accomplish that with ICC?

0 Kudos
22 Replies
morca
Beginner
105 Views

No, you should not insert __asm__ to generate invalid instruction sequences. You got what you asked the compiler to generate.

I think that is the only way to generate machine code directly as far as I know. That means, I was in the wrong direction for my investigations. From what I have read from Chris Domas experiments on unknown opcodes, it *is* possible to give a random sequence of 15-bytes (15 is the maximum length) and observe 1) the instructions length and 2) if it is valid/invalid instruction.

Do you mean I have to use an assembler, e.g NASM, instead of a compiler?

jimdempseyatthecove
Black Belt
105 Views

>>Do you mean I have to use an assembler, e.g NASM, instead of a compiler?

No. The __asm__ statement will do.

Keep in mind that invalid instruction sequences on CPU x may become valid on CPU y.

For example the REPxx prefixes being use on formerly undefined instructions for the RTM XACQUIRE/XRELEASE.

If you have a compiler that does not support XACQUIRE/XRELEASE but your CPU does support those instructions, then you could use the __asm__ to incorporate these into your program. That is one example.

Jim Dempsey

 

Reply