如何在 XCode 中使用 ARM 汇编程序? [英] How do I use the ARM assembler in XCode?
问题描述
仅出于教育目的,我想向现有的 iPhone 应用程序添加一个函数,该应用程序是用 ARM 程序集编写的.我一般不需要关于 ARM 汇编的教程,因为我已经阅读了太多的教程.我只是不知道如何实际运行代码!
Just for educational purposes, I would like to add a function to an existing iPhone app, written in ARM assembly. I don't need a tutorial on ARM assembly in general, because I already read too many of them. I just don't know how to actually run the code!
我想做的是:
useless.h:
void useless();
useless.s:
useless:
bx lr
如果这也适用于模拟器,那就没问题了...在模拟器上,.s 文件将无法编译,所以我可能应该执行以下操作:
If this also works on the simulator it would be fine... On the simulator, the .s file would not compile, so I should maybe do something like:
useless.s:
#if I_AM_ARM
useless:
bx lr
#endif
useless.c:
#if !I_AM_ARM
void useless()
{
}
#endif
我知道我使用的语法有问题,但我该如何正确书写呢?(仅仅因为我想尝试一些内联汇编而在模拟器上破坏一个应用程序是没有选择的......)
I know the syntax I use is broken, but how do I write it correctly? (Breaking an app on the simulator just because I want to try some inline assembly is no option...)
次佳的选择是使用内联汇编,但我更喜欢非内联汇编.
The second-best option would be to use inline assembly, but I would strongly prefer non-inline assembly.
谢谢!
我想学习ARM汇编,所以我想找到一种方法来编译ARM汇编代码,并执行ARM汇编代码.
推荐答案
我终于自己找到了答案.其实没那么难.不过我只针对 32 位 ARM 版本解决了这个问题.
I finally found the answer myself. It's actually not that hard. I only solved it for the 32-bit ARM version though.
useless.h:
void useless();
useless.s:
#ifdef __arm__
.syntax unified
.globl _useless
.align 2
.code 16
.thumb_func _useless
_useless:
//.cfi_startproc
bx lr
//.cfi_endproc
// CFI means Call Frame Information
// Optionally. Use for better debug-ability.
#endif
useless.c:
#ifndef __arm__
void useless()
{
}
#endif
注意事项:
CLANG ARM 汇编器语法与您在网络上的示例中看到的略有不同.也支持以 //
开头的注释和 /* 多行注释 */
.它还理解标准的 C 预处理器.该函数必须定义为 Thumb 函数,如果您指定 arm 函数(.code 32
),程序将崩溃.行 .thumb_func _useless
可以省略,它仍然有效.我不知道这意味着什么.如果省略 .code 16
行,程序会崩溃.
The CLANG ARM Assembler syntax is a bit different from what you see in example all over the web. Comments start with //
and /* multiline comments */
are also supported. It also understands the standard C preprocessor. The function has to be defined as a Thumb function, if you specify an arm function (.code 32
) the program will just crash. The line .thumb_func _useless
can be ommited and it works still. I have no Idea what it means. If you omit the .code 16
line, the program crashes.
关于#ifdef
.对于 ARMv7,定义了 __arm__
.对于 ARMv8,即 iPhone 5S 上的 64 位变体,没有定义 __arm__
,而是定义了 __arm64__
.上述代码不适用于 64 位 ARM 版本.相反,将使用 useless.c
的实现.(我没有忘记 ARMv7s,只是我目前手上没有带有那个拱形的设备,所以我无法测试.)
about the #ifdef
. For ARMv7, __arm__
is defined. For ARMv8, i.e. the 64bit-variant on the iPhone 5S, __arm__
is not defined, but __arm64__
is defined instead. The above code does not work for the 64bit-ARM-version. Instead, the implementation from useless.c
will be used. (I didn't forget ARMv7s, I just don't have a device with that arch in my hands currently, so I cannot test.)
这篇关于如何在 XCode 中使用 ARM 汇编程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!