Reputation: 33428
I'm investigating a little bit on how Objective-C language is mapped into Assembl. I've started from a tutorial found at iOS Assembly Tutorial.
The code snippet under analysis is the following.
void fooFunction() {
int add = addFunction(12, 34);
printf("add = %i", add);
}
It is translated into
_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}
About the assembly code, I cannot understand the following two points
-> Comment @1
The author says that push
decrements the stack by 8 byte since r7
and lr
are of 4byte each. Ok. But he also says that the two values are stored with the one instruction. What does it mean?
-> Comment @6
movw r0, :lower16:(L_.str-(LPC1_0+4))
movt r0, :upper16:(L_.str-(LPC1_0+4))
The author says the that r0
will hold the address of the "add = %i"
(that can be find in the data segment) but I don't really get how the memory layout looks like. Why does he represent the difference L_.str-(LPC1_0+4)
with the dotted black line and not with red one (drawn by me).
Any clarifications will be appreciated.
Edit
I'm missing the concept of pushing r7
onto the stack. What does mean to push that value and what does it contain?
Upvotes: 0
Views: 298
Reputation: 58762
But he also says that the two values are stored with the one instruction. What does it mean?
That the single push
instruction will put both values onto the stack.
Why does he represent the difference L_.str-(LPC1_0+4)
Because the add r0, pc
implicitly adds 4 bytes more. To quote the 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.
Upvotes: 1
Reputation: 4457
For comment 1:
The two values pushed to the stack are the values store in r7
and lr
.
Two 4 byte values equals 8 bytes.
For comment 6:
The label LPC1_0
is followed by the instruction
add r0, pc
which adds another 4 bytes to the difference between the two addresses.
Upvotes: 0