我們從匯編來看,各種變量在內存中的存放位置
1、使用交叉編譯器將C語言轉換成匯編
寫一個C語言程序:
static int x
;
static int y
= 10 ;
int z
;
int w
= 20 ; int main ( )
{ int s
; s
= x
; s
= y
; s
= z
; s
= w
; return 0 ;
}
(arm32) 使用交叉編譯器將其編譯成.o文件 : arm-linux-androideabi-gcc-4.9 -c main.c 使用objdump工具將.o反匯編:arm-linux-androideabi-objdump -d main.o
hehezhou@buildsrv
- 165 : ~ / workspace
/ test
/ test1$ arm
- linux
- androideabi
- objdump
- D main
. omain
. o
: file format elf32
- littlearmDisassembly of section
. text
: 00000000 < main
> : 0 : e52db004 push
{ fp
} ; ( str fp
, [ sp
, #
- 4 ] ! ) 4 : e28db000 add fp
, sp
, #
0 8 : e24dd00c sub sp
, sp
, #
12 c
: e59f3054 ldr r3
, [ pc
, #
84 ] ; 68 < main
+ 0x68 > 10 : e08f3003 add r3
, pc
, r3
14 : e59f2050 ldr r2
, [ pc
, #
80 ] ; 6 c
< main
+ 0x6c > 18 : e08f2002 add r2
, pc
, r2
1 c
: e5922000 ldr r2
, [ r2
] 20 : e50b2008 str r2
, [ fp
, #
- 8 ] 24 : e59f2044 ldr r2
, [ pc
, #
68 ] ; 70 < main
+ 0x70 > 28 : e08f2002 add r2
, pc
, r2
2 c
: e5922000 ldr r2
, [ r2
] 30 : e50b2008 str r2
, [ fp
, #
- 8 ] 34 : e59f2038 ldr r2
, [ pc
, #
56 ] ; 74 < main
+ 0x74 > 38 : e7932002 ldr r2
, [ r3
, r2
] 3 c
: e5922000 ldr r2
, [ r2
] 40 : e50b2008 str r2
, [ fp
, #
- 8 ] 44 : e59f202c ldr r2
, [ pc
, #
44 ] ; 78 < main
+ 0x78 > 48 : e7933002 ldr r3
, [ r3
, r2
] 4 c
: e5933000 ldr r3
, [ r3
] 50 : e50b3008 str r3
, [ fp
, #
- 8 ] 54 : e3a03000 mov r3
, #
0 58 : e1a00003 mov r0
, r3
5 c
: e24bd000 sub sp
, fp
, #
0 60 : e49db004 pop
{ fp
} ; ( ldr fp
, [ sp
] , #
4 ) 64 : e12fff1e bx lr
68 : 00000050 andeq r0
, r0
, r0
, asr r0
6 c
: 0000004 c andeq r0
, r0
, ip
, asr #
32 70 : 00000040 andeq r0
, r0
, r0
, asr #
32 . . . Disassembly of section
. data
: 00000000 < y
> : 0 : 0000000 a andeq r0
, r0
, sl
00000004 < w
> : 4 : 00000014 andeq r0
, r0
, r4
, lsl r0Disassembly of section
. bss
: 00000000 < x
> : 0 : 00000000 andeq r0
, r0
, r0Disassembly of section
. comment
: 00000000 < . comment
> : 0 : 43434700 movtmi r4
, #
14080 ; 0x3700 4 : 4728203 a
; < UNDEFINED
> instruction
: 0x4728203a 8 : 2029554 e eorcs r5
, r9
, lr
, asr #
10 c
: 2e392 e34 mrccs
14 , 1 , r2
, cr9
, cr4
, { 1 } 10 : 30322078 eorscc r2
, r2
, r8
, ror r0
14 : 31303531 teqcc r0
, r1
, lsr r5
18 : 28203332 stmdacs r0
! , { r1
, r4
, r5
, r8
, r9
, ip
, sp
} 1 c
: 72657270 rsbvc r7
, r5
, #
112 , 4 20 : 61656 c65 cmnvs r5
, r5
, ror #
24 24 : 00296573 eoreq r6
, r9
, r3
, ror r5Disassembly of section
. ARM
. attributes
: 00000000 < . ARM
. attributes
> : 0 : 00002 a41 andeq r2
, r0
, r1
, asr #
20 4 : 61656100 cmnvs r5
, r0
, lsl #
2 8 : 01006962 tsteq r0
, r2
, ror #
18 c
: 00000020 andeq r0
, r0
, r0
, lsr #
32 10 : 45543505 ldrbmi r3
, [ r4
, #
- 1285 ] ; 0xfffffafb 14 : 08040600 stmdaeq r4
, { r9
, sl
} 18 : 12010901 andne r0
, r1
, #
16384 ; 0x4000 1 c
: 15011404 strne r1
, [ r1
, #
- 1028 ] ; 0xfffffbfc 20 : 18031701 stmdane r3
, { r0
, r8
, r9
, sl
, ip
} 24 : 1 a011901 bne
46430 < w
+ 0x4642c > 28 : Address
0x0000000000000028 is out of bounds
.
(arm64) 使用交叉編譯器將其編譯成.o文件 : aarch64-linux-android-gcc-4.9 -c main.c 使用objdump工具將.o反匯編:aarch64-linux-android-objdump -D main.o
hehezhou@buildsrv
- 165 : ~ / workspace
/ test
/ test1$ aarch64
- linux
- android
- objdump
- D main
. omain
. o
: file format elf64
- littleaarch64Disassembly of section
. text
: 0000000000000000 < main
> : 0 : d10043ff sub sp
, sp
, #
0x10 4 : 90000000 adrp x0
, 0 < main
> 8 : 91000000 add x0
, x0
, #
0x0 c
: b9400000 ldr w0
, [ x0
] 10 : b9000fe0 str w0
, [ sp
, #
12 ] 14 : 90000000 adrp x0
, 0 < main
> 18 : 91000000 add x0
, x0
, #
0x0 1 c
: b9400000 ldr w0
, [ x0
] 20 : b9000fe0 str w0
, [ sp
, #
12 ] 24 : 90000000 adrp x0
, 4 < main
+ 0x4 > 28 : f9400000 ldr x0
, [ x0
] 2 c
: b9400000 ldr w0
, [ x0
] 30 : b9000fe0 str w0
, [ sp
, #
12 ] 34 : 90000000 adrp x0
, 4 < main
+ 0x4 > 38 : f9400000 ldr x0
, [ x0
] 3 c
: b9400000 ldr w0
, [ x0
] 40 : b9000fe0 str w0
, [ sp
, #
12 ] 44 : 52800000 mov w0
, #
0x0 48 : 910043ff add sp
, sp
, #
0x10 4 c
: d65f03c0 retDisassembly of section
. data
: 0000000000000000 < y
> : 0 : 0000000 a
. word
0x0000000a 0000000000000004 < w
> : 4 : 00000014 . word
0x00000014 Disassembly of section
. bss
: 0000000000000000 < x
> : 0 : 00000000 . word
0x00000000 Disassembly of section
. comment
: 0000000000000000 < . comment
> : 0 : 43434700 . inst
0x43434700 ; undefined
4 : 4728203 a
. inst
0x4728203a ; undefined
8 : 2029554 e
. inst
0x2029554e ; undefinedc
: 2e392 e34 uqsub v20
. 8 b
, v17
. 8 b
, v25
. 8 b
10 : 30322078 adr x24
, 6441 d
< w
+ 0x64419 > 14 : 31303531 adds w17
, w9
, #
0xc0d 18 : 28203332 stnp w18
, w12
, [ x25
, #
- 256 ] 1 c
: 72657270 . inst
0x72657270 ; undefined
20 : 61656 c65
. inst
0x61656c65 ; undefined
24 : 00296573 . inst
0x00296573 ; NYI
hehezhou@buildsrv
- 165 : ~ / workspace
/ test
/ test1$
2 以aarch64的匯編分析
(1)、可以看到帶有初始值的static int y = 10 和int w = 20 都存放在data段 未初始值的static int x在bss段
static int x
;
static int y
= 10 ;
int z
;
int w
= 20 ;
Disassembly of section
. data
:
0000000000000000 < y
> : 0 : 0000000 a
. word
0x0000000a 0000000000000004 < w
> : 4 : 00000014 . word
0x00000014 Disassembly of section
. bss
: 0000000000000000 < x
> : 0 : 00000000 . word
0x00000000
(2)、我們再來看匯編代碼
static int x
;
static int y
= 10 ;
int z
;
int w
= 20 ; int main ( )
{ int s
; s
= x
; s
= y
; s
= z
; s
= w
; return 0 ;
}
0000000000000000 < main
> : 0 : d10043ff sub sp
, sp
, #
0x10 4 : 90000000 adrp x0
, 0 < main
> 8 : 91000000 add x0
, x0
, #
0x0 c
: b9400000 ldr w0
, [ x0
] 10 : b9000fe0 str w0
, [ sp
, #
12 ] 14 : 90000000 adrp x0
, 0 < main
> 18 : 91000000 add x0
, x0
, #
0x0 1 c
: b9400000 ldr w0
, [ x0
] 20 : b9000fe0 str w0
, [ sp
, #
12 ] 24 : 90000000 adrp x0
, 4 < main
+ 0x4 > 28 : f9400000 ldr x0
, [ x0
] 2 c
: b9400000 ldr w0
, [ x0
] 30 : b9000fe0 str w0
, [ sp
, #
12 ] 34 : 90000000 adrp x0
, 4 < main
+ 0x4 > 38 : f9400000 ldr x0
, [ x0
] 3 c
: b9400000 ldr w0
, [ x0
] 40 : b9000fe0 str w0
, [ sp
, #
12 ] 44 : 52800000 mov w0
, #
0x0 48 : 910043ff add sp
, sp
, #
0x10 4 c
: d65f03c0 ret
總結
以上是生活随笔 為你收集整理的[ARM-assembly]-全局变量/静态全局变量/初始化/未初始化变量的存放位置分析 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。