flat assembler
Message board for the users of flat assembler.

Index > DOS > Reverse the order of bytes of an array

Author
Thread Post new topic Reply to topic
YONG



Joined: 16 Mar 2005
Posts: 8000
Location: 22° 15' N | 114° 10' E
YONG
For example, 'ABCD...WXYZ' => 'ZYXW...DCBA'

I am currently using this approach:
Code:
                mov     si, (bufSize/2)-1
                mov     di, stdBuf
        cB_RBO_LP:
                mov     al, [stdBuf+bufSize/2+si]
                mov     ah, [di]

                mov     [di], al
                mov     [stdBuf+bufSize/2+si], ah

                inc     di
                dec     si
                jns     cB_RBO_LP

bufSize         =       256             ; 64, 128, 256, 512, ...
stdBuf          rb      bufSize
    

Could anyone think of a better / further optimized approach?

Thanks,
YONG
Post 02 Jun 2005, 12:41
View user's profile Send private message Visit poster's website Reply with quote
donkey7



Joined: 31 Jan 2005
Posts: 127
Location: Poland, Malopolska
donkey7
what about:
Code:
                mov     si, bufSize/2-4
                mov     di, stdBuf+bufSize/2
        cB_RBO_LP:
                mov     eax, [stdBuf+si]
                mov     ebx, [di]

                bswap   eax
                bswap   ebx

                mov     [di], eax
                mov     [stdBuf+si], ebx

                add     di,4
                sub     si,4
                jns      cB_RBO_LP

bufSize         =       256             ; 8, 16, 24, 32, 40, 48, 56, 64, 72 ...
stdBuf          rb      bufSize 
    

note1: this code requires 486+ (bswap)
note2: this code works only if the array size in bytes is multiple of eight

_________________
Keep coding!
Post 02 Jun 2005, 19:15
View user's profile Send private message Visit poster's website Reply with quote
YONG



Joined: 16 Mar 2005
Posts: 8000
Location: 22° 15' N | 114° 10' E
YONG
A small bugfix for the suggested code:
Code:
                ...
                mov     eax, dword [stdBuf+si]  ; requires dword
                ...
                mov     dword [stdBuf+si], ebx  ; ditto
                ...
    

Quote:

note1: this code requires 486+ (bswap)
note2: this code works only if the array size in bytes is multiple of eight

To me, these requirements are no problem.

The suggested code is slightly faster than my approach.

Thank donkey7 for the input.

YONG
Post 04 Jun 2005, 08:27
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.