`
yaojingguo
  • 浏览: 208136 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Sample Code to Brennan's Guide to Inline Assembly

阅读更多

#include <stdio.h>

int times5(int n) {
  asm ("leal (%1,%1,4), %0"
      : "=r" (n)
      : "0" (n) );
  return n;
}

void test_times5() {
  int n = 2;
  printf("%d\n", times5(n));
}

#define rep_movsl(src, dest, numwords) \
  __asm__ __volatile( \
      "cld\n\t" \
      "rep movsl" \
      : \
      : "S" (src), "D" (dest), "c" (numwords))

void test_rep_movsl() {
  int src[] = {100, 200, 300};
  int dest[3];
  rep_movsl(src, dest, 3);

  int i;
  for (i = 0; i < 3; i++)
    printf("dest[%d]: %d\n", i, dest[i]);
}

#define RDTSC(ll) ({ \
    __asm__ __volatile__ ( \
      ".byte 0x0f; .byte 0x31" \
      : "=A" (ll) ); })

void test_RDTSC() {
  unsigned long long ll = 0;
  RDTSC(ll);
  printf("time-stamp counter: %#llX", ll);
}

int main(int argc, const char *argv[]) {
  test_times5();
  test_rep_movsl();
  test_RDTSC();
  return 0;
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics