函数指针:
- #include <stdlib.h>
- #include <stdio.h>
- void add()
- {
- printf("12\n");
- }
- int main()
- {
- int * func;
- func = (int *)add;
- return 0;
- }
- .file "main.c"
- .section .rodata
- .LC0:
- .string "12"
- .text
- .globl add
- .type add, @function
- add:
- pushl %ebp
- movl %esp, %ebp
- subl $24, %esp
- movl $.LC0, (%esp)
- call puts
- leave
- ret
- .size add, .-add
- .globl main
- .type main, @function
- main:
- pushl %ebp
- movl %esp, %ebp
- subl $16, %esp
- movl $add, -4(%ebp)
- movl $0, %eax
- leave
- ret
- .size main, .-main
- .ident "GCC: (GNU) 4.5.1 20100924 (Red Hat 4.5.1-4)"
- .section .note.GNU-stack,"",@progbits
- (gdb) disassemble main
- Dump of assembler code for function main:
- 0x080483c8 <+0>: push %ebp
- 0x080483c9 <+1>: mov %esp,%ebp
- 0x080483cb <+3>: sub $0x10,%esp
- 0x080483ce <+6>: movl $0x80483b4,-0x4(%ebp)
- 0x080483d5 <+13>: mov $0x0,%eax
- 0x080483da <+18>: leave
- 0x080483db <+19>: ret
- End of assembler dump.
- (gdb) p add
- $1 = {<text variable, no debug info>} 0x80483b4 <add>
- (gdb)
指针的指针:
- #include <stdlib.h>
- #include <stdio.h>
- void main()
- {
- int a = 0x12;
- int *p1 = &a;
- int **p2 = &p1;
- int ***p3 = &p3;
- }
- (gdb) disassemble main
- Dump of assembler code for function main:
- 0x08048394 <+0>: push %ebp
- 0x08048395 <+1>: mov %esp,%ebp
- 0x08048397 <+3>: sub $0x10,%esp
- 0x0804839a <+6>: movl $0x12,-0x8(%ebp)
- 0x080483a1 <+13>: lea -0x8(%ebp),%eax
- 0x080483a4 <+16>: mov %eax,-0xc(%ebp)
- 0x080483a7 <+19>: lea -0xc(%ebp),%eax
- 0x080483aa <+22>: mov %eax,-0x4(%ebp)
- 0x080483ad <+25>: lea -0x10(%ebp),%eax
- 0x080483b0 <+28>: mov %eax,-0x10(%ebp)
- 0x080483b3 <+31>: leave
- 0x080483b4 <+32>: ret
- End of assembler dump.
- (gdb)
