我该如何开始使用iOS上的ARM? [英] How do I get started with ARM on iOS?

查看:157
本文介绍了我该如何开始使用iOS上的ARM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是好奇,如何让iOS的下启动的理解ARM。任何帮助将是超好听。

Just curious as to how to get started understanding ARM under iOS. Any help would be super nice.

推荐答案

在我看来,最好的办法上手就是

In my opinion, the best way to get started is to


  1. 写C code(后来的Objective-C)
  2. 小片段
  3. 查看相应的装配code

  4. 找到了足够的了解大会code

  5. 重复!

要做到这一点,你可以使用X code:

To do this you can use Xcode:


  1. 创建一个新的iOS项目(单个视图应用程序是罚款)

  2. 添加C文件scratchpad.c

  3. 在项目生成设置中,将生成调试符号为否

  4. 确保目标是iOS设备,而不是模拟器

  5. 打开了scratchpad.c并打开助理编辑

  6. 设置助理编辑器装配和选择释放

添加下面的函数来scratchpad.c:

Example 1

Add the following function to scratchpad.c:

void do_nothing(void)
{
    return;
}

如果你现在刷新大会的助理编辑,你应该看到很多开始点(指令)线,然后按

If you now refresh the Assembly in the assistant editor, you should see lots of lines starting with dots (directives), followed by

_do_nothing:
@ BB#0:
    bx  lr

现在,让我们忽略的指示进行,并期待在这三行。带着几分在互联网上搜索,你会发现,这些线路分别是:

Let's ignore the directives for now and look at these three lines. With a bit of searching on the internet, you'll find out that these lines are:


  1. 的标签(用下划线pfixed功能$ P $的名称)。

  2. 只是一个由编译器发出的评论。

  3. return语句。在 B 表示分支,忽略 X 现在(它是与指令集之间切换),和 LR 是链接寄存器,存储来电者的返回地址。

  1. A label (the name of the function prefixed with an underscore).
  2. Just a comment emitted by the compiler.
  3. The return statement. The b means branch, ignore the x for now (it has something to do with switching between instruction sets), and lr is the link register, where callers store the return address.

让牛肉它一点,改变code为:

Example 2

Let's beef it up a bit and change the code to:

extern void do_nothing(void);

void do_nothing_twice(void)
{
    do_nothing();
    do_nothing();
}

保存和刷新组装后,您会收到以下code:

After saving and refreshing the assembly, you get the following code:

_do_nothing_twice:
@ BB#0:
    push    {r7, lr}
    mov r7, sp
    blx _do_nothing
    pop.w   {r7, lr}
    b.w _do_nothing

再次带着几分在互联网上搜索,你会发现每一行的意义。一些工作需要做,因为做两个调用:第一次调用需要返回到我们,所以我们需要改变 LR 。这是由 BLX 指令,它并不只分支 _do_nothing 完成,但还存储下一个地址指令(返回地址) LR

Again, with a bit of searching on the internet, you'll find out the meaning of each line. Some more work needs to be done because make two calls: The first call needs to return to us, so we need to change lr. That is done by the blx instruction, which does not only branch to _do_nothing, but also stores the address of the next instruction (the return address) in lr.

由于我们改变返回地址,我们必须存储在某个地方,所以它是压入堆栈。第二跳有一个 .W 后缀,但让我们忽略了现在。为什么不函数这个样子?

Because we change the return address, we have to store it somewhere, so it is pushed on the stack. The second jump has a .w suffixed to it, but let's ignore that for now. Why doesn't the function look like this?

_do_nothing_twice:
@ BB#0:
    push    {lr}
    blx _do_nothing
    pop.w   {lr}
    b.w _do_nothing

这也行,但在iOS中,惯例是存储在 R7 帧指针。帧指针指向我们存放previous帧指针和previous返回地址堆栈的地方。

That would work as well, but in iOS, the convention is to store the frame pointer in r7. The frame pointer points to the place in the stack where we store the previous frame pointer and the previous return address.

那么什么code的作用是:第一,它推动 R7 LR 到堆栈,然后设置 R7 来指向新的堆栈帧(这是在堆栈的顶部,而 SP 点堆栈的顶部),然后将其分支机构的第一次,然后将其恢复 R7 LR ,最后IT部门第二次。 A BX LR 结尾是不需要的,因为调用的函数会返回 LR ,这点对我们的调用者

So what the code does is: First, it pushes r7 and lr to the stack, then it sets r7 to point to the new stack frame (which is on the top of the stack, and sp points to the top of the stack), then it branches for the first time, then it restores r7 and lr, finally it branch for the second time. Abx lr at the end is not needed, because the called function will return to lr, which points to our caller.

让我们来看看最后一个例子:

Let's have a look at a last example:

void swap(int *x, int *y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}

组装code是:

The assembly code is:

_swap:
@ BB#0:
    ldr r2, [r0]
    ldr r3, [r1]
    str r3, [r0]
    str r2, [r1]
    bx  lr

通过一点搜索,你会学到参数和返回值存储在寄存器 R0 - R3 ,而我们可以自由地使用那些为我们的计算。什么code的作用是直接的:它装载值 R0 R1 指向 R2 R3 ,然后将它们存储在背面交换顺序,那么它的分支回来。

With a bit of searching, you will learn that arguments and return values are stored in registers r0-r3, and that we may use those freely for our calculations. What the code does is straightforward: It loads the value that r0 and r1 point to in r2 and r3, then it stores them back in exchanged order, then it branches back.

就是这样:写的小片段,获得足够的信息来大致了解发生了什么事中的每一行,重复。希望帮助!

That's it: Write small snippets, get enough info to roughly understand what's going on in each line, repeat. Hope that helps!

这篇关于我该如何开始使用iOS上的ARM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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