Created
November 12, 2019 12:27
-
-
Save MichaelStett/168fe32d4d9c19fccce6da15337fdca4 to your computer and use it in GitHub Desktop.
Reverse bits unsigned char
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
unsigned char func(unsigned char input) { | |
unsigned char output = 0; | |
unsigned char mask = 0x80; | |
while (input != 0) { | |
if (input & 0x01) | |
output += mask; | |
input >>= 1; | |
mask >>= 1; | |
} | |
return output; | |
} | |
unsigned char funcASM(unsigned char input) { | |
unsigned char output = 0; | |
__asm | |
{ | |
xor al, al; | |
xor ah, ah; | |
xor bl, bl; | |
mov al, 0; // output | |
mov ah, 0x80; // maska | |
mov bl, input;// input | |
start: | |
test bl, 1; | |
jz jump; | |
or al, ah; | |
jump: | |
shr bl, 1; | |
shr ah, 1; | |
jnz start; | |
mov output, al; // al to output | |
} | |
return output; | |
} | |
int main() | |
{ | |
printf("0xDA: \n \n"); | |
printf("0x%x \n", func(0xDA)); | |
printf("0x%x \n", funcASM(0xDA)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment