1

I am using an assembly code in a C function. But gcc gives unsupported instruction 'mov' error for the following code:

uint32_t faulting_address;
asm volatile("mov %%cr2, %0" : "=r" (faulting_address));  

I don't understand what is wrong with this.

2
  • GCC's inline assembly uses AT&T syntax.
    – user3920237
    Aug 24, 2014 at 2:27
  • 1
    Thank you, I tried asm volatile("movl $0, %%cr2" : "=r" (faulting_address)) but nothing changes. Aug 24, 2014 at 2:40

1 Answer 1

2

The syntax is correct, but the error is more than likely because you're compiling for a 64-bit target, in which case the mov needs to be 64 bits. Just change faulting_address to a uint64_t and it will compile.

Alternatively, if you want it to be 32 bits, you'll need to use -m32 or other similar option to compile for a 32-bit target.

2
  • -m32 worked for me thanks. For linking those files should I use -melf_i386? Aug 24, 2014 at 4:41
  • uintptr_t would pick the right width depending on mode. (Except for the x32 ABI; 32-bit pointers in 64-bit mode. But you wouldn't normally use that for an x86-64 kernel, and CRs aren't readable in user-space.) May 1, 2022 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.