PolarSSL的asm代码在iPad4和模拟器中执行不同的结果

时间:2014-01-07 09:10:05

标签: ios assembly arm i386 polarssl

我正在开发一个加密和解密app.I使用bignum算法。这是polarSSL。 在Xcode模拟器中,它运行良好,可以成功解密文件。但是当我在真实设备(iPad 4)上运行相同的项目时,它崩溃了。

所以我调试它,我找到了原因。在模拟器中,它执行以下代码:

 #if defined(__i386__)
#define MULADDC_INIT                            \
    asm( "movl   %0, %%esi      " :: "m" (s));  \
    asm( "movl   %0, %%edi      " :: "m" (d));  \
    asm( "movl   %0, %%ecx      " :: "m" (c));  \
    asm( "movl   %0, %%ebx      " :: "m" (b));

#define MULADDC_CORE                            \
    asm( "lodsl                 " );            \
    asm( "mull   %ebx           " );            \
    asm( "addl   %ecx,   %eax   " );            \
    asm( "adcl   $0,     %edx   " );            \
    asm( "addl   (%edi), %eax   " );            \
    asm( "adcl   $0,     %edx   " );            \
    asm( "movl   %edx,   %ecx   " );            \
    asm( "stosl                 " );

#define MULADDC_STOP                            \
    asm( "movl   %%ecx, %0      " :: "m" (c));  \
    asm( "movl   %%edi, %0      " :: "m" (d));  \
    asm( "movl   %%esi, %0      " :: "m" (s) :  \
    "eax", "ecx", "edx", "ebx", "esi", "edi" );

while in iPad4, it execute the following code:
#if defined(__arm__)

#define MULADDC_INIT                            \
    asm( "ldr    r0, %0         " :: "m" (s));  \
    asm( "ldr    r1, %0         " :: "m" (d));  \
    asm( "ldr    r2, %0         " :: "m" (c));  \
    asm( "ldr    r3, %0         " :: "m" (b));

#define MULADDC_CORE                            \
    asm( "ldr    r4, [r0], #4   " );            \
    asm( "mov    r5, #0         " );            \
    asm( "ldr    r6, [r1]       " );            \
    asm( "umlal  r2, r5, r3, r4 " );            \
    asm( "adds   r7, r6, r2     " );            \
    asm( "adc    r2, r5, #0     " );            \
    asm( "str    r7, [r1], #4   " );

#define MULADDC_STOP                            \
    asm( "str    r2, %0         " :: "m" (c));  \
    asm( "str    r1, %0         " :: "m" (d));  \
    asm( "str    r0, %0         " :: "m" (s) :  \
    "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7" );

原因很明显,设备的cpu是ARM架构而模拟器是i386.但是i386代码可以成功执行而arm失败让我感到困惑。这些asm代码来自polarSSL,用标准arm指令编写。    所以我认为Apple Inc是否改变了CPU体系结构,正如我们所知,iPad 4的cpu是由Apple设计的A6X。    所以我不知道如何解决这个问题。希望有人可以帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

根据您提供的代码,您使用的是旧版本的库!

如果检查'当前'bn_mul.h,您将看到语句已更改为'单个'asm语句(这是编译器优化所必需的)。

请尝试新的并报告:)