Skip to content

Instantly share code, notes, and snippets.

@MichaelStett
Created November 12, 2019 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelStett/168fe32d4d9c19fccce6da15337fdca4 to your computer and use it in GitHub Desktop.
Save MichaelStett/168fe32d4d9c19fccce6da15337fdca4 to your computer and use it in GitHub Desktop.
Reverse bits unsigned char
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