0

I am trying to follow the instructions in this thread to turn of CPU caches for research purposes. I have tried to convert the code from the page into AT&T format, but am having trouble getting assembler code to compile. Here is the code:

.section .text
        .global main
main:
        mov     %cr0, %eax
        or      $1<<30, %eax
        and     $~(1<<29), %eax
        mov     %eax, %cr0
        wbinvd
        xor     %eax, %eax
        xor     %edx, %edx
        mov     $0x2ff, %ecx
        wrmsr
        wbinvd
        ret

I get the following errors quite persistently:

$ gcc -o asm asm.s
asm.s: Assembler messages:
asm.s:4: Error: unsupported instruction `mov'
asm.s:7: Error: unsupported instruction `mov'

I have tried to replace mov with movl to no effect. You might guess assembly is not my domain. Help... please.

jhu
  • 432
  • 2
  • 10
  • 2
    This is 32 bit code, you tried to assemble as 64 bit. As for the `~` you are missing a leading `$` sign (applies to the preceding `or` as well). You can use `gcc -m32` but you are also missing a `ret` at the end, and it will crash anyway since you are using privileged instructions. – Jester Feb 16 '22 at 14:04
  • Ok.I noticed the literal `$` too. `ret` added. And this compiles with `-m32`, thanks! The plan was to run this on a machine dedicated only for this purpose, so system corruption would not be an issue. However, I notice people recommending a virtual machine for running privileged instructions. On the other hand, the purpose was to disable cache, which in virtual machines is handled by the underlying hardware. This is getting complicated. I wonder what it would take to do the same operation in 64 bit code? – jhu Feb 16 '22 at 14:17
  • 2
    In 64-bit code it is the same except you would use rax instead of eax for the move to/from cr0. – prl Feb 16 '22 at 20:22
  • It’s probably easier to just use `.intel_syntax noprefix` at the beginning to switch GNU as to Intel syntax. – mirabilos Feb 16 '22 at 22:31
  • Even on a virtual machine, you will have to build and run this as a bare-metal program, such as a boot sector. A C-type program, with a `main` function, will expect all the support of an operating system, and that OS will run your code in an unprivileged mode, so these instructions won't work. To use such functionality, you essentially have to *be* the operating system. – Nate Eldredge Feb 17 '22 at 16:10

0 Answers0