[ARM-assembly]-全局变量/静态全局变量/初始化/未初始化变量的存放位置分析

我们从汇编来看,各种变量在内存中的存放位置

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.o

main.o:     file format elf32-littlearm


Disassembly 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]   ; 6c <main+0x6c>
  18:   e08f2002        add     r2, pc, r2
  1c:   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
  2c:   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]
  3c:   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]
  4c:   e5933000        ldr     r3, [r3]
  50:   e50b3008        str     r3, [fp, #-8]
  54:   e3a03000        mov     r3, #0
  58:   e1a00003        mov     r0, r3
  5c:   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
  6c:   0000004c        andeq   r0, r0, ip, asr #32
  70:   00000040        andeq   r0, r0, r0, asr #32
        ...

Disassembly of section .data:

00000000 <y>:
   0:   0000000a        andeq   r0, r0, sl

00000004 <w>:
   4:   00000014        andeq   r0, r0, r4, lsl r0

Disassembly of section .bss:

00000000 <x>:
   0:   00000000        andeq   r0, r0, r0

Disassembly of section .comment:

00000000 <.comment>:
   0:   43434700        movtmi  r4, #14080      ; 0x3700
   4:   4728203a                        ; <UNDEFINED> instruction: 0x4728203a
   8:   2029554e        eorcs   r5, r9, lr, asr #10
   c:   2e392e34        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}
  1c:   72657270        rsbvc   r7, r5, #112, 4
  20:   61656c65        cmnvs   r5, r5, ror #24
  24:   00296573        eoreq   r6, r9, r3, ror r5

Disassembly of section .ARM.attributes:

00000000 <.ARM.attributes>:
   0:   00002a41        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
  1c:   15011404        strne   r1, [r1, #-1028]        ; 0xfffffbfc
  20:   18031701        stmdane r3, {r0, r8, r9, sl, ip}
  24:   1a011901        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.o

main.o:     file format elf64-littleaarch64


Disassembly 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
  1c:   b9400000        ldr     w0, [x0]
  20:   b9000fe0        str     w0, [sp,#12]
  24:   90000000        adrp    x0, 4 <main+0x4>
  28:   f9400000        ldr     x0, [x0]
  2c:   b9400000        ldr     w0, [x0]
  30:   b9000fe0        str     w0, [sp,#12]
  34:   90000000        adrp    x0, 4 <main+0x4>
  38:   f9400000        ldr     x0, [x0]
  3c:   b9400000        ldr     w0, [x0]
  40:   b9000fe0        str     w0, [sp,#12]
  44:   52800000        mov     w0, #0x0                        // #0
  48:   910043ff        add     sp, sp, #0x10
  4c:   d65f03c0        ret

Disassembly of section .data:

0000000000000000 <y>:
   0:   0000000a        .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:   4728203a        .inst   0x4728203a ; undefined
   8:   2029554e        .inst   0x2029554e ; undefined
   c:   2e392e34        uqsub   v20.8b, v17.8b, v25.8b
  10:   30322078        adr     x24, 6441d <w+0x64419>
  14:   31303531        adds    w17, w9, #0xc0d
  18:   28203332        stnp    w18, w12, [x25,#-256]
  1c:   72657270        .inst   0x72657270 ; undefined
  20:   61656c65        .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:   0000000a        .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
  1c:   b9400000        ldr     w0, [x0]
  20:   b9000fe0        str     w0, [sp,#12]
  24:   90000000        adrp    x0, 4 <main+0x4>
  28:   f9400000        ldr     x0, [x0]
  2c:   b9400000        ldr     w0, [x0]
  30:   b9000fe0        str     w0, [sp,#12]
  34:   90000000        adrp    x0, 4 <main+0x4>
  38:   f9400000        ldr     x0, [x0]
  3c:   b9400000        ldr     w0, [x0]
  40:   b9000fe0        str     w0, [sp,#12]
  44:   52800000        mov     w0, #0x0                        // #0
  48:   910043ff        add     sp, sp, #0x10
  4c:   d65f03c0        ret
上一篇:深耕智慧教育十六年,科大讯飞让「因材施教」变得不一样


下一篇:[CLR via C#]9. 参数