home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.os.msdos.programmer
- Path: sparky!uunet!sun-barr!ames!pasteur!cory.Berkeley.EDU!librik
- From: librik@cory.Berkeley.EDU (David Librik)
- Subject: Re: Efficient byte/word mirroring request
- Message-ID: <librik.714986664@cory.Berkeley.EDU>
- Sender: nntp@pasteur.Berkeley.EDU (NNTP Poster)
- Nntp-Posting-Host: cory
- Organization: University of California, at Berkeley
- References: <1992Aug28.083405.164422@dstos3.dsto.gov.au>
- Date: Fri, 28 Aug 1992 07:24:24 GMT
- Lines: 70
-
- egg@dstos3.dsto.gov.au writes:
-
- >Does anyone know how to quickly and efficiently mirror a byte.
- >ie
- > 11001010 to 01010011
-
- >the only way I know is this
-
- > mov al,byte_to_shift
- > mov cx,8
- >ShiftLoop:
- > rcr al
- > rcl ah
- > loop ShiftLoop
- > mov shifted_byte,ah
-
- >There must be an easier (and smarter) way to speed this up using logical
- >op's perhaps ?
-
- >Themie Gouthas
- >egg@dstos3.dsto.gov.au
-
- This is an interesting problem. No, I don't think there is any straightforward
- logical set of operations you can do, because none of the logical ops are
- too concerned with bit-order. But we can do a lot better than the code you
- have there for speed -- an assembler hint, don't use loops unless you are
- held to it at gunpoint.
-
- A good solution using your approach would be as follows:
- mov ah,byte_to_shift
- xor al,al ; Clear AL
-
- shr ah,ah ; Get low bit of AH into carry
- adc al,al ; Put carry into low bit of AL
- shr ah,ah ; Get next lowest bit of AH into carry
- adc al,al ; Put carry into low bit of AL, shift left
- shr ah,ah ; the rest of AL. An ADC is equivalent
- adc al,al ; to an RCL, but takes 2 rather than 9 cycles
- shr ah,ah ; Do this thing once for each bit.
- adc al,al ; This is what macro assemblers were made for.
- shr ah,ah
- adc al,al
- shr ah,ah
- adc al,al
- shr ah,ah
- adc al,al
- shr ah,ah
- adc al,al
-
- mov shifted_byte,al
-
- A better solution, if this is an important enough operation to sacrifice 256
- bytes of memory in order to massively improve speed is, to make a table:
-
- reverse_table label byte
- db 00000000b
- db 10000000b
- db 01000000b
- db 11000000b
- ... you get the point. Then the actual reversal code is:
- mov al,byte_to_shift
- mov bx,offset reverse_table
- xlatb
- mov shifted_byte,al
-
- Not too shabby. Anyone with a better solution is more than welcome to
- correct my code.
-
- - David Librik 'Crac Cymraeg!'
- librik@cory.Berkeley.edu
-