home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH17 / MINMAX.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-07-12  |  2.7 KB  |  123 lines

  1. ; This program demonstrates how to compute the minimum and maximum values
  2. ; for an array of signed integers using the bound instruction
  3.  
  4.         .xlist
  5.                 .286
  6.         include     stdlib.a
  7.         includelib    stdlib.lib
  8.         .list
  9.  
  10. dseg        segment    para public 'data'
  11.  
  12. ; The following two values contain the bounds for the BOUND instruction.
  13.  
  14. LowerBound    word    ?
  15. UpperBound    word    ?
  16.  
  17. ; Save the INT 5 address here:
  18.  
  19. OldInt5        dword    ?
  20.  
  21. ; Here is the array we want to compute the minimum and maximum for:
  22.  
  23. Array        word    1, 2, -5, 345, -26, 23, 200, 35, -100, 20, 45
  24.         word    62, -30, -1, 21, 85, 400, -265, 3, 74, 24, -2
  25.         word    1024, -7, 1000, 100, -1000, 29, 78, -87, 60
  26. ArraySize    =    ($-Array)/2
  27.  
  28. dseg        ends
  29.  
  30.  
  31. cseg        segment    para public 'code'
  32.         assume    cs:cseg, ds:dseg
  33.  
  34.  
  35. ; Our interrupt 5 ISR.  It compares the value in AX with the upper and
  36. ; lower bounds and stores AX in one of them (we know AX is out of range
  37. ; by virtue of the fact that we are in this ISR).
  38. ;
  39. ; Note: in this particular case, we know that DS points at dseg, so this
  40. ; ISR will get cheap and not bother reloading it.
  41. ;
  42. ; Warning: This code does not handle the conflict between bound/int5 and
  43. ; the print screen key.  Pressing prtsc while executing this code may
  44. ; produce incorrect results (see the text).
  45.  
  46. BoundISR    proc    near
  47.         cmp    ax, LowerBound
  48.         jl    NewLower
  49.  
  50. ; Must be an upper bound violation.
  51.  
  52.         mov    UpperBound, ax
  53.         iret
  54.  
  55. NewLower:    mov    LowerBound, ax
  56.         iret
  57. BoundISR    endp
  58.  
  59.  
  60.  
  61. Main        proc
  62.         mov    ax, dseg
  63.         mov    ds, ax
  64.         meminit
  65.  
  66. ; Begin by patching in the address of our ISR into int 5's vector.
  67.  
  68.         mov    ax, 0
  69.         mov    es, ax
  70.         mov    ax, es:[5*4]
  71.         mov    word ptr OldInt5, ax
  72.         mov    ax, es:[5*4 + 2]
  73.         mov    word ptr OldInt5+2, ax
  74.  
  75.         mov    word ptr es:[5*4], offset BoundISR
  76.         mov    es:[5*4 + 2], cs
  77.  
  78.  
  79. ; Okay, process the array elements.  Begin by initializing the upper
  80. ; and lower bounds values with the first element of the array.
  81.  
  82.         mov    ax, Array
  83.         mov    LowerBound, ax
  84.         mov    UpperBound, ax
  85.  
  86. ; Now process each element of the array:
  87.  
  88.         mov    bx, 2            ;Start with second element.
  89.         mov    cx, ArraySize
  90. GetMinMax:    mov    ax, Array[bx]
  91.         bound    ax, LowerBound
  92.         add    bx, 2            ;Move on to next element.
  93.         loop    GetMinMax        ;Repeat for each element.
  94.  
  95.         printf
  96.         byte    "The minimum value is %d\n"
  97.         byte    "The maximum value is %d\n",0
  98.         dword    LowerBound, UpperBound
  99.  
  100. ; Okay, restore the interrupt vector:
  101.  
  102.         mov    ax, 0
  103.         mov    es, ax
  104.         mov    ax, word ptr OldInt5
  105.         mov    es:[5*4], ax
  106.         mov    ax, word ptr OldInt5+2
  107.         mov    es:[5*4+2], ax
  108.  
  109.  
  110. Quit:        ExitPgm            ;DOS macro to quit program.
  111. Main        endp
  112.  
  113. cseg        ends
  114.  
  115. sseg        segment    para stack 'stack'
  116. stk        db    1024 dup ("stack   ")
  117. sseg        ends
  118.  
  119. zzzzzzseg    segment    para public 'zzzzzz'
  120. LastBytes    db    16 dup (?)
  121. zzzzzzseg    ends
  122.         end    Main
  123.