Rate This Document
Findability
Accuracy
Completeness
Readability

Rewriting Assembly Instructions in Code

Symptom

The assembly language of ARM is different from that of x86 and needs to be rewritten. The code that uses inline assembly needs to be modified accordingly for ARM.

Procedure

Rewrite the assembly code segments.

Example:

  • In the x86 architecture:
    static inline long atomic64_add_and_return(long i, atomic64_t *v) 
    { 
    long i = i; 
    asm_volatile_( 
    "lock ; " "xaddq %0, %1;" 
    :"=r"(i) 
    :"m"(v->counter), "0"(i)); 
    return i + __i; 
    } 
    
    static inline void prefetch(void *x) 
    { 
    asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x)); 
    }
  • On the ARM64 platform, use the GCC built-in functions to implement the following instructions:
    static __inline__ long atomic64_add_and_return(long i, atomic64_t *v) 
    { 
    return __sync_add_and_fetch(&((v)->counter), i); 
    } 
    #define prefetch(_x) __builtin_prefetch(_x) 
    
    For example, for __sync_add_and_fetch, the corresponding disassembly code is as follows:
    <__sync_add_and_fetch >: 
    ldxr   x2, [x0] 
    add   x2, x2, x1 
    stlxr   w3, x2, [x0]