Created
August 12, 2016 17:38
-
-
Save Alcaro/b0d40686a3db50572e6d2f1f50285e76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Byte shenanigans, little endian | |
uint32_t p(uint32_t pix) { uint8_t argb[4]; argb[3-1]=pix>>24; argb[3-2]=pix>>16; argb[3-3]=pix>>8; argb[3-0]=pix>>0; return *(uint32_t*)argb; } | |
_Z1pj: # edi=1234 | |
mov eax, edi # edi=1234 eax=1234 | |
mov edx, edi # edi=1234 eax=1234 edx=1234 | |
shr eax, 24 # edi=1234 eax=0001 edx=1234 | |
shr edx, 16 # edi=1234 eax=0001 edx=0012 | |
sal eax, 16 # edi=1234 eax=0100 edx=0012 | |
mov ah, dl # edi=1234 eax=0120 edx=0012 | |
mov edx, edi # edi=1234 eax=0120 edx=1234 | |
sal edi, 24 # edi=4000 eax=0120 edx=1234 | |
shr edx, 8 # edi=4000 eax=0120 edx=0123 | |
mov al, dl # edi=4000 eax=0123 edx=0123 | |
and eax, 16777215 # edi=4000 eax=0123 edx=0123 (useless opcode?) | |
or eax, edi # edi=4000 eax=4123 edx=0123 | |
ret # return = 4123 | |
Byte shenanigans, big endian (I just removed the ^3s; still actually LE, but the registers have no endianness until you store them) | |
uint32_t p(uint32_t pix) { uint8_t argb[4]; argb[1]=pix>>24; argb[2]=pix>>16; argb[3]=pix>>8; argb[0]=pix>>0; return *(uint32_t*)argb; } | |
_Z1pj: # edi=1234 | |
mov eax, edi # edi=1234 eax=1234 | |
mov ecx, edi # edi=1234 eax=1234 ecx=1234 | |
mov rdx, rdi # edi=1234 eax=1234 ecx=1234 edx=1234 | |
shr eax, 24 # edi=1234 eax=0001 ecx=1234 edx=1234 | |
and ecx, 16711680 # edi=1234 eax=0001 ecx=0200 edx=1234 | |
movzx edx, dh # edi=1234 eax=0001 ecx=0200 edx=0003 (nice catch, gcc) | |
sal eax, 8 # edi=1234 eax=0010 ecx=0200 edx=0003 | |
sal edx, 24 # edi=1234 eax=0010 ecx=0200 edx=3000 | |
or eax, ecx # edi=1234 eax=0210 ecx=0200 edx=3000 | |
or eax, edx # edi=1234 eax=3210 ecx=0200 edx=3000 | |
mov al, dil # edi=1234 eax=3214 ecx=0200 edx=3000 | |
ret # return = 3214 | |
Optimized | |
uint32_t p(uint32_t pix) { return pix<<24 | pix>>8; } | |
_Z1pj: # edi=1234 eax=FFFF | |
mov eax, edi # edi=1234 eax=1234 | |
ror eax, 8 # edi=1234 eax=4123 | |
ret # return = 4123 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment