2

I am trying to find out an example of memory reordering, so that i can demonstrate the use of barrier (asm volatile (“” : : : “memory”))

I copied the code from one of the stackoverflow question, and compiled it with gcc -O3 flag. Working of __asm__ __volatile__ ("" : : : "memory")

#include <stdio.h>

int c(int *d, int *e) {
        int r;
        d[0] += 1;
        r = e[0];
        d[1] += 1;
        return r;
}


int main()
{
    int arr[2] = {0x00, 0x01};
    int e[2] = {0x1, 0x2};
    c(arr, e);
    printf("arr[0]:%d\t arr[1]:%d\n", arr[0], arr[1]);
    printf("e[0]:%d\t e[1]:%d\n", e[0], e[1]);
    return 0;
}

$ objdump -D

00000000000006c0 <c>:
 6c0:   83 07 01                addl   $0x1,(%rdi)
 6c3:   8b 06                   mov    (%rsi),%eax
 6c5:   83 47 04 01             addl   $0x1,0x4(%rdi)
 6c9:   c3                      retq   
 6ca:   66 0f 1f 44 00 00       nopw   0x0(%rax,%rax,1)

It's as per the order here. Is there any code which actually demonstrate reordering memory operations.

md.jamal
  • 4,067
  • 8
  • 45
  • 108

0 Answers0