#include <stdio.h>
void fun();
int main()
{
int a = 10;
fun();
return 0;
}
void fun()
{
int a = 5;
}
Assembly code.
000103e4 <main>:
103e4: e52db008 str fp, [sp, #-8]!
103e8: e58de004 str lr, [sp, #4]
103ec: e28db004 add fp, sp, #4
103f0: e24dd008 sub sp, sp, #8
103f4: e3a0300a mov r3, #10
103f8: e50b3008 str r3, [fp, #-8]
103fc: eb000005 bl 10418 <fun>
10400: e3a03000 mov r3, #0
10404: e1a00003 mov r0, r3
10408: e24bd004 sub sp, fp, #4
1040c: e59db000 ldr fp, [sp]
10410: e28dd004 add sp, sp, #4
10414: e49df004 pop {pc} ; (ldr pc, [sp], #4)
00010418 <fun>:
10418: e52db004 push {fp} ; (str fp, [sp, #-4]!)
1041c: e28db000 add fp, sp, #0
10420: e24dd00c sub sp, sp, #12
10424: e3a03005 mov r3, #5
10428: e50b3008 str r3, [fp, #-8]
1042c: e1a00000 nop ; (mov r0, r0)
10430: e24bd000 sub sp, fp, #0
10434: e49db004 pop {fp} ; (ldr fp, [sp], #4)
10438: e12fff1e bx lr
In a above assembly code 103e4: e52db008 str fp, [sp, #-8]!
I am new to assembly language. why '!' has been used what is the purpose.
That !
in ARM assembly code means the address register is updated. (It is very exciting!) In str fp, [sp, #-8]!
, −8 is added to sp
, and then the contents of fp
are stored at the new address in sp
. If the !
were not present, fp
would still be stored at the same address, but sp
would not be updated.
There is a corresponding instruction which you see elsewhere in the code, ldr fp, [sp], #4
. In this form, the contents of fp
are stored at the address in sp
, and then 4 is added to sp
.
The first form is commonly used for pushing things onto the stack: The stack pointer is decremented to make room for new things on the stack, and then the value is written. The second form is used for popping: The value on the stack is read, and then the stack pointer is incremented, effectively removing the space from the live part of the stack.
They can also be used to iterate through an array, loading or storing elements and moving the pointer without needing separate instructions for loading/storing and changing the pointer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.