Aarch64 のアセンブラ

Menu

もとのソース

    extern int printf(const char *,...);
    #define TYPE int
    TYPE f(TYPE a, TYPE b) {
        return a + b;
    }
    int main() 
    {
        TYPE a = 1;
        TYPE b = 2;
        printf("%x = %x + %x \n",f(a,b),a,b);
        return 0;
    }

これを

   clang -O0 -S aho.c

でコンパイルすると aho.s ができる。


関数 f の変更

以下の _f を変更する

    _f:                                     ; @f
            .cfi_startproc
    ; %bb.0:
            mul     w9, w1, w9
            ret
            .cfi_endproc
                                            ; -- End function
            .globl  _main                           ; -- Begin function main
            .p2align        2
    _main:                                  ; @main


実行

 % clang aho-arm.s
 % :44  lldb a.out
    (lldb) stepi
    Process 71648 stopped
    * thread #1, queue = 'com.apple.main-thread', stop reason = instruction step into
        frame #0: 0x0000000100003f3c a.out`f + 4
    a.out`f:
    ->  0x100003f3c <+4>: ret    
    a.out`main:
        0x100003f40 <+0>: sub    sp, sp, #0x30
        0x100003f44 <+4>: stp    x29, x30, [sp, #0x20]
        0x100003f48 <+8>: add    x29, sp, #0x20
    Target 0: (a.out) stopped.
    (lldb) register read
    General Purpose Registers:
            x0 = 0x0000000000000001
            x1 = 0x0000000000000002
            x2 = 0x000000016fdff0a8
            x3 = 0x000000016fdff298
            x4 = 0x000000019a850ac8  dyld`lsl::EphemeralAllocator::allocate_buffer(unsigned long long, unsigned long long, unsigned long long)
            x5 = 0x00000002073efad8  dyld`vtable for lsl::EphemeralAllocator + 72
            x6 = 0x0000000000000000
            x7 = 0x0000000000000db0
            x8 = 0x0000000000000001
            x9 = 0x0000000000000006
           x10 = 0x0000000000000002
           x11 = 0x00000000000002c0
           x12 = 0x0000000000008000
           x13 = 0x1000000000000000
           x14 = 0x0000000000000004
           x15 = 0x0000000000008000
           x16 = 0x000000016fdfee60
           x17 = 0x000000016fdfee60
           x18 = 0x0000000000000000
           x19 = 0x0000000100411b90
           x20 = 0x0000000100003f40  a.out`main
           x21 = 0x000000016fdfee60
           x22 = 0x0000000100411910
           x23 = 0x000000016fdfeee0
           x24 = 0x000000016fdfef20
           x25 = 0x000000019a89e2db  "/usr/lib/dyld"
           x26 = 0x0000000000000000
           x27 = 0x0000000000000000
           x28 = 0x0000000000000000
            fp = 0x000000016fdfee40
            lr = 0x0000000100003f60  a.out`main + 32
            sp = 0x000000016fdfee20
            pc = 0x0000000100003f3c  a.out`f + 4
          cpsr = 0x80001000
    (lldb) dissa _f
    error: 'dissa' is not a valid command.
    (lldb) dis _f
    error: 'disassemble' doesn't take any arguments.
    (lldb) dis 
    a.out`f:
        0x100003f38 <+0>: mul    w9, w1, w9
    ->  0x100003f3c <+4>: ret    
    (lldb) p $pc
    (unsigned long) 4294983484
    (lldb) p (void*) $pc
    (void *) 0x0000000100003f3c
    (lldb) 

Shinji KONO / Fri Oct 4 15:51:27 2024