NASM Forum > Example Code

Reversing string at high speed


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: ---flipit:
  cmp CX,0
  je nflip
    push DI
    push SI
    mov DI,SI
    rcr CX,1
    shl CX,1
    jnc flst
      add CX,2
    add DI,CX
      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

--- End code ---

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)?


[0] Message Index

Go to full version