home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / msdos / programm / 8854 < prev    next >
Encoding:
Text File  |  1992-08-27  |  2.3 KB  |  83 lines

  1. Newsgroups: comp.os.msdos.programmer
  2. Path: sparky!uunet!sun-barr!ames!pasteur!cory.Berkeley.EDU!librik
  3. From: librik@cory.Berkeley.EDU (David Librik)
  4. Subject: Re: Efficient byte/word mirroring request
  5. Message-ID: <librik.714986664@cory.Berkeley.EDU>
  6. Sender: nntp@pasteur.Berkeley.EDU (NNTP Poster)
  7. Nntp-Posting-Host: cory
  8. Organization: University of California, at Berkeley
  9. References: <1992Aug28.083405.164422@dstos3.dsto.gov.au>
  10. Date: Fri, 28 Aug 1992 07:24:24 GMT
  11. Lines: 70
  12.  
  13. egg@dstos3.dsto.gov.au writes:
  14.  
  15. >Does anyone know how to quickly and efficiently mirror a byte.
  16. >ie
  17. >    11001010 to 01010011
  18.  
  19. >the only way I know is this
  20.  
  21. >   mov  al,byte_to_shift
  22. >   mov  cx,8
  23. >ShiftLoop:
  24. >   rcr  al
  25. >   rcl  ah
  26. >   loop ShiftLoop
  27. >   mov  shifted_byte,ah
  28.  
  29. >There must be an easier (and smarter) way to speed this up using logical
  30. >op's perhaps ?
  31.  
  32. >Themie Gouthas
  33. >egg@dstos3.dsto.gov.au
  34.  
  35. This is an interesting problem.  No, I don't think there is any straightforward
  36. logical set of operations you can do, because none of the logical ops are
  37. too concerned with bit-order.  But we can do a lot better than the code you
  38. have there for speed -- an assembler hint, don't use loops unless you are
  39. held to it at gunpoint.
  40.  
  41. A good solution using your approach would be as follows:
  42.     mov    ah,byte_to_shift
  43.     xor    al,al        ; Clear AL
  44.  
  45.     shr    ah,ah        ; Get low bit of AH into carry
  46.     adc    al,al        ; Put carry into low bit of AL
  47.     shr    ah,ah        ; Get next lowest bit of AH into carry
  48.     adc    al,al        ; Put carry into low bit of AL, shift left
  49.     shr    ah,ah        ;   the rest of AL.  An ADC is equivalent
  50.     adc    al,al        ;   to an RCL, but takes 2 rather than 9 cycles
  51.     shr    ah,ah        ; Do this thing once for each bit.
  52.     adc    al,al        ; This is what macro assemblers were made for.
  53.     shr    ah,ah
  54.     adc    al,al
  55.     shr    ah,ah
  56.     adc    al,al
  57.     shr    ah,ah
  58.     adc    al,al
  59.     shr    ah,ah
  60.     adc    al,al
  61.  
  62.     mov    shifted_byte,al
  63.  
  64. A better solution, if this is an important enough operation to sacrifice 256
  65. bytes of memory in order to massively improve speed is, to make a table:
  66.  
  67. reverse_table    label    byte
  68.         db    00000000b
  69.         db    10000000b
  70.         db    01000000b
  71.         db    11000000b
  72. ... you get the point.  Then the actual reversal code is:
  73.         mov    al,byte_to_shift
  74.         mov    bx,offset reverse_table
  75.         xlatb
  76.         mov    shifted_byte,al
  77.  
  78. Not too shabby.  Anyone with a better solution is more than welcome to
  79. correct my code.
  80.  
  81. - David Librik                                       'Crac Cymraeg!'
  82.   librik@cory.Berkeley.edu
  83.