从swift调用在汇编中定义的函数 [英] calling a function defined in assembly from swift

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

问题描述

我知道很快可以在swift模块中的函数上使用@_silgen_name attr与c互操作.有没有办法用程序集文件中定义的符号来执行此操作?我想使用Swift进行系统调用.这就是为什么我问.

I know in swift it is possible to interop with c using the @_silgen_name attr on a function in a swift module. Is there a way to do this with a symbol defined in an assembly file? I would like to make syscalls using Swift. This is why I'm asking.

推荐答案

创建网桥header.h文件,并将该函数的原型放入该文件中.

Create the bridge header.h file and put the prototype of the function in that file.

例如您的汇编代码:

.globl _add // make it global so that others can find this symbol
....
_add: // int add(int a, int b)
  movl %esi, %eax
  addl %edi, %eax
  ret

然后在桥接header.h文件中

Then in bridging header.h file

int add(int a, int b);

OR

在swift模块的顶部定义此

define this at the top of the swift module

@_silgen_name("add") func add(a: Int32, b: Int32) -> Int32

然后迅速使用它:

let a = add(1, 2);

这篇关于从swift调用在汇编中定义的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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