Mixing C and Assembly files

后端 未结 4 982
囚心锁ツ
囚心锁ツ 2020-12-16 23:24

I want to use a naked function in my C++ program using g++. Unfortunately g++, unlike VC++, does not support naked functions and the only way to manage this is to write your

4条回答
  •  清歌不尽
    2020-12-17 00:02

    Here's an example of a trick to achieve the "naked function" effect.

    #include 
    
    extern "C" int naked_func ();
    
    static void
    dummy ()
    {
      __asm__ __volatile__
        (
         "  .global naked_func\n"
         "naked_func:\n"
         "  movq    $3141569, %rax\n"
         "  ret\n"
         );
    }
    
    int
    main ()
    {
      printf ("%d\n", naked_func ());
      return 0;
    }
    

提交回复
热议问题