如何编写汇编code调用的C函数 [英] How to write a C function invoked from assembly code

查看:152
本文介绍了如何编写汇编code调用的C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写将从组装code Linux内核调用的C函数。

I need to write a C function that will be invoked from assembly code in linux kernel.

什么特别的问题应予以考虑?

What special issues should be taken into account?

我有一些想法,但有谁能够提供更多的细节:

I have some in mind, but can anybody provide more details:

(1)调用约定

请确保在装配方和在C握手言和被叫方好。但调用约定,我应该使用?我怎么可以声明C函数和组装code申报呢?

Make sure the caller in assembly and the callee in c shake hands well. But which calling convention should I use? How can I declare the c function and declare it in assembly code?

(2)保护寄存器

某些寄存器应该在调用之前被保存在装配。我记得大概他们是EAX,ECX和EDX。但我没有去救他们,除非我需要调用C函数之前引用旧的价值,对吧?

Some registers should be saved in the assembly before the invoke. I roughly remember they are eax, ecx and edx. But I don't have to save them unless I need to refer to the old value before the invoke to the C function, right?

什么其他问题?

推荐答案

当你追问标记 Linux内核看看下面的内核头:的弓/ 86 /有/ ASM / calling.h

As your questing tagged with linux-kernel look at the following kernel header: arch/x86/include/asm/calling.h.

   3 x86 function call convention, 64-bit:
   4 -------------------------------------
   5  arguments           |  callee-saved      | extra caller-saved | return
   6 [callee-clobbered]   |                    | [callee-clobbered] |
   7 ---------------------------------------------------------------------------
   8 rdi rsi rdx rcx r8-9 | rbx rbp [*] r12-15 | r10-11             | rax, rdx [**]
   9
  10 ( rsp is obviously invariant across normal function calls. (gcc can 'merge'
  11   functions when it sees tail-call optimization possibilities) rflags is
  12   clobbered. Leftover arguments are passed over the stack frame.)
  13
  14 [*]  In the frame-pointers case rbp is fixed to the stack frame.
  15
  16 [**] for struct return values wider than 64 bits the return convention is a
  17      bit more complex: up to 128 bits width we return small structures
  18      straight in rax, rdx. For structures larger than that (3 words or
  19      larger) the caller puts a pointer to an on-stack return struct
  20      [allocated in the caller's stack frame] into the first argument - i.e.
  21      into rdi. All other arguments shift up by one in this case.
  22      Fortunately this case is rare in the kernel.
  23
  24 For 32-bit we have the following conventions - kernel is built with
  25 -mregparm=3 and -freg-struct-return:
  26
  27 x86 function calling convention, 32-bit:
  28 ----------------------------------------
  29  arguments         | callee-saved        | extra caller-saved | return
  30 [callee-clobbered] |                     | [callee-clobbered] |
  31 -------------------------------------------------------------------------
  32 eax edx ecx        | ebx edi esi ebp [*] | <none>             | eax, edx [**]
  33
  34 ( here too esp is obviously invariant across normal function calls. eflags
  35   is clobbered. Leftover arguments are passed over the stack frame. )
  36
  37 [*]  In the frame-pointers case ebp is fixed to the stack frame.
  38
  39 [**] We build with -freg-struct-return, which on 32-bit means similar
  40      semantics as on 64-bit: edx can be used for a second return value
  41      (i.e. covering integer and structure sizes up to 64 bits) - after that
  42      it gets more complex and more expensive: 3-word or larger struct returns
  43      get done in the caller's frame and the pointer to the return struct goes
  44      into regparm0, i.e. eax - the other arguments shift up and the
  45      function's register parameters degenerate to regparm=2 in essence.

这篇关于如何编写汇编code调用的C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆