#include <stdio.h>
int add(int a, int b) { return a b; }
int mul(int a, int b) { return a * b; }
int callback_self(int a, int b, int (*rec)(int c, int d)) { return rec(a, b); }
int main(int argc, char *argv[]) { printf("[add = %d]\n",callback_self(3,3,add)); printf("[mul = %d]\n",callback_self(3,3,mul)); return 0; }
反汇编:
00000000004005fc <add>: 4005fc: d10043ff sub sp, sp, #0x10 // sp=sp-0x10 400600: b9000fe0 str w0, [sp, #12] // [sp 12]=w0 400604: b9000be1 str w1, [sp, #8] // [sp 8]=w1 400608: b9400fe1 ldr w1, [sp, #12] // w1=[sp 12] 40060c: b9400be0 ldr w0, [sp, #8] // w0=[sp 8] 400610: 0b000020 add w0, w1, w0 // w0=w1 w0 400614: 910043ff add sp, sp, #0x10 // sp=sp 0x10 400618: d65f03c0 ret
000000000040061c <mul>: 40061c: d10043ff sub sp, sp, #0x10 // sp=sp-0x10 400620: b9000fe0 str w0, [sp, #12] // [sp 12]=w0 400624: b9000be1 str w1, [sp, #8] // [sp 8]=w1 400628: b9400fe1 ldr w1, [sp, #12] // w1=[sp 12] 40062c: b9400be0 ldr w0, [sp, #8] // w0=[sp 8] 400630: 1b007c20 mul w0, w1, w0 // w0=w1*w0 400634: 910043ff add sp, sp, #0x10 // sp=sp 0x10 400638: d65f03c0 ret
/* x29 0x7ffffff3f0 549755810800 x30 0x400690 4195984 sp 0x7ffffff3f0 0x7ffffff3f0 pc 0x40063c 0x40063c */ 000000000040063c <callback_self>: 40063c: a9be7bfd stp x29, x30, [sp, #-32]! 400640: 910003fd mov x29, sp // x29 = sp = 0x7ffffff3d0 400644: b9001fa0 str w0, [x29, #28] // 0x7ffffff3ec: 0xfffff41000000003 400648: b9001ba1 str w1, [x29, #24] // 0x7ffffff3e8: 0x0000000300000003 40064c: f9000ba2 str x2, [x29, #16] // 0x7ffffff3e0: 0x00000000004005fc 400650: f9400ba2 ldr x2, [x29, #16] // x2 = 0x4005fc 400654: b9401ba1 ldr w1, [x29, #24] // w1 = 3 400658: b9401fa0 ldr w0, [x29, #28] // w0 = 3 40065c: d63f0040 blr x2 // 0x4005fc(3, 3) 400660: a8c27bfd ldp x29, x30, [sp], #32 40064: d65f03c0 ret
/* x29 0x7ffffff410 549755810832 x30 0x7fb7d9e6e0 548545357536 sp 0x7ffffff410 0x7ffffff410 pc 0x400668 0x400668 */ 0000000000400668 <main>: 400668: a9be7bfd stp x29, x30, [sp, #-32]! 40066c: 910003fd mov x29, sp // x29 = sp = 0x7ffffff3f0 400670: b9001fa0 str w0, [x29, #28] // 0x7ffffff40c: 0x0000000000000001 400674: f9000ba1 str x1, [x29, #16] // 0x7ffffff400: 0x0000007ffffff548 400678: 90000000 adrp x0, 400000 <_init-0x480> 40067c: 9117f000 add x0, x0, #0x5fc // x0 = 0x4005fc 400680: aa0003e2 mov x2, x0 // x2 = 0x4005fc 400684: 52800061 mov w1, #0x3 // w1 = #3 400688: 52800060 mov w0, #0x3 // w0 = #3 40068c: 97ffffec bl 40063c <callback_self> // callback_self(3, 3, 0x4005fc) 400690: 2a0003e1 mov w1, w0 400694: 90000000 adrp x0, 400000 <_init-0x480> 400698: 911e2000 add x0, x0, #0x788 40069c: 97ffff95 bl 4004f0 <printf@plt> 4006a0: 90000000 adrp x0, 400000 <_init-0x480> 4006a4: 91187000 add x0, x0, #0x61c // x0 = 0x40061c 4006a8: aa0003e2 mov x2, x0 // x2 = 0x40061c 4006ac: 52800061 mov w1, #0x3 // #3 4006b0: 52800060 mov w0, #0x3 // #3 4006b4: 97ffffe2 bl 40063c <callback_self> // callback_self(3, 3, 0x40061c) 4006b8: 2a0003e1 mov w1, w0 4006bc: 90000000 adrp x0, 400000 <_init-0x480> 4006c0: 911e6000 add x0, x0, #0x798 // 0x400798: 0x25203d206c756d5b 4006c4: 97ffff8b bl 4004f0 <printf@plt> 4006c8: 52800000 mov w0, #0x0 // #0 4006cc: a8c27bfd ldp x29, x30, [sp], #32 4006d0: d65f03c0 ret