Author Topic: Reversing string at high speed  (Read 16257 times)

Offline mik3ca

  • Jr. Member
  • *
  • Posts: 30
Reversing string at high speed
« on: February 14, 2021, 06:42:13 PM »
Since I'm dealing with data of an opposite endian, I found it beneficial to flip entire strings of input so I can process 16-bit data structures without flipping bits.

This is the code I came up with to flip a string so it comes out in reverse.  Note that I'm working in a real DOS environment so I have to use segments and offsets.

Anyways, This code uses ES:SI as the pointer to the string, and CX as the size.
Size is rounded up to the next even number so flip works properly.

I'm just wondering if there is a solution that can give me better speed.

Code: [Select]
flipit:
  cmp CX,0
  je nflip
    push DI
    push SI
    mov DI,SI
    rcr CX,1
    shl CX,1
    jnc flst
      add CX,2
    flst:
    add DI,CX
    flst2:
      dec DI
      mov AL,[ES:DI]
      mov AH,[ES:SI]
      mov [ES:DI],AH
      mov [ES:SI],AL
      inc SI
    loop flst2
    pop SI
    pop DI
  nflip:
ret

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Reversing string at high speed
« Reply #1 on: February 14, 2021, 09:14:08 PM »
Not so simple...

Even if the pair ES:SI is normalized (SI is lower as possible), what will happen if SI or DI overflows (or underflows)?

Offline Danielle3562

  • New Member
  • Posts: 1
Re: Reversing string at high speed
« Reply #2 on: January 03, 2023, 06:51:31 AM »
We can use the reversed() function to get the reverse of the string in a list and then use the . join() function to concatenate all the characters to our original string. Time Complexity of this approach is O(N) where N is the length of the string to be reversed.

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Reversing string at high speed
« Reply #3 on: January 03, 2023, 08:35:37 AM »
I really fail to see why NOT to use BSWAP and ROL (or ROR) instructions to change thee endianess of DWORDs and WORDs...

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Reversing string at high speed
« Reply #4 on: January 03, 2023, 04:01:09 PM »
Ahhhh, just a reminder: Since Nehalem microarchitecture (first generation of Core i5, released in 2008), processors have the MOVBE instruction (MOV Big Endian):

  movbe reg32,mem32
  movbe mem32, reg32
  movbe reg16,mem16
  movbe mem16,reg16