home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH25 / IMGPRCS3.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-08-05  |  4.8 KB  |  222 lines

  1. ; IMGPRCS.ASM
  2. ;
  3. ; An image processing program (Second optimization pass).
  4. ;
  5. ; This program blurs an eight-bit grayscale image by averaging a pixel
  6. ; in the image with the eight pixels around it.  The average is computed
  7. ; by (CurCell*8 + other 8 cells)/16, weighting the current cell by 50%.
  8. ;
  9. ; Because of the size of the image (almost 64K), the input and output
  10. ; matrices are in different segments.
  11. ;
  12. ; Version #1: Straight-forward translation from Pascal to Assembly.
  13. ; Version #2: Three major optimizations. (1) used movsd instruction rather
  14. ;          than a loop to copy data from DataOut back to DataIn.
  15. ;          (2) Used repeat..until forms for all loops.  (3) unrolled
  16. ;          the innermost two loops (which is responsible for most of
  17. ;          the performance improvement).
  18. ; Version #3: Used registers for all variables. Set up segment registers
  19. ;          once and for all through the execution of the main loop so
  20. ;          the code didn't have to reload ds each time through.  Computed
  21. ;          index into each row only once (outside the j loop).
  22. ;
  23. ;
  24. ;    Performance comparisons (66 MHz 80486 DX/2 system).
  25. ;
  26. ;    This code-         4 seconds.
  27. ;    1st optimization pass-     6 seconds.
  28. ;    Original ASM code-    36 seconds.
  29. ;    Borland Pascal v7.0-    45 seconds.
  30. ;    Borland C++ v4.02-    29 seconds.
  31. ;    Microsoft C++ v8.00-    21 seconds.
  32.  
  33.         .xlist
  34.         include     stdlib.a
  35.         includelib    stdlib.lib
  36.         .list
  37.         .386
  38.         option        segment:use16
  39.  
  40.  
  41.  
  42. dseg        segment    para public 'data'
  43. InName        byte    "roller1.raw",0
  44. OutName        byte    "roller2.raw",0
  45. dseg        ends
  46.  
  47.  
  48. ; Here is the input data that we operate on.
  49.  
  50. InSeg        segment    para public 'indata'
  51.  
  52. DataIn        byte    251 dup (256 dup (?))
  53.  
  54. InSeg        ends
  55.  
  56.  
  57. ; Here is the output array that holds the result.
  58.  
  59. OutSeg        segment    para public 'outdata'
  60.  
  61. DataOut        byte    251 dup (256 dup (?))
  62.  
  63. OutSeg        ends
  64.  
  65.  
  66.  
  67.  
  68. cseg        segment    para public 'code'
  69.         assume    cs:cseg, ds:dseg
  70.  
  71. Main        proc
  72.         mov    ax, dseg
  73.         mov    ds, ax
  74.         meminit
  75.  
  76.         mov    ax, 3d00h    ;Open input file for reading.
  77.         lea    dx, InName
  78.         int    21h
  79.         jnc    GoodOpen
  80.         print
  81.         byte    "Could not open input file.",cr,lf,0
  82.         jmp    Quit
  83.  
  84. ; Optimization modification- read the data into DataOut rather than
  85. ; DataIn because we'll move it into DataIn at the beginning of the
  86. ; h loop.
  87.  
  88. GoodOpen:    mov    bx, ax        ;File handle.
  89.         mov    dx, OutSeg    ;Where to put the data.
  90.         mov    ds, dx
  91.         lea    dx, DataOut
  92.         mov    cx, 256*251    ;Size of data file to read.
  93.         mov    ah, 3Fh
  94.         int    21h
  95.         cmp    ax, 256*251    ;See if we read the data.
  96.         je    GoodRead
  97.         print
  98.         byte    "Did not read the file properly",cr,lf,0
  99.         jmp    Quit
  100.  
  101. GoodRead:       print
  102.         byte    "Enter number of iterations: ",0
  103.         getsm
  104.         atoi
  105.         free
  106.         mov    bp, ax
  107.         cmp    ax, 0
  108.         jle    Quit
  109.  
  110.         print
  111.         byte    "Computing Result",cr,lf,0
  112.  
  113.  
  114. ; Copy the input data to the output buffer.
  115.  
  116.  
  117. hloop:        mov    ax, InSeg
  118.         mov    es, ax
  119.         mov    ax, OutSeg
  120.         mov    ds, ax
  121.         lea    si, DataOut
  122.         lea    di, DataIn
  123.         mov    cx, (251*256)/4
  124.     rep    movsd
  125.  
  126.         assume    ds:InSeg, es:OutSeg
  127.         mov    ax, InSeg
  128.         mov    ds, ax
  129.         mov    ax, OutSeg
  130.         mov    es, ax
  131.  
  132.  
  133.  
  134.         mov    cl, 249
  135. iloop:        mov    bh, cl            ;i*256
  136.         mov    bl, 1            ;Start at j=1.
  137.         mov    ch, 254            ;# of times through loop.
  138. jloop:
  139.         mov    dx, 0            ;Compute sum here.
  140.         mov    ah, dh
  141.         mov    dl, DataIn[bx-257]    ;DataIn[i-1][j-1]
  142.         mov    al, DataIn[bx-256]    ;DataIn[i-1][j]
  143.         add    dx, ax
  144.         mov    al, DataIn[bx-255]    ;DataIn[i-1][j+1]
  145.         add    dx, ax
  146.         mov    al, DataIn[bx-1]    ;DataIn[i][j-1]
  147.         add    dx, ax
  148.         mov    al, DataIn[bx+1]    ;DataIn[i][j+1]
  149.         add    dx, ax
  150.         mov    al, DataIn[bx+255]    ;DataIn[i+1][j-1]
  151.         add    dx, ax
  152.         mov    al, DataIn[bx+256]    ;DataIn[i+1][j]
  153.         add    dx, ax
  154.         mov    al, DataIn[bx+257]    ;DataIn[i+1][j+1]
  155.         add    dx, ax
  156.  
  157.         mov    al, DataIn[bx]        ;DataIn[i][j]
  158.         shl    ax, 3            ;DataIn[i][j]*8
  159.         add    dx, ax
  160.         shr    dx, 4            ;Divide by 16
  161.         mov    DataOut[bx], dl
  162.  
  163.         inc    bx
  164.         dec    ch
  165.         jne    jloop
  166.  
  167.         dec    cl
  168.         jne    iloop
  169.  
  170.         dec    bp
  171.         jne    hloop
  172.  
  173. Done:        print
  174.         byte    "Writing result",cr,lf,0
  175.  
  176.  
  177. ; Okay, write the data to the output file:
  178.  
  179.         mov    ah, 3ch        ;Create output file.
  180.         mov    cx, 0        ;Normal file attributes.
  181.         mov    dx, dseg
  182.         mov    ds, dx
  183.         lea    dx, OutName
  184.         int    21h
  185.         jnc    GoodCreate
  186.         print
  187.         byte    "Could not create output file.",cr,lf,0
  188.         jmp    Quit
  189.  
  190. GoodCreate:    mov    bx, ax        ;File handle.
  191.         push    bx
  192.         mov    dx, OutSeg    ;Where the data can be found.
  193.         mov    ds, dx
  194.         lea    dx, DataOut
  195.         mov    cx, 256*251    ;Size of data file to write.
  196.         mov    ah, 40h        ;Write operation.
  197.         int    21h
  198.         pop    bx        ;Retrieve handle for close.
  199.         cmp    ax, 256*251    ;See if we wrote the data.
  200.         je    GoodWrite
  201.         print
  202.         byte    "Did not write the file properly",cr,lf,0
  203.         jmp    Quit
  204.  
  205. GoodWrite:    mov    ah, 3eh        ;Close operation.
  206.         int    21h
  207.  
  208.  
  209. Quit:        ExitPgm            ;DOS macro to quit program.
  210. Main        endp
  211.  
  212. cseg        ends
  213.  
  214. sseg        segment    para stack 'stack'
  215. stk        byte    1024 dup ("stack   ")
  216. sseg        ends
  217.  
  218. zzzzzzseg    segment    para public 'zzzzzz'
  219. LastBytes    byte    16 dup (?)
  220. zzzzzzseg    ends
  221.         end    Main
  222.