我正在研究Objective-C语言如何映射到Assembly。我从iOS Assembly Tutorial上的教程开始。

正在分析的代码段如下。

void fooFunction() {
    int add = addFunction(12, 34);
    printf("add = %i", add);
}


它被翻译成

 _fooFunction:
@ 1:
    push    {r7, lr}
@ 2:
    movs    r0, #12
    movs    r1, #34
@ 3:
    mov r7, sp
@ 4:
    bl  _addFunction
@ 5:
    mov r1, r0
@ 6:
    movw    r0, :lower16:(L_.str-(LPC1_0+4))
    movt    r0, :upper16:(L_.str-(LPC1_0+4))
LPC1_0:
    add r0, pc
@ 7:
    blx _printf
@ 8:
    pop {r7, pc}


关于汇编代码,我无法理解以下两点

->评论@ 1

作者说push将堆栈递减8个字节,因为r7lr分别为4个字节。好。但是他还说这两个值与一条指令一起存储。这是什么意思?

->评论@ 6

movw    r0, :lower16:(L_.str-(LPC1_0+4))
movt    r0, :upper16:(L_.str-(LPC1_0+4))


作者说r0将保存"add = %i"的地址(可以在数据段中找到),但是我真的不了解内存布局的样子。为什么他用黑色虚线而不是红色(我绘制)来代表差异L_.str-(LPC1_0+4)



任何澄清将不胜感激。

编辑

我缺少将r7推入堆栈的概念。推动该价值意味着什么,它包含什么?

最佳答案

但他还说,这两个值与一个一起存储
  指令。这是什么意思?


单个push指令会将两个值都放入堆栈。


  他为什么代表差异L_.str-(LPC1_0 + 4)


因为add r0, pc隐式增加了4个字节。引用instruction set reference

Add an immediate constant to the value from sp or pc, and place the result into a low register.
Syntax: ADD Rd, Rp, #expr
where:
Rd   is the destination register. Rd mustbe in the range r0-r7.
Rp   is either sp or pc.
expr is an expression that evaluates (at assembly time) to a multiple of 4 in the range 0-1020.

If Rp is the pc, the value used is: (the address of the current instruction + 4) AND &FFFFFFFC.

07-24 09:44
查看更多