home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / alde_c / misc / util / super_c / histalib.asm < prev    next >
Encoding:
Assembly Source File  |  1980-01-01  |  7.6 KB  |  178 lines

  1. ;               Histogram Assembly Library
  2.  
  3.         HISTSIZE = 4100         ; The size of the histogram table
  4.  
  5. _BSS    segment word public 'BSS' ; Define data segment
  6. _BSS    ends
  7.  
  8. _TEXT   segment byte public 'CODE' ; Define code segment
  9. _TEXT   ends
  10.  
  11. DGROUP  group _BSS              ; Define the DGROUP group
  12.  
  13. _BSS    segment                 ; Place variables in data segment
  14.  
  15. histtab db      HISTSIZE dup(?) ; The histogram table
  16. oldVIP  dw      ?               ; Storage for old timer vector IP
  17. oldVCS  dw      ?               ; Storage for old timer vector CS
  18. intCnt  db      ?               ; Count of times thru interrupt
  19.  
  20. _BSS    ends                    ; End of variable storage
  21.  
  22. _TEXT   segment                 ; Place code in the code segment
  23.  
  24.         assume CS:_TEXT, DS:DGROUP, ES: DGROUP ; Assume CS points to code
  25.                                                ;   segment, and DS and ES
  26.                                                ;   to data segment
  27.  
  28.         extrn   _prHist:near    ; We'll use prHist from the C module
  29.  
  30. ; _startH()
  31. ;
  32. ; Function: Initialize and start IP histogram collection.
  33. ;
  34. ; Algorithm: Zero the histogram table. Save the old timer interrupt
  35. ; vector and replace it with the new one. And reset the timer interrupt
  36. ; rate.
  37.  
  38.         public  _startH         ; Routine is available to other modules
  39.  
  40. _startH proc near               ; NEAR type subroutine
  41.         push    bp              ; Save the BP register
  42.         mov     bp,sp           ; Set BP to SP; easier access to parameters
  43.         push    di              ; Save the DI register
  44.         push    es              ; Save the ES register
  45.         mov     ax,ds           ; Make sure ES = DS
  46.         mov     es,ax
  47.         mov     di,offset DGROUP:histtab ; DI = histtab
  48.         xor     ax,ax           ; AX = 0
  49.         mov     cx,HISTSIZE/2   ; CX = size of histtab in words
  50.         rep stos word ptr [di]  ; Zero histtab
  51.         cli                     ; Disable interrupts
  52.         xor     ax,ax           ; ES = 0
  53.         mov     es,ax
  54.         mov     ax,es:20H       ; Save the old timer vector
  55.         mov     oldVIP,ax
  56.         mov     ax,es:22H
  57.         mov     oldVCS,ax
  58.         mov     ax,OFFSET intServ ; Set the timer vector to intServ
  59.         mov     es:20H,ax
  60.         mov     ax,_TEXT
  61.         mov     es:22H,ax
  62.         mov     al,64           ; Set interrupt count
  63.         mov     intCnt,al
  64.         mov     al,36H          ; Tell timer to prepare for a new count
  65.         out     43H,al
  66.         mov     ax,400H         ; Write count; first low byte
  67.         out     40H,al
  68.         mov     al,ah           ; Then high byte
  69.         out     40H,al
  70.         sti                     ; Enable interrupts
  71.         pop     es              ; Restore ES register
  72.         pop     di              ; Restore DI register
  73.         pop     bp              ; Restore BP register
  74.         ret                     ; Return to caller
  75. _startH endp                    ; End of subroutine
  76.  
  77. ; intServ
  78. ;
  79. ; Function: Service a timer interrupt, updating histogram counters and
  80. ; calling the normal timer interrupt routine if appropriate.
  81. ;
  82. ; Algorithm: Save registers. Increment the appropriate counter. Check
  83. ; if we should call the old timer interrupt vector; if so, do it. Otherwise,
  84. ; just ack the interrupt at the 8259, restore registers, and return.
  85.  
  86. intServ proc far                ; FAR type subroutine (this is used to let
  87.                                 ;   us do a RET to the old vector, even if
  88.                                 ;   it's in a different segment)
  89.         push    ax              ; Push room for a second return address
  90.         push    ax              ;   (this may be used below)
  91.         push    ax              ; Save the AX register
  92.         push    bx              ; Save BX
  93.         push    cx              ; Save CX
  94.         push    bp              ; Save BP
  95.         push    ds              ; Save DS
  96.         mov     bp,sp           ; Set BP to SP, to access interrupts CS:IP
  97.         mov     ax,DGROUP       ; DS = DGROUP, to access variables
  98.         mov     ds,ax
  99.         xor     bx,bx           ; BX = 0; offset to "other" counter
  100.         mov     ax,[bp+16]      ; AX = interrupted CS
  101.         cmp     ax,_text        ; Is it in the code segment?
  102.         jne     intS2           ; If not, then "other" was correct
  103.         mov     bx,[bp+14]      ; BX = IP
  104.         mov     cl,6            ; BX /= 64 (i.e., make BX counter number)
  105.         shr     bx,cl
  106.         shl     bx,1            ; BX = 4*BX + 4 (i.e., make histogram offset)
  107.         shl     bx,1
  108.         add     bx,4
  109. intS2:  inc     word ptr [histtab+bx] ; Increment low word of counter
  110.         jnz     intS3           ; If no overflow from low word, skip
  111.         inc     word ptr [histtab+2+bx] ; Increment high word of counter
  112. intS3:  dec     intCnt          ; Decrement count of interrupts
  113.         jnz     intS4           ; If 64 haven't gone by yet, go exit
  114.         mov     al,64           ; Otherwise, reload count of interrupts
  115.         mov     intCnt,al
  116.         mov     ax,oldVCS       ; Set up stack for RET to old timer vector
  117.         mov     [bp+12],ax
  118.         mov     ax,oldVIP
  119.         mov     [bp+10],ax
  120.         pop     ds              ; Restore DS register
  121.         pop     bp              ; Restore BP
  122.         pop     cx              ; Restore CX
  123.         pop     bx              ; Restore BX
  124.         pop     ax              ; Restore AX
  125.         ret                     ; Return to old vector address
  126.  
  127. intS4:  mov     al,20H          ; Send end-of-interrupt to 8259
  128.         out     20H,al
  129.         pop     ds              ; Restore DS register
  130.         pop     bp              ; Restore BP
  131.         pop     cx              ; Restore CX
  132.         pop     bx              ; Restore BX
  133.         pop     ax              ; Restore AX
  134.         pop     ax              ; Pop space for vector return
  135.         pop     ax
  136.         iret                    ; Return to interrupted code
  137. intServ endp                    ; End of subroutine
  138.  
  139. ; _endH()
  140. ;
  141. ; Function: Turn off histogram collection, and print out the results.
  142. ;
  143. ; Algorithm: Reset the timer interrupt rate to the old one. Restore the
  144. ; timer interrupt vector. And call _prHist to print out the histogram
  145. ; results.
  146.  
  147.         public  _endH           ; Routine is available to other modules
  148.  
  149. _endH   proc near               ; NEAR type subroutine
  150.         push    bp              ; Save the BP register
  151.         mov     bp,sp           ; Set BP to SP; easier to access parameters
  152.         push    es              ; Save the ES register
  153.         xor     ax,ax           ; ES = 0, to access low memory vectors
  154.         mov     es,ax
  155.         cli                     ; Turn off interrupts
  156.         mov     al,36H          ; Tell timer to accept a new count value
  157.         out     43H,al
  158.         xor     al,al           ; Write zero to the timer counter
  159.         out     40H,al          ; First the low byte
  160.         out     40H,al          ; Then the high byte
  161.         mov     ax,oldVIP       ; Restore the old interrupt vector
  162.         mov     es:20H,ax
  163.         mov     ax,oldVCS
  164.         mov     es:22H,ax
  165.         sti                     ; Turn on interrupts
  166.         pop     es              ; Restore the ES register
  167.         mov     ax,offset DGROUP:histtab ; _prHist(histtab): push histtab
  168.         push    ax
  169.         call    _prHist         ; Call _prHist
  170.         pop     ax              ; Pop histtab
  171.         pop     bp              ; Restore BP
  172.         ret                     ; Return to caller
  173. _endH   endp                    ; End of subroutine
  174.  
  175. _TEXT   ends                    ; End of code segment
  176.         end                     ; End of assembly code
  177.  
  178.