News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Reverse all bits of a DWORD

Started by aw27, November 01, 2019, 01:53:40 AM

Previous topic - Next topic

HSE

Quote from: jj2007 on November 01, 2019, 08:04:30 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)
Equations in Assembly: SmplMath

jj2007

#16
Quote from: HSE on November 01, 2019, 09:16:03 AMyou 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:

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


TimoVJL

May the source be with you

jj2007

Quote from: TimoVJL on November 01, 2019, 06:42:31 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.

aw27

This is the smallest without using the loop instruction  :badgrin:


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

aw27

For people with Haswell because this requires Bit Manipulation Instruction Set 2 (BMI2):


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

Quote from: AW on November 01, 2019, 09:33:01 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.

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
Equations in Assembly: SmplMath

aw27

lzcnt is similar to bsr but bsr brought me a headache with the flags.

TimoVJL

This was modified from C code  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

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 ...?
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

jj2007

Quote from: daydreamer 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 ...?

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