code -> .data & data -> .code

Started by
21 comments, last by fir 10 years, 1 month ago

I did some test (it is a somewhat 'casual' test but i got no energy to

rewrite it

#include <stdio.h>
 
char rtdsc_bin[] =
{
 0x0f, 0x31,
 0xc3
};
 
 
int f(int* x)
{
  x[0] = 10;
  x[1] = 20;
 
}
 
 
char f_bin[] =      //f() body + rdtsc commnd
{
 0x55,
 0x89, 0xE5,
 0x8B, 0x45, 0x08,
 0xC7, 0x00, 0x0A, 0x00, 0x00, 0x00,
 0x8B, 0x45, 0x08,
 0x83, 0xC0, 0x04,
 0xC7, 0x00, 0x14, 0x00, 0x00, 0x00,
 0x5D,
 0x0f, 0x31,  //rdtsc here
 0xC3,
};
 
void main()
{
 
   int (*foo)() ;
   int (*food)(int*) ;
 
   foo = rtdsc_bin;
 
   int a = foo();
   int b = foo();
 
   printf(" %d ",b-a);
 
   static int i[2];
   /////////////////////////////
   food = f_bin;
 
   int c = food(i);
   int d = food(i);
 
   printf(" %d ",d-c);
 
   printf("\n %d ",i[0]);
   printf("\n %d ",i[1]);
 
}
 

and im at least happy with that , seem to be no slowdown

probably here in mingw

results are like

112 119

10

20

112 126

10

20

112 is probably rtdsc cost itself (?) the rest of the function f

takes only few cycles probably - no applied slowdown noticable

(as far as i can be sure) so this machine code trick (one of my favourites) still work

Advertisement

If you're going to put code in the data section, at least pad to a new cache line (often 64 bytes). Ideally, you'd give the code its own page.

If you're going to put code in the data section, at least pad to a new cache line (often 64 bytes). Ideally, you'd give the code its own page.

was not aligned even to 4

SECTION .data   align=4 noexecute                       ; section number 2, data
 
_rtdsc_bin:                                             ; byte
        db 0FH, 31H, 0C3H                               ; 0000 _ .1.
 
_f_bin:                                                 ; byte
        db 55H, 89H, 0E5H, 8BH, 45H, 08H, 0C7H, 00H     ; 0003 _ U...E...
        db 0AH, 00H, 00H, 00H, 8BH, 45H, 08H, 83H       ; 000B _ .....E..
        db 0C0H, 04H, 0C7H, 00H, 14H, 00H, 00H, 00H     ; 0013 _ ........
        db 5DH, 0FH, 31H, 0C3H, 00H                     ; 001B _ ].1..

This topic is closed to new replies.

Advertisement