Best way in D2 to rotate a ubyte[4] array

bearophile bearophileHUGS at lycos.com
Thu Mar 10 14:01:22 PST 2011


While creating the rotation code I have found two things I don't understand. Maybe some of you is able to help me understand.

This version of the code:


union Four {
    uint u;
    ubyte[4] a;
}
void main() {
    Four f;
    asm {
        rol f.u, 8;
    }
}


DMD 2.052 gives this error, do you know why?

test.d(8): bad type/size of operands 'f.u'

----------------------------------

So to avoid wasting load asm instructions I have tried to write it like this:


union Four {
    ubyte[4] arr;
    uint ui;
}
void main() {
    Four fo;
    fo.arr[0] = 1;
    fo.arr[1] = 2;
    fo.arr[2] = 3;
    fo.arr[3] = 4;
    uint* uptr = &(fo.ui);
    asm {
        rol [uptr], 8;
    }
    asm {
        rol uptr, 8;
    }
}


but looking at the asm it produces, do you know why the rol with [uptr] and uptr are translated to the same instruction (so it rotates the pointer instead of the pointed uint)?


__Dmain comdat
    push EBP
    mov EBP,ESP
    sub ESP,8
    mov dword ptr -8[EBP],0
    lea EAX,-8[EBP]
    mov byte ptr -8[EBP],1
    mov byte ptr -7[EBP],2
    mov byte ptr -6[EBP],3
    mov byte ptr -5[EBP],4
    mov -4[EBP],EAX
    rol -4[EBP],8  ; <======
    rol -4[EBP],8  ; <======
    mov ESP,EBP
    pop EBP
    ret

Bye and thank you,
bearophile


More information about the Digitalmars-d-learn mailing list