How to mask bytes in ARM assembly?

前端 未结 5 1053
醉酒成梦
醉酒成梦 2021-02-04 15:53

i have got an 32bit (hexadecimal)word 0xaabbccdd and have to swap the 2. and the 3. byte. in the end it should look like 0xaaccbbdd

how can i \"mask\" the 2nd and the 3r

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 16:03

    Back in the day we used to rely heavily on EOR for this kind of trickery.

    You can do it in 4 cycles.

    First off, we need the fact that: A ^ (A^B) = B

    We start with 0xAABBCCDD, and we want 0xAACCBBDD. To get there, we need 0x00EEEE00^0xAABBCCDD, where EE = BB^CC.

    Now, we need a few cycles to build 00EEEE00:

    eor     r1,r0,r0,lsr #8
    and     r1,r1,#0xFF00
    orr     r1,r1,r1,lsl #8
    eor     r0,r0,r1
    

    In c:

    t=x^(x>>8);
    t=t&0xFF00;
    t=t|(t<<8);
    x^=t;
    

    After each line, the result calculated is: starting with: AABBCCDD

    eor  XXXXEEXX
    and  0000EE00
    orr  00EEEE00
    eor  AACCBBDD
    

    This will work on any 32bit ARM core.

提交回复
热议问题