iPhone 的汇编 (.s) 代码中的条件编译 - 如何进行?

发布于 2024-08-10 22:20:21 字数 570 浏览 4 评论 0原文

我在 .s 文件中有几行汇编器arm代码。只是我需要调用的一些例程。在为设备构建时它工作正常,但是当我切换到 iPhone Simulator 时,我收到“没有这样的指令”错误。我尝试用我所知道的有条件地编译 .s 文件的部分内容:

#if !TARGET_IPHONE_SIMULATOR

但是汇编器无法识别这些预处理器指令(当然),而且我记得或发现的汇编器条件编译技术都不起作用,所以我'我现在正在绞尽脑汁地思考如何在构建模拟器时避免编译该汇编程序代码。我也没有在 Xcode 中看到允许我根据目标平台编译或不编译文件的项目选项。

已解决:

我所缺少的是汇编程序文件中正确的#import。我没有想到添加它,因为 Xcode 语法以绿色突出显示了任何预处理器指令(注释),这让我假设这些命令未被识别,而实际上它们工作得很好。

这有效:

#import "TargetConditionals.h"

#if !TARGET_IPHONE_SIMULATOR

... asm code here ...

#endif

I have a few lines of assembler arm code in an .s file. Just a few routines i need to call. It works fine when building for the device, however when i switch to iPhone Simulator i get "no such instruction" errors. I tried to compile parts of the .s file conditionally with what i know:

#if !TARGET_IPHONE_SIMULATOR

But the assembler doesn't recognize these preprocessor directives (of course) and none of the conditional compilation techniques for assembler that i could remember or find worked, so i'm scratching my head now on how to avoid compilation of that assembler code when building for the Simulator. I also don't see a project option in Xcode that would allow me to compile the file or not depending on the target platform.

SOLVED:

All i was missing was the proper #import in the assembler file. I did not think of adding it because Xcode syntax highlighted any preprocessor directive in green (comment) which made me assume that these commands are not recognized when in fact they work just fine.

This works:

#import "TargetConditionals.h"

#if !TARGET_IPHONE_SIMULATOR

... asm code here ...

#endif

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星軌x 2024-08-17 22:20:21

您可以使用预处理器宏来完成此操作。它们在 TargetConditionals.h TARGET_IPHONE_SIMULATOR 中定义,应该在那里! (但是你确实需要 #include 它。)

You do do it with a pre-processor macro. They are defined in TargetConditionals.h TARGET_IPHONE_SIMULATOR should be there! (You do need to #include it however.)

深居我梦 2024-08-17 22:20:21

这是我用来检测 ARM、Thumb 与 Simulator 的代码:

#include "TargetConditionals.h"

#if defined(__arm__)
# if defined(__thumb__)
#  define COMPILE_ARM_THUMB_ASM 1
# else
#  define COMPILE_ARM_ASM 1
# endif
#endif

#if TARGET_IPHONE_SIMULATOR
  // Simulator defines
#else
  // ARM or Thumb mode defines
#endif

// 以下是您如何使用它

uint32_t
test_compare_shifted_operand(uint32_t w1) {
  uint32_t local;
#if defined(COMPILE_ARM_ASM)
  const uint32_t shifted = (1 << 8);
  __asm__ __volatile__ (
                        "mov %[w2], #1\n\t"
                        "cmp %[w2], %[w1], lsr #8\n\t"
                        "moveq %[w2], #10\n\t"
                        "movne %[w2], #11\n\t"
                        : \
                        [w1] "+l" (w1),
                        [w2] "+l" (local)
                        : \
                        [shifted] "l" (shifted)
                        );
#else // COMPILE_ARM_ASM
  if ((w1 >> 8) == 1) {
    local = 10;
  } else {
    local = 11;
  }
#endif // COMPILE_ARM_ASM  
  return local;
}

Here is code I use to detect ARM vs Thumb vs Simulator:

#include "TargetConditionals.h"

#if defined(__arm__)
# if defined(__thumb__)
#  define COMPILE_ARM_THUMB_ASM 1
# else
#  define COMPILE_ARM_ASM 1
# endif
#endif

#if TARGET_IPHONE_SIMULATOR
  // Simulator defines
#else
  // ARM or Thumb mode defines
#endif

// And here is how you might use it

uint32_t
test_compare_shifted_operand(uint32_t w1) {
  uint32_t local;
#if defined(COMPILE_ARM_ASM)
  const uint32_t shifted = (1 << 8);
  __asm__ __volatile__ (
                        "mov %[w2], #1\n\t"
                        "cmp %[w2], %[w1], lsr #8\n\t"
                        "moveq %[w2], #10\n\t"
                        "movne %[w2], #11\n\t"
                        : \
                        [w1] "+l" (w1),
                        [w2] "+l" (local)
                        : \
                        [shifted] "l" (shifted)
                        );
#else // COMPILE_ARM_ASM
  if ((w1 >> 8) == 1) {
    local = 10;
  } else {
    local = 11;
  }
#endif // COMPILE_ARM_ASM  
  return local;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文