home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / ddjmag / ddj9103.zip / 80X86.ASC < prev    next >
Text File  |  1991-02-15  |  6KB  |  165 lines

  1. _80x86 OPTIMIZATION_
  2. by Michael Abrash
  3.  
  4.  
  5. [LISTING ONE]
  6.  
  7. ; Copies one string to another string, converting all characters to
  8. ; uppercase in the process, using a loop containing LODSB and STOSB.
  9. ; Adapted from Zen of Assembly Language, by Michael Abrash; not a
  10. ; standalone program, but designed to be used with the Zen timer from
  11. ; that book via the Zen timer's PZTIME.BAT batch file: ZTimerOn starts
  12. ; the clock, ZTimerOff stops it, and the test-bed program linked in by
  13. ; PZTIME.BAT starts the program, reports the results, and ends.
  14.  
  15.     jmp    Skip        ;skip over data in CS and subroutine
  16.  
  17. SourceString    label    word        ;sample string to copy
  18.     db    'This space intentionally left not blank',0
  19. DestString    db    100 dup (?)    ;destination for copy
  20.  
  21. ; Copies one zero-terminated string to another string,
  22. ; converting all characters to uppercase.
  23. ; Input: DS:SI = start of source string; DS:DI = start of destination buffer
  24. ; Output: none
  25. ; Registers altered: AX, BX, SI, DI, ES
  26. ; Direction flag cleared
  27.  
  28. CopyStringUpper:
  29.     mov    ax,ds
  30.     mov    es,ax    ;for STOS
  31.     mov    bl,'a'    ;set up for fast register-register
  32.     mov    bh,'z'    ; comparisons
  33.     cld
  34. StringUpperLoop:
  35.     lodsb        ;get next character and point to following character
  36.     cmp    al,bl    ;below 'a'?
  37.     jb    IsUpper    ;yes, not lowercase
  38.     cmp    al,bh    ;above 'z'?
  39.     ja    IsUpper    ;yes, not lowercase
  40.     and    al,not 20h ;is lowercase-make uppercase
  41. IsUpper:
  42.     stosb        ;put character into new string and point to 
  43.                         ; following location
  44.     and    al,al    ;is this the zero that marks end of the string?
  45.     jnz    StringUpperLoop ;no, do the next character
  46.     ret
  47.  
  48. ; Calls CopyStringUpper to copy & convert SourceString->DestString.
  49. Skip:
  50.     call    ZTimerOn        ;start timing
  51.     mov    si,offset SourceString    ;point SI to the string to copy from
  52.     mov    di,offset DestString    ;point DI to the string to copy to
  53.     call    CopyStringUpper        ;copy & convert to uppercase
  54.     call    ZTimerOff        ;stop timing
  55.  
  56.  
  57. [LISTING TWO]
  58.  
  59. ; Copies one string to another string, converting all characters to
  60. ; uppercase in the process, using no string instructions.
  61. ; Not a standalone program, but designed to be used with the Zen
  62. ; timer, as described in Listing 1.
  63.  
  64.     jmp    Skip        ;skip over data in CS and subroutine
  65.  
  66. SourceString    label    word        ;sample string to copy
  67.     db    'This space intentionally left not blank',0
  68. DestString    db    100 dup (?)    ;destination for copy
  69.  
  70. ; Copies one zero-terminated string to another string,
  71. ; converting all characters to uppercase. 
  72. ; Input: DS:SI = start of source string; DS:DI = start of destination string
  73. ; Output: none
  74. ; Registers altered: AL, BX, SI, DI
  75.  
  76. CopyStringUpper:
  77.     mov    bl,'a'    ;set up for fast register-register
  78.     mov    bh,'z'    ; comparisons
  79. StringUpperLoop:
  80.     mov    al,[si]    ;get the next character and
  81.     inc    si    ; point to the following character
  82.     cmp    al,bl    ;below 'a'?
  83.     jb    IsUpper    ;yes, not lowercase
  84.     cmp    al,bh    ;above 'z'?
  85.     ja    IsUpper    ;yes, not lowercase
  86.     and    al,not 20h ;is lowercase-make uppercase
  87. IsUpper:
  88.     mov    [di],al    ;put the character into the new string and
  89.     inc    di    ; point to the following location
  90.     and    al,al    ;is this the zero that marks the end of the string?
  91.     jnz    StringUpperLoop ;no, do the next character
  92.     ret
  93.  
  94. ; Calls CopyStringUpper to copy & convert SourceString->DestString.
  95. Skip:
  96.     call    ZTimerOn
  97.     mov    si,offset SourceString    ;point SI to the string to copy from
  98.     mov    di,offset DestString    ;point DI to the string to copy to
  99.     call    CopyStringUpper        ;copy & convert to uppercase
  100.     call    ZTimerOff
  101.  
  102.  
  103. [LISTING THREE]
  104.  
  105. ; Clears a buffer using MOV/ADD in a loop.
  106. ; Not a standalone program, but designed to be used with the Zen
  107. ; timer, as described in Listing 1.
  108.  
  109.     mov    dx,2        ;repeat the test code twice, to make
  110.                 ; sure it's in the cache (if there is one)
  111.     mov    bx,dx        ;distance from the start of one word
  112.                 ; to the start of the next
  113.     sub    ax,ax        ;set buffer to zeroes
  114. TestTwiceLoop:
  115.     mov    cx,1024        ;clear 1024 words starting at address
  116.     mov    di,8000h    ; DS:8000h (this is just unused memory
  117.                 ; past the end of the program)
  118.     call    ZTimerOn    ;start timing (resets timer to 0)
  119. StoreLoop:
  120.     mov    [di],ax        ;clear the current word
  121.     add    di,bx        ;point to the next word
  122.     dec    cx        ;count off words to clear until none
  123.     jnz    StoreLoop    ; remain
  124.     call    ZTimerOff    ;stop timing
  125.     dec    dx        ;count off passes through test code
  126.     jz    StoreDone    ;that was the second pass; we're done
  127.     jmp    TestTwiceLoop    ;that was first pass; do second pass with all 
  128.                                 ; instructions and data in the cache
  129. StoreDone:
  130.  
  131.  
  132. [LISTING FOUR]
  133.  
  134. ; Clears a buffer using MOV/ADD in an unrolled loop.
  135. ; Not a standalone program, but designed to be used with the Zen
  136. ; timer, as described in Listing 1.
  137.  
  138.     mov    dx,2        ;repeat the test code twice, to make
  139.                 ; sure it's in the cache (if there is one)
  140.     mov    bx,dx        ;distance from the start of one word
  141.                 ; to the start of the next
  142.     sub    ax,ax        ;set buffer to zeroes
  143. TestTwiceLoop:
  144.     mov    si,1024        ;clear 1024 words starting at address
  145.     mov    di,8000h    ; DS:8000h (this is just unused memory
  146.                 ; past the end of the program)
  147.     call    ZTimerOn    ;start timing (resets timer to 0)
  148.     mov    cl,4        ;divide the count of words to clear by
  149.     shr    si,cl        ; 16, because we'll clear 16 words
  150.                 ; each time through the loop
  151. StoreLoop:
  152.     REPT    16        ;clear 16 words in a row without looping
  153.     mov    [di],ax        ;clear the current word
  154.     add    di,bx        ;point to the next word
  155.     ENDM
  156.     dec    si        ;count off blocks of 16 words to clear
  157.     jnz    StoreLoop    ; until none remain
  158.     call    ZTimerOff    ;stop timing
  159.     dec    dx        ;count off passes through test code
  160.     jz    StoreDone    ;that was the second pass; we're done
  161.     jmp    TestTwiceLoop    ;that was the first pass; do the second pass 
  162.                                 ; with all instructions and data in the cache
  163. StoreDone:
  164.  
  165.