Author Topic: Reverse all bits of a DWORD  (Read 565 times)

HSE

  • Member
  • *****
  • Posts: 1148
  • <AMD>< 7-32>
Re: Reverse all bits of a DWORD
« Reply #15 on: November 01, 2019, 09:16:03 AM »
@HSE: Why 11 "active lines"? It's 8 instructions.

Hi JJ!
 
See last Nidud post, you forget some lines. You can see your results in previous post

Active lines are lines with instructions, lines with only labels or comments (or nothing) don't count. Some assemblers require only labels lines, others no. (I prefer to use only labes lines always)

jj2007

  • Member
  • *****
  • Posts: 9794
  • Assembler is fun ;-)
    • MasmBasic
Re: Reverse all bits of a DWORD
« Reply #16 on: November 01, 2019, 02:20:02 PM »
you forget some lines

New version. The input in eax remains unchanged, while the result is returned in edx. Actually, 7 lines are enough, and no labels needed - see attachment :tongue:

Code: [Select]
input before    b:eax           11111111111111111111111111111111
reversed once   b:edx           11111111111111111111111111111111
reversed twice  b:edx           11111111111111111111111111111111

input before    b:eax           11110000111100001111000011110000
reversed once   b:edx           00001111000011110000111100001111
reversed twice  b:edx           11110000111100001111000011110000

input before    b:eax           11101110111011101110111011101110
reversed once   b:edx           01110111011101110111011101110111
reversed twice  b:edx           11101110111011101110111011101110

input before    b:eax           11001100110011001100110011001100
reversed once   b:edx           00110011001100110011001100110011
reversed twice  b:edx           11001100110011001100110011001100

input before    b:eax           10101010101010101010101010101010
reversed once   b:edx           01010101010101010101010101010101
reversed twice  b:edx           10101010101010101010101010101010

input before    b:eax           00000000000000000000000000000000
reversed once   b:edx           00000000000000000000000000000000
reversed twice  b:edx           00000000000000000000000000000000
« Last Edit: November 01, 2019, 04:09:23 PM by jj2007 »

TimoVJL

  • Member
  • ***
  • Posts: 476
Re: Reverse all bits of a DWORD
« Reply #17 on: November 01, 2019, 06:42:31 PM »
without m2m MACRO it could be 7.
May the source be with you

jj2007

  • Member
  • *****
  • Posts: 9794
  • Assembler is fun ;-)
    • MasmBasic
Re: Reverse all bits of a DWORD
« Reply #18 on: November 01, 2019, 07:28:09 PM »
without m2m MACRO it could be 7.

Right, mov ecx, 32 is ok, too. If it's not speed-critical, I always use m2m for the range -128...127.

AW

  • Member
  • *****
  • Posts: 2435
  • Let's Make ASM Great Again!
Re: Reverse all bits of a DWORD
« Reply #19 on: November 01, 2019, 09:33:01 PM »
This is the smallest without using the loop instruction  :badgrin:

Code: [Select]
mov eax, 0
mov esi, 00100110011110001001100110010101b
reverse:
lzcnt edx, esi
bts eax, edx
not dl
and dl, 1fh
btc esi, edx
jnz reverse

AW

  • Member
  • *****
  • Posts: 2435
  • Let's Make ASM Great Again!
Re: Reverse all bits of a DWORD
« Reply #20 on: November 01, 2019, 11:31:59 PM »
For people with Haswell because this requires Bit Manipulation Instruction Set 2 (BMI2):

Code: [Select]
mov eax, 0
mov esi, 00100110011110001001100110010101b
reverse:
lzcnt ecx, esi
bsr edx, esi
bts eax, ecx
bzhi esi, esi, edx ; requires BMI2
jnz reverse


HSE

  • Member
  • *****
  • Posts: 1148
  • <AMD>< 7-32>
Re: Reverse all bits of a DWORD
« Reply #21 on: November 01, 2019, 11:45:21 PM »
This is the smallest without using the loop instruction  :badgrin:
Apparently lzcnt is a lot simpler CISC algorithm than loop (but this machine only have SS3  :biggrin:) . I think that replacing loop with dec ecx / jnl @B is simpler and universal.

Code: [Select]
edx = 00100110011110001001100110010101y, challenge [awch1.asm, 49]
eax = 10101001100110010001111001100100y, Nidud (4 active lines, value in edx) [awch1.asm, 56]
eax = 10101001100110010001111001100100y, AW (8 active lines, value in esi) [awch1.asm, 70]
edx = 10101001100110010001111001100101y, JJ Oops (if wrong no matter active lines, value in eax) [awch1.asm, 83]
edx = 10101001100110010001111001100100y, JJ-Nidud (11 active lines, value in eax) [awch1.asm, 101]
edx = 10101001100110010001111001100100y, JJ Another (7 active lines, value in eax) [awch1.asm, 114]


note: asmc /Zne /c /coff awch1.asm

AW

  • Member
  • *****
  • Posts: 2435
  • Let's Make ASM Great Again!
Re: Reverse all bits of a DWORD
« Reply #22 on: November 01, 2019, 11:56:16 PM »
lzcnt is similar to bsr but bsr brought me a headache with the flags.

TimoVJL

  • Member
  • ***
  • Posts: 476
Re: Reverse all bits of a DWORD
« Reply #23 on: November 02, 2019, 12:04:03 AM »
This was modified from C code
Code: [Select]
  mov esi, 26789995h
  xor edi, edi

  xor eax, eax
  mov edx, 1Fh
L1:
  bt esi, eax
  ;setb cl
  ;test cl, cl
  jnb @F ; jz @F
  bts edi, edx
@@:
  ;setb cl
  ;add eax, 1h
  inc eax
  ;sub edx, 1h
  dec edx
  jnz L1
May the source be with you

daydreamer

  • Member
  • ****
  • Posts: 942
  • watch Chebyshev on the backside of the Moon
Re: Reverse all bits of a DWORD
« Reply #24 on: November 02, 2019, 12:30:37 AM »
why shouldnt a single 1/x with fdiv with double work on reversing bits,as 2 reversed=0.5,4 reversed =0.25,8 reversed=0.125 ...?
Quote from Flashdance
Nick  :  When you give up your dream, you die
*wears a flameproof asbestos suit*
Gone serverside programming p:  :D

jj2007

  • Member
  • *****
  • Posts: 9794
  • Assembler is fun ;-)
    • MasmBasic
Re: Reverse all bits of a DWORD
« Reply #25 on: November 02, 2019, 01:55:41 AM »
why shouldnt a single 1/x with fdiv with double work on reversing bits,as 2 reversed=0.5,4 reversed =0.25,8 reversed=0.125 ...?

Why don't you test it and post some code?