home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / PJ8_3.ZIP / 386_TRAP.ASM < prev    next >
Assembly Source File  |  1989-12-21  |  2KB  |  149 lines

  1.     title    386 memory protect
  2.     .model    small
  3.  
  4.     .data
  5. original_int1    label    dword
  6.     dw    trap_interrupt,@code
  7.  
  8. terminate_address label    dword
  9.     dw    terminate_program,@code
  10.  
  11. stack_over_text    db    'Stack overflow at CS:IP==$'
  12.  
  13.  
  14.     .386p
  15.     .code
  16.  
  17.  
  18. ;;    outchr
  19. ;
  20. ;    entry    AL    character to display
  21. ;    uses    AX
  22. ;
  23. outchr    proc
  24.     push    dx
  25.     mov    dl,al
  26.     mov    ah,2
  27.     int    21h
  28.     pop    dx
  29.     ret
  30. outchr    endp
  31.  
  32.  
  33. ;;    outchr hex word
  34. ;
  35. ;    entry    AX    word
  36. ;    uses    AX
  37. ;
  38. outchr_hex_word proc
  39.     push    ax
  40.     mov    al,ah
  41.     call    ohw1
  42.     pop    ax
  43. ohw1:    push    ax
  44.     sar    al,1
  45.     sar    al,1
  46.     sar    al,1
  47.     sar    al,1
  48.     call    ohw2
  49.     pop    ax
  50. ohw2:    and    al,0Fh
  51.     add    al,90h
  52.     daa
  53.     adc    al,40h
  54.     daa
  55.     jmp    outchr
  56. outchr_hex_word endp
  57.  
  58.  
  59. ;;    terminate program
  60. ;
  61. terminate_program proc
  62.     mov    eax,0            ; disable breakpoints
  63.     mov    dr7,eax
  64.  
  65.     mov    ax,@data        ; restore original interrupt 1
  66.     mov    ds,ax
  67.     mov    eax,original_int1
  68.     mov    dx,0
  69.     mov    es,dx
  70.     mov    es:[4],eax
  71.  
  72.     jmp    terminate_address    ; goto original terminate address
  73. terminate_program endp
  74.  
  75.  
  76. ;;    trap interrupt
  77. ;
  78. trap_interrupt proc
  79.     mov    eax,0            ; disable breakpoints
  80.     mov    dr7,eax
  81.  
  82.     mov    ah,9            ; *Stack overflow at CS:IP==*
  83.     mov    dx,@data
  84.     mov    ds,dx
  85.     lea    dx,stack_over_text
  86.     int    21h
  87.  
  88.     pop    dx            ; display CS:IP
  89.     pop    ax
  90.     call    outchr_hex_word
  91.     mov    al,':'
  92.     call    outchr
  93.     mov    ax,dx
  94.     call    outchr_hex_word
  95.  
  96.     mov    ax,4C01h        ; exit program
  97.     int    21h
  98. trap_interrupt endp
  99.  
  100.  
  101. ;;    trap memory access
  102. ;
  103. ;    entry    DX:AX    real address to protect
  104. ;    uses    EAX,BX,EDX,ES
  105. ;
  106. trap_memory_access proc
  107.     push    ds
  108.     movzx    eax,ax            ; convert real-mode dword address
  109.     movzx    edx,dx            ;  to linear address
  110.     shl    edx,4
  111.     add    edx,eax
  112.  
  113.     mov    ah,30h            ; check dos version
  114.     int    21h
  115.     cmp    al,3
  116.     jb    tma3            ;  if before DOS3
  117.  
  118.     mov    ah,62h            ; get PSP (requires DOS3)
  119.     int    21h
  120.     mov    es,bx
  121.  
  122.     mov    ax,@data        ; set terminate address at PSP:0Ah
  123.     mov    ds,ax
  124.     mov    eax,terminate_address
  125.     cmp    ax,offset terminate_program
  126.     jne    tma1            ;  if terminate address set already
  127.     xchg    eax,es:[0Ah]
  128.     mov    terminate_address,eax
  129.  
  130. tma1:    mov    ax,0            ; set trap interrupt vector
  131.     mov    es,ax
  132.     mov    eax,original_int1
  133.     cmp    ax,offset trap_interrupt
  134.     jne    tma2            ;  if set already
  135.     xchg    eax,es:[4]
  136.     mov    original_int1,eax
  137.  
  138. tma2:    mov    dr0,edx            ; enable global breakpoint 0 for any
  139.     mov    eax,070202h        ;  16 bit read or write to linear adr
  140.     mov    dr7,eax
  141.  
  142. tma3:    pop    ds
  143.     ret
  144. trap_memory_access endp
  145.  
  146.     public    trap_memory_access,trap_interrupt,terminate_program
  147.  
  148.     end
  149.