24 lines · c
1void __attribute__((naked)) cas(int *a, int *b) {2 // This atomic sequence implements a copy-and-swap function. This test should3 // stop at the first instruction, and after step instruction, we should stop4 // at the end of the sequence (on the ret instruction).5 asm volatile("1:\n\t"6 "lr.w a2, (a0)\n\t"7 "and a5, a2, a4\n\t"8 "beq a5, a1, 2f\n\t"9 "xor a5, a2, a0\n\t"10 "and a5, a5, a4\n\t"11 "xor a5, a2, a5\n\t"12 "sc.w a5, a1, (a3)\n\t"13 "beqz a5, 1b\n\t"14 "nop\n\t"15 "2:\n\t"16 "ret\n\t");17}18 19int main() {20 int a = 4;21 int b = 2;22 cas(&a, &b);23}24