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

  1. ; IMGPRCS.ASM
  2. ;
  3. ; An image processing program.
  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. ;
  14. ;    Performance comparisons (66 MHz 80486 DX/2 system).
  15. ;
  16. ;    This code-        36 seconds.
  17. ;    Borland Pascal v7.0-    45 seconds.
  18. ;    Borland C++ v4.02-    29 seconds.
  19. ;    Microsoft C++ v8.00-    21 seconds.
  20.  
  21.         .xlist
  22.         include     stdlib.a
  23.         includelib    stdlib.lib
  24.         .list
  25.         .286
  26.  
  27. dseg        segment    para public 'data'
  28.  
  29. ; Loop control variables and other variables:
  30.  
  31. h        word    ?
  32. i        word    ?
  33. j        word    ?
  34. k        word    ?
  35. l        word    ?
  36. sum        word    ?
  37. iterations    word    ?
  38.  
  39. ; File names:
  40.  
  41. InName        byte    "roller1.raw",0
  42. OutName        byte    "roller2.raw",0
  43.  
  44. dseg        ends
  45.  
  46.  
  47. ; Here is the input data that we operate on.
  48.  
  49. InSeg        segment    para public 'indata'
  50.  
  51. DataIn        byte    251 dup (256 dup (?))
  52.  
  53. InSeg        ends
  54.  
  55.  
  56. ; Here is the output array that holds the result.
  57.  
  58. OutSeg        segment    para public 'outdata'
  59.  
  60. DataOut        byte    251 dup (256 dup (?))
  61.  
  62. OutSeg        ends
  63.  
  64.  
  65.  
  66.  
  67. cseg        segment    para public 'code'
  68.         assume    cs:cseg, ds:dseg
  69.  
  70. Main        proc
  71.         mov    ax, dseg
  72.         mov    ds, ax
  73.         meminit
  74.  
  75.         mov    ax, 3d00h    ;Open input file for reading.
  76.         lea    dx, InName
  77.         int    21h
  78.         jnc    GoodOpen
  79.         print
  80.         byte    "Could not open input file.",cr,lf,0
  81.         jmp    Quit
  82.  
  83. GoodOpen:    mov    bx, ax        ;File handle.
  84.         mov    dx, InSeg    ;Where to put the data.
  85.         mov    ds, dx
  86.         lea    dx, DataIn
  87.         mov    cx, 256*251    ;Size of data file to read.
  88.         mov    ah, 3Fh
  89.         int    21h
  90.         cmp    ax, 256*251    ;See if we read the data.
  91.         je    GoodRead
  92.         print
  93.         byte    "Did not read the file properly",cr,lf,0
  94.         jmp    Quit
  95.  
  96. GoodRead:       mov    ax, dseg
  97.         mov    ds, ax
  98.         print
  99.         byte    "Enter number of iterations: ",0
  100.         getsm
  101.         atoi
  102.         free
  103.         mov    iterations, ax
  104.         print
  105.         byte    "Computing Result",cr,lf,0
  106.  
  107. ; Copy the input data to the output buffer.
  108.  
  109.         mov    i, 0
  110. iloop0:        cmp    i, 250
  111.         ja    iDone0
  112.         mov    j, 0
  113. jloop0:        cmp    j, 255
  114.         ja    jDone0
  115.  
  116.         mov    bx, i            ;Compute index into both
  117.         shl    bx, 8            ; arrays using the formula
  118.         add    bx, j            ; i*256+j (row major).
  119.  
  120.         mov    cx, InSeg        ;Point at input segment.
  121.         mov    es, cx
  122.         mov    al, es:DataIn[bx]    ;Get DataIn[i][j].
  123.  
  124.         mov    cx, OutSeg        ;Point at output segment.
  125.         mov    es, cx
  126.         mov    es:DataOut[bx], al    ;Store into DataOut[i][j]
  127.  
  128.         inc    j            ;Next iteration of j loop.
  129.         jmp    jloop0
  130.  
  131. jDone0:        inc    i            ;Next iteration of i loop.
  132.         jmp    iloop0
  133.  
  134. iDone0:
  135.  
  136. ; for h := 1 to iterations-
  137.  
  138.         mov    h, 1
  139. hloop:        mov    ax, h
  140.         cmp    ax, iterations
  141.         ja    hloopDone
  142.  
  143.  
  144.  
  145. ; for i := 1 to 249 -
  146.  
  147.         mov    i, 1
  148. iloop:        cmp    i, 249
  149.         ja    iloopDone
  150.  
  151. ; for j := 1 to 254 -
  152.         mov    j, 1
  153. jloop:        cmp    j, 254
  154.         ja    jloopDone
  155.  
  156.  
  157. ; sum := 0;
  158. ; for k := -1 to 1 do for l := -1 to 1 do
  159.  
  160.         mov    ax, InSeg        ;Gain access to InSeg.
  161.         mov    es, ax
  162.  
  163.         mov    sum, 0
  164.         mov    k, -1
  165. kloop:        cmp    k, 1
  166.         jg    kloopDone
  167.  
  168.         mov    l, -1
  169. lloop:        cmp    l, 1
  170.         jg    lloopDone
  171.  
  172. ; sum := sum + datain [i+k][j+l]
  173.  
  174.         mov    bx, i
  175.         add    bx, k
  176.         shl    bx, 8        ;Multiply by 256.
  177.         add    bx, j
  178.         add    bx, l
  179.  
  180.         mov    al, es:DataIn[bx]
  181.         mov    ah, 0
  182.         add    Sum, ax
  183.  
  184.         inc    l
  185.         jmp    lloop
  186.  
  187. lloopDone:    inc    k
  188.         jmp    kloop
  189.  
  190.  
  191. ; dataout [i][j] := (sum + datain[i][j]*7) div 16;
  192.  
  193. kloopDone:    mov    bx, i
  194.         shl    bx, 8            ;*256
  195.         add    bx, j
  196.         mov    al, es:DataIn[bx]
  197.         mov    ah, 0
  198.         imul    ax, 7
  199.         add    ax, sum
  200.         shr    ax, 4                   ;div 16
  201.  
  202.         mov    bx, OutSeg
  203.         mov    es, bx
  204.  
  205.         mov    bx, i
  206.         shl    bx, 8
  207.         add    bx, j
  208.         mov    es:DataOut[bx], al
  209.  
  210.         inc    j
  211.         jmp    jloop
  212.  
  213. jloopDone:    inc    i
  214.         jmp    iloop
  215.  
  216. iloopDone:
  217. ; Copy the output data to the input buffer.
  218.  
  219.         mov    i, 0
  220. iloop1:        cmp    i, 250
  221.         ja    iDone1
  222.         mov    j, 0
  223. jloop1:        cmp    j, 255
  224.         ja    jDone1
  225.  
  226.         mov    bx, i            ;Compute index into both
  227.         shl    bx, 8            ; arrays using the formula
  228.         add    bx, j            ; i*256+j (row major).
  229.  
  230.         mov    cx, OutSeg        ;Point at input segment.
  231.         mov    es, cx
  232.         mov    al, es:DataOut[bx]    ;Get DataIn[i][j].
  233.  
  234.         mov    cx, InSeg        ;Point at output segment.
  235.         mov    es, cx
  236.         mov    es:DataIn[bx], al    ;Store into DataOut[i][j]
  237.  
  238.         inc    j            ;Next iteration of j loop.
  239.         jmp    jloop1
  240.  
  241. jDone1:        inc    i            ;Next iteration of i loop.
  242.         jmp    iloop1
  243.  
  244. iDone1:        inc    h
  245.         jmp    hloop
  246.  
  247. hloopDone:    print
  248.         byte    "Writing result",cr,lf,0
  249.  
  250.  
  251. ; Okay, write the data to the output file:
  252.  
  253.         mov    ah, 3ch        ;Create output file.
  254.         mov    cx, 0        ;Normal file attributes.
  255.         lea    dx, OutName
  256.         int    21h
  257.         jnc    GoodCreate
  258.         print
  259.         byte    "Could not create output file.",cr,lf,0
  260.         jmp    Quit
  261.  
  262. GoodCreate:    mov    bx, ax        ;File handle.
  263.         push    bx
  264.         mov    dx, OutSeg    ;Where the data can be found.
  265.         mov    ds, dx
  266.         lea    dx, DataOut
  267.         mov    cx, 256*251    ;Size of data file to write.
  268.         mov    ah, 40h        ;Write operation.
  269.         int    21h
  270.         pop    bx        ;Retrieve handle for close.
  271.         cmp    ax, 256*251    ;See if we wrote the data.
  272.         je    GoodWrite
  273.         print
  274.         byte    "Did not write the file properly",cr,lf,0
  275.         jmp    Quit
  276.  
  277. GoodWrite:    mov    ah, 3eh        ;Close operation.
  278.         int    21h
  279.  
  280.  
  281. Quit:        ExitPgm            ;DOS macro to quit program.
  282. Main        endp
  283.  
  284. cseg        ends
  285.  
  286. sseg        segment    para stack 'stack'
  287. stk        byte    1024 dup ("stack   ")
  288. sseg        ends
  289.  
  290. zzzzzzseg    segment    para public 'zzzzzz'
  291. LastBytes    byte    16 dup (?)
  292. zzzzzzseg    ends
  293.         end    Main
  294.