home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / PROFILER.ZIP / PRF.ASM < prev    next >
Encoding:
Assembly Source File  |  1986-12-31  |  9.7 KB  |  262 lines

  1. PAGE,132
  2. ;from PC Tech Journal, NOV/86, Vol4 No11, p120  "Execution Profiler"
  3. ;
  4. ;User changeable constants to reconfigure profiler
  5. ;
  6. ;   Number of bytes in data table  (F000 = 60K)
  7. Table_Size         EQU  0F000H
  8. ;   Number of ticks between jumps to original clock interrupt handler
  9. ;          = Clock Speedup Factor
  10. NUMBER_OF_TICKS    EQU  1
  11. ;
  12. ;Other constants
  13. ;
  14. ;   Maximum value of countdown for Timer 0 interrupt
  15. MAX_TIMER_COUNT    EQU  0FFFFH
  16. ;
  17. ;   New value of countdown for Timer 0 interrupt
  18. New_Timer_Count    EQU  MAX_TIMER_COUNT / NUMBER_OF_TICKS
  19. ;
  20. ;
  21. PSP      SEGMENT PUBLIC
  22.          ASSUME CS:PSP, DS:PSP, ES:PSP, SS:PSP
  23.          ORG 100H                 ;COM file
  24. ;
  25. ;    Profiler kernel
  26. ;
  27. ;    1. Program executes file 'subject.com' which may be a com or exe file.
  28. ;       Use a batch file to copy subject file to 'subject.com'
  29. ;
  30. ;    2. Run with 'prf fn1 fn2' where 'fn1' and 'fn2' are the parameters
  31. ;       you would have typed if you had been running the subject code by
  32. ;       itself.  This allows copying the formatted file info from the
  33. ;       profiler's PSP into the PSP for the subject code.
  34. ;
  35. PRF      PROC FAR
  36.          MOV  SP, OFFSET END_STACK     ;set top of stack to internal area
  37.          CALL FREE_MEM
  38.          CALL REPLACE_TIMER
  39.          CALL EXEC_SUBJECT
  40.          CALL RESTORE_TIMER
  41.          CALL EXEC_PRINT
  42.          INT  20H
  43. PRF      ENDP
  44. ;     ==================================
  45. ;                     Subroutines
  46. ;
  47. ;
  48. ;   Return unused memory to DOS
  49. ;
  50. FREE_MEM PROC NEAR
  51.          MOV  BX,OFFSET EOM       ; offset top of used memory
  52.          MOV  CL,4
  53.          SHR  BX,CL               ; divide by 16 to get number of paragraphs
  54.          ADD  BX,01               ; to take care of any remainder
  55.          MOV  AH,4AH              ; modify allocated memory
  56.          INT  21H
  57.          JNC  SUCCESS             ; carry flag is set if error
  58.          MOV  AH,09               ; DOS fnctn print string
  59.          MOV  DX,OFFSET MEM_ERR_STRNG
  60.          INT  21H
  61.          INT  20H                 ; terminate after printing error message
  62. SUCCESS: RET
  63. FREE_MEM ENDP
  64. ;      -------------------------------
  65. ;    Run subject program under profiler
  66. ;
  67. EXEC_SUBJECT PROC NEAR
  68.          MOV  AX,CS               ;to set seg registers
  69.          MOV  DS,AX               ;DS:DX points to ASCIIZ string with
  70.          MOV  DX,OFFSET SUBJECT_NAME        ;name of file to execute
  71.          MOV  ES,AX               ;es:bx points to parameter block
  72.          MOV  BX,OFFSET EXEC_PARAMS
  73.          MOV  CX,CS:[2CH]         ;set segment address
  74.          MOV  ENVIRON_SEG,CX      ;of passed environment
  75.          MOV  COMMND_OFS,80H      ;set offset and segment
  76.          MOV  COMMND_SEG,CS       ;of passed command line
  77.          MOV  FCB1_OFS,5CH        ;set offset and segment
  78.          MOV  FCB1_SEG,CS         ;of unopened FCB1
  79.          MOV  FCB2_OFS,6CH        ;set offset and segment
  80.          MOV  FCB2_SEG,CS         ;of unopened FCB@
  81.          MOV  AX,4B00H            ;DOS fnctn Load/Execute
  82.          MOV  ACCUM_FLAG, 1       ;turn on address accumulation flag
  83.          MOV  SAV_SP,SP           ;save stack pointer
  84.          INT  21H
  85.          MOV  AX,CS               ;restore segment registers and stack
  86.          MOV  ES,AX
  87.          MOV  DS,AX
  88.          MOV  SS,AX
  89.          MOV  SP,SAV_SP
  90.          MOV  ACCUM_FLAG, 0       ;and turn off address accumulation flag
  91.          RET
  92. EXEC_SUBJECT ENDP
  93. ;       -----------------------------------
  94. ;
  95. ;   Run program to display profiler output
  96. ;
  97. EXEC_PRINT PROC NEAR
  98.          MOV  AX,CS
  99.          MOV  ES,AX
  100.          MOV  DS,AX               ;DS:DX point to ASCIIZ string with name of
  101.          MOV  SS,AX               ;file to execute
  102.          MOV  DS,AX
  103.          MOV  DX,OFFSET PRINT_NAME
  104. ;          move segment and offset of data into words 0-3 of FCB1, CS:5CH
  105.          MOV  AX,OFFSET DATA_TABLE
  106.          MOV  BX,5CH              ;offset into code seg of FCB1
  107.          MOV  CS:[BX],AX          ;move offset into into FCB1
  108.          MOV  AX,CS
  109.          MOV  CS:[BX+2],AX        ;move CS into FCB1
  110. ;           divide table offset by four to get number of samples
  111.          MOV  CL,2
  112.          SHR  TABLE_OFS,CL        ;shift to divide
  113.          MOV  ES,AX               ;ES:BX points to parameter block
  114.          MOV  BX,OFFSET EXEC_PARAMS
  115.          MOV  CX,CS:[2CH]         ;turn off address
  116.          MOV  ENVIRON_SEG,CS      ;accumulation flag.
  117.          MOV  COMMND_OFS,80H      ;set offset and segment
  118.          MOV  COMMND_SEG,CS       ;of passed command line.
  119.          MOV  FCB1_OFS,5CH        ;set offset and segment
  120.          MOV  FCB1_SEG,CS         ;to unopened FCB1
  121.          MOV  FCB2_OFS,6CH        ;and to FCB2
  122.          MOV  FCB2_SEG,CS
  123.          MOV  SAV_SP,SP           ;save stack pointer
  124.          MOV  AX,4B00H
  125.          INT  21H                 ;execute named program
  126.          MOV  AX,CS
  127.          MOV  DS,AX               ;restore segment registers
  128.          MOV  ES,AX
  129.          MOV  SS,AX
  130.          MOV  SP,SAV_SP           ;and stack pointer
  131.          RET
  132. EXEC_PRINT ENDP
  133. ;      --------------------------------------
  134. ;
  135. ;   Restore original INT 8 and timer 0 countdown to power-up value
  136. ;
  137. RESTORE_TIMER  PROC NEAR
  138.          CLI
  139. ;    restore INT 8
  140.          MOV  AX,0
  141.          MOV  DS,AX
  142.          MOV  AX,CS:OLD_OFS
  143.          MOV  DS:[20H],AX        ;puts old offset back into int table
  144.          MOV  AX,CS:OLD_SEG
  145.          MOV  DS:[22H],AX        ;puts old segment back
  146. ;    restore timer 0 countdown to power-up value
  147.          MOV  AL,36H             ;timer 0, mode 3 (send lsb, msb)
  148.          OUT  43H,AL             ;write current mode control word to timer
  149.          MOV  AL,0               ;lsb and msb of 0
  150.          OUT  40H,AL             ;write lsb to timer 0
  151.          OUT  40H,AL             ;write msb to timer 0
  152.          STI
  153.          RET
  154. RESTORE_TIMER ENDP
  155. ;     ---------------------------------------
  156. ;
  157. ;   Replaces old timer interrupt.  accumulates data if ACCUM_FLAG is set &
  158. ;     jumps to old INT 8 if timing is appropriate.
  159. NEW_TIMER  PROC NEAR
  160.          CMP  CS:ACCUM_FLAG,0
  161.          JE   DO_OLD_INT8?        ;skip accumulate if 0
  162.          CALL ACCUMULATE
  163. DO_OLD_INT8?:
  164.          INC  CS:TIMER_COUNT      ;increment counter
  165.          CMP  CS:TIMER_COUNT, NUMBER_OF_TICKS ;do it this tick?
  166.          JNE  SKIP_OLD_INT8       ;skip INT 8 if not time
  167.          MOV  CS:TIMER_COUNT,0    ;reset counter to 0
  168.          JMP  CS:DWORD PTR OLD_INT8
  169. SKIP_OLD_INT8:
  170.          PUSH AX
  171.          MOV  AL,20H              ;end of interrupt code
  172.          OUT  20H,AL              ;send eoi to 8259
  173.          POP  AX
  174.          IRET
  175. NEW_TIMER  ENDP
  176. ;
  177. ;       accumulate data at clock interrupt
  178. ACCUMULATE PROC NEAR
  179.          PUSH AX
  180.          PUSH BX
  181.          PUSH CX
  182.          PUSH BP
  183. ;      check for table overflow
  184.          MOV  AX,CS:MAX_OFS
  185.          CMP  CS:TABLE_OFS, AX    ;have we reached end of table?
  186.          JAE  END_ACCUM           ;if so, skip data accumulation
  187. ;      accumulate samples
  188.          MOV  BX,CS:TABLE_OFS
  189.          MOV  BP,SP
  190.          MOV  AX,SS:[BP+10]       ;load AX with interrupted IP
  191.          MOV  CS:WORD PTR DATA_TABLE[BX],AX ;save IP
  192.          MOV  AX,SS:[BP+12]       ;load AX with interrupted CS
  193.          MOV  CS:WORD PTR DATA_TABLE[BX+2],AX  ;save CS
  194.          ADD  CS:TABLE_OFS, 4     ;increment offset into table
  195. END_ACCUM:
  196.          POP BP
  197.          POP CX
  198.          POP BX
  199.          POP AX
  200.          RET
  201. ACCUMULATE ENDP
  202. ;   ----------------------------------
  203. ;
  204. ;   Replace existing timer 0 countdown and existing INT 8
  205. ;
  206. REPLACE_TIMER   PROC NEAR
  207.          PUSH DS
  208.          CLI                      ;disable maskable interrupts
  209. ;         change timer 0 countdown to (maybe) speed up timer
  210.          MOV  AL,36H              ;timer 0, mode 3, will send lsb, msb
  211.          OUT  43H,AL              ;write mode control word to timer
  212.          MOV  BX,NEW_TIMER_COUNT
  213.          MOV  AL,BL               ;lsb of NEW_TIMER_COUNT
  214.          OUT  40H,AL              ;write lsb to timer 0
  215.          MOV  AL,BH               ;msb of NEW_TIMER_COUNT
  216.          OUT  40H,AL              ;write msb to timer 0
  217. ;          replace existing INT 8
  218.          XOR  AX,AX
  219.          MOV  DS,AX               ;seg for interrupt vectors (0)
  220.          MOV  AX,DS:[20H]         ;offset of INT 8 vector to AX
  221.          MOV  CS:OLD_OFS,AX       ;and save it
  222.          MOV  AX,DS:[22H]         ;segment of INT 8 vector to AX
  223.          MOV  CS:OLD_SEG,AX       ;and save it
  224.          MOV  DS:[20H],OFFSET NEW_TIMER  ;implant new offset
  225.          MOV  DS:[22H],CS         ;implant new segment
  226.          STI                      ;enable maskable interrupts
  227.          POP DS
  228.          RET
  229. REPLACE_TIMER ENDP
  230. ;  -------------------------------------
  231. ;               Data area
  232. ;
  233. SUBJECT_NAME  DB  'SUBJECT.COM'   ;ASCIIZ name of subject
  234.               DB  0               ;ASCIIZ terminator
  235. MEM_ERR_STRNG DB  'Error shrinking memory - Profile ends$'
  236. EXEC_PARAMS   EQU $               ;parameters passed
  237. ENVIRON_SEG   DW  ?               ;to executed programs
  238. COMMND_OFS    DW  ?
  239. COMMND_SEG    DW  ?
  240. FCB1_OFS      DW  ?
  241. FCB1_SEG      DW  ?
  242. FCB2_OFS      DW  ?
  243. FCB2_SEG      DW  ?
  244. SAV_SP        DW  ?
  245. PRINT_NAME    DB  'LISTPRF.COM'
  246.               DB  0               ;ASCIIZ terminator
  247. TIMER_COUNT   DW  0
  248. ACCUM_FLAG    DW  0
  249. OLD_INT8      EQU $               ;address old timer interrupt
  250. OLD_OFS       DW  ?
  251. OLD_SEG       DW  ?
  252. OUR_STACK     DD  100H DUP(0)     ;space for our stack
  253. END_STACK     EQU $               ;set SP to this offset
  254. MAX_OFS       DW  Table_Size      ;max offset from beginning of data table
  255. TABLE_OFS     DW  ?               ;current offset into data table
  256. DATA_TABLE    DB  Table_Size DUP(0)
  257. EOM           EQU $
  258. PSP           ENDS
  259.               END  PRF
  260.  
  261.  
  262.