home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / ASM / GRDBDL17.ZIP / MTRAP.ASM < prev    next >
Encoding:
Assembly Source File  |  1998-10-26  |  7.1 KB  |  339 lines

  1. ;
  2. ; GRDB
  3. ;
  4. ; Copyright(c) LADsoft
  5. ;
  6. ; David Lindauer, camille@bluegrass.net
  7. ;
  8. ;
  9. ; mtrap.asm
  10. ;
  11. ; Function: handle the all traps.  Int 3 and Int 1 are debug traps and
  12. ; are handled specially
  13.     ;MASM MODE
  14.     .model small
  15.     .386
  16.  
  17.     .stack 1024
  18.  
  19. include iversion.inc
  20. include  eprints.inc 
  21. include  eregs.inc 
  22. include  einput.inc 
  23. include  ebreaks.inc 
  24. include     eenv.inc
  25. include  eloader.inc
  26. include  elogging.inc
  27. include  edos.inc
  28. include  eMemory.inc
  29. include  eints.inc
  30. include  eexec.inc
  31. include eoptions.inc
  32. include eloader.inc
  33. include ehistory.inc
  34. include eswift.inc
  35. include efp.inc
  36. include ehbreak.inc
  37.  
  38.     public  entry3,indos,entry1
  39.     PUBLIC    RegdumpFLAGS,RegdumpEAX,RegdumpEBX,RegdumpECX,RegdumpEDX
  40.     PUBLIC    RegdumpESI,RegdumpEDI,RegdumpEBP
  41.     PUBLIC    RegdumpESP,RegdumpEIP
  42.     PUBLIC    RegdumpCS,RegdumpES,RegdumpDS,RegdumpSS,RegdumpFS,RegdumpGS
  43.     PUBLIC    TrapHandler, _exit, psp
  44.     PUBLIC  reentry, grdbname, veclist, entry1
  45.     PUBLIC    stackseg,stackofs, dosver
  46.  
  47.     extrn checkcpu : PROC, FileLoadErr : PROC, trapcount : dword
  48.         extrn ReTrap : PROC
  49.     EXTRN    initflatreal:PROC, rundownflatreal : PROC
  50. ;
  51. ; CPU instruction trap enable flag
  52. ;
  53. TRAPFLAG = 100h
  54.  
  55.     .data
  56. stackofs    dw    0    ;to restore out stack in INT1
  57. stackseg    dw    0
  58. ;
  59. ; Register image
  60. ;
  61. veclist    db    00h
  62.     dw    entry0
  63.     db    1
  64.     dw    entry1
  65.     db    3
  66.     dw    entry3
  67.     db    1bh
  68.     dw    int1bh
  69.     db    20h
  70.     dw    int20handle
  71.     db    21h
  72.     dw    int21handle
  73.     db    0ffh    ; trailer
  74. RegdumpFLAGS    dd    0
  75. RegdumpEAX    dd    0
  76. RegdumpEBX    dd    0
  77. RegdumpECX    dd    0
  78. RegdumpEDX    dd    0
  79. RegdumpESI    dd    0
  80. RegdumpEDI    dd    0
  81. RegdumpEBP    dd    0
  82. RegdumpESP    dd    0
  83. RegdumpEIP    dd    0
  84. RegdumpCS    dw    0
  85. RegdumpES    dw    0
  86. RegdumpDS    dw    0
  87. RegdumpSS    dw    0
  88. RegdumpFS    dw    0
  89. RegdumpGS    dw    0
  90. ;
  91. dosver    dw    0
  92. haserr    dw    0    ; If there is an error# on stack
  93. errnum    dw    0    ; The error#
  94. trapnum    dw    0    ; The trap#
  95. indos    dd    0    ; pointer to indos flag
  96. grdbname    db    '********',0
  97. psp    dw    0    ;debugger PSP
  98. neederr    db    0    ;flag for file load result from command line
  99.  
  100. trappop    dd    6790h    ;traps that need to pop a value
  101.  
  102.     .code
  103. cpuerr    db    "GRDB requires a 386 or better processor",0
  104. memerr    db    "not enough memory/corrupt arena",0
  105. vererr    db    "GRDB requires MSDOS 4.0 or better"
  106. nomsg    db    0
  107.  
  108. ;This macro generates a front end for the interrupt handlers installed for
  109. ;int 0, 1 and 3.
  110.  
  111. entry    macro    num
  112.     local    est
  113. entry&num:
  114. if num EQ 1
  115.     call    hdwechk
  116.     jc    est
  117.     call    swiftrace
  118. ENDIF
  119. IF num EQ 3
  120.     call    swiftrace
  121. ENDIF
  122. est:
  123.     push    gs
  124.     push    fs
  125.     push    es
  126.     push    ds
  127.     pushad
  128.     mov    al,num
  129.     jmp    traphandler
  130. endm
  131.  
  132.     entry    0
  133.     entry    1
  134.     entry    3
  135.     
  136. ;
  137. ; Save an image of the regs
  138. ; This MUST BE the first thing the trap handler calls; it assumes
  139. ; there is ONE PUSH (return address) followed by the DS at the time
  140. ; of interrupt followed by the interrupt data
  141. ;
  142. saveregs    PROC
  143.     pop    cx    
  144.     pop    [RegdumpEDI]
  145.     pop    [RegdumpESI]
  146.     pop    [RegdumpEBP]
  147.     pop    eax
  148.     pop    [RegdumpEBX]
  149.     pop    [RegdumpEDX]
  150.     pop    [RegdumpECX]
  151.     pop    [RegdumpEAX]
  152.     pop    [RegdumpDS]
  153.     pop    [RegdumpES]
  154.     pop    [RegdumpFS]
  155.     pop    [RegdumpGS]
  156.     bt    [haserr],0    ; See if an error
  157.     jnc    short noerr    ;
  158.     pop    eax        ; Get the error #
  159.     mov    [errnum],ax    ;
  160. noerr:
  161.     pop    word ptr [RegdumpEIP]
  162.     pop    [RegdumpCS]
  163.     pop    word ptr [RegdumpFLAGS]
  164.     mov    ax,ss        ; Otherwise just save the current
  165.     mov    [RegdumpSS],ax    ; stack pointer before we started pushing
  166.     mov    [RegdumpESP],esp    ;
  167.     jmp    cx
  168. saveregs    ENDP    
  169. ;
  170. ; Adjust EIP to the trap if it's not int 3
  171. ;
  172. adjusteip    PROC    
  173.     cmp    [trapnum],3    ; See if int 3
  174.     jnz    short noadj    ; No, get out
  175.     mov    ebx,[RegdumpEIP]    ;
  176.     mov    fs,[RegdumpCS]    ;
  177.     cmp    BYTE PTR fs:[bx-1],0cch ; See if is an INT 3
  178.     jz    short nodecrement ; Get out if so
  179.     dec    word ptr [RegdumpEIP]        ; Else point at trap
  180. nodecrement:
  181. noadj:
  182.     ret
  183. adjusteip    ENDP    
  184. ;
  185. ; Generic trap handler
  186. ;
  187. traphandler    PROC
  188.     cld
  189.     sti
  190.     mov    bx,dgroup
  191.     mov    ds,bx
  192.     mov    es,bx
  193.     mov    [CtrlBrkPressedInDOS],0
  194.     movzx    eax,al
  195.     mov    [trapnum],ax
  196.     bt    [trappop],eax
  197.     jnc    noerr2
  198.     inc    [haserr]    ; Set the error flag
  199. noerr2:
  200.     mov    si,offset veclist
  201.     call    ReleaseRMInts
  202.     call    saveregs    ; Save Regs
  203.     and    [RegdumpFLAGS],NOT TRAPFLAG ; reset trap flag in flags image
  204.     mov    ss,[stackseg]    ; load our stack
  205.     movzx    esp,[stackofs]    ; clear top of ESP just in case...
  206.     call    disableBreaks    ; Disable breakpoints if not
  207.     call    adjusteip    ; Adjust the EIP to point to the breakpoint
  208.     call    SetDebugPSP
  209.     call    crlf
  210.     cmp     [trapnum],3    ; No stats if it is int 3
  211.     jz    short reentry    ;
  212.     cmp    [trapnum],1    ; Or int 1
  213.     jz    short reentry    ;
  214.     PRINT_MESSAGE    <'Trap: '>
  215.     mov    ax,[trapnum]    ; Say which one
  216.     call    printbyte    ;
  217.     call    crlf
  218.     btr    [haserr],0    ; If has error 
  219.     jnc    reentry        ;
  220.     PRINT_MESSAGE    <'Error: '>
  221.     mov    ax,[errnum]    ; Say which one
  222.     call    printword
  223.     call    crlf
  224. ; fall through
  225. traphandler ENDP
  226. reentry    PROC
  227.     call    DisplayRegisters; Display registers
  228.     test    [trapcount],-1
  229.     jz    noretrace
  230.     dec    [trapcount]
  231.     jmp    ReTrap
  232. noretrace:
  233.     call    initflatreal
  234.     test    [neederr],-1
  235.     jz    InputHandler
  236.     mov    ah,[neederr]
  237.     mov    [neederr],0
  238.     sahf
  239.     call    FileLoadErr
  240.     jmp    InputHandler    ; Go do input
  241.  
  242. reentry    ENDP    
  243. ;
  244. ; Monitor init routine, point all traps to point to the monitor handler
  245. ;
  246.  
  247. start:
  248.     mov    bx,dgroup    ; setup
  249.     mov    ds,bx
  250.     mov    [psp],es    ; our PSP
  251.     mov    es,bx        ;set ES to DX
  252.     mov    ax,ss        ; have to adjust stack to be DS-relative
  253.     sub    ax,bx        ; the disassembler requires it
  254.     shl    ax,4        ;to segment
  255.     add    ax,sp        ;add in the stack pointer
  256.     mov    ss,bx        ;set SS to DS
  257.     mov    sp,ax        ;and new stack based on DS
  258.     mov    [stackseg],ss
  259.     mov    [stackofs],sp
  260.     PRINT_MESSAGE    <"GRDB version ",30h +VERID/10,'.',30h + VERID MOD 10, " Copyright (c) LADsoft",13,10>
  261.     mov    ah,30h                ; dos version check
  262.     int    21h
  263.     cmp    al,4
  264.     mov    dx,offset cs:vererr
  265.     jc    errx
  266.     xchg    ah,al
  267.     mov    [dosver],ax
  268.     push    es                    ; get indos flag
  269.     mov    ah,34h    
  270.     int    21h
  271.     mov    word ptr [indos],bx
  272.     mov    word ptr [indos+2],es
  273.     pop    es
  274.     call    checkcpu            ; validate CPU
  275.     mov    dx,offset cs:cpuerr
  276.     jc    errx
  277. IFNDEF TEST1
  278.     call    ResizeMem            ; cut mem back
  279.     mov    dx,offset cs:memerr
  280.     jc    errx
  281.     call    IntSnapshot            ; grab the ints so we can
  282.                         ; restoire context later
  283.     mov    dx,offset cs:memerr
  284.     jc    errx2
  285.     mov    si,offset grdbname    ; create data spaces for
  286.     call    MakeEmptyProg            ; empty prog
  287.     mov    dx,offset cs:memerr
  288.     jc    errx2
  289.     call    ReadOptions            ; read the options disk file
  290.     call    FloatCheck            ; check for an FP coprocessor
  291.  
  292.     push    ds                ; parse command line params
  293.     mov    ds,[psp]
  294.     mov    si,81h
  295.     call    ParseProgName
  296.     pop    ds
  297.     test    [loadfile],-1            ; load program
  298.     jz    dohist
  299.     sub    ax,ax
  300.     call    LoadProgram            ; load the program
  301.     lahf
  302.     mov    [neederr],ah
  303. dohist:
  304.     test    [opthist],0ffh            ; init history
  305.     jz    reentry
  306.     call    LoadHistory
  307. ENDIF
  308.     jmp    reentry                ; get us started
  309. ;
  310. ; program exit
  311. ;
  312. _exit:
  313. IFNDEF TEST1
  314.     call    KillFiles            ; close files they opened
  315.     call    CloseLogFile            ; close our log file
  316.     call    rundownflatreal            ; no more flat real mode
  317.     mov    dx,offset cs:nomsg
  318. ENDIF
  319. errx2:
  320. IFNDEF TEST1
  321.     push    dx
  322.     call    UnLoadInts            ; unload int vects
  323.     call    ReleaseMemory            ; release any memory tagged
  324.                         ; to them
  325.     call    ReleaseDebugMem            ; release any memory tagged
  326.                         ; to us
  327.     call    WriteOptions            ; write options file
  328.     pop    dx
  329. ENDIF
  330. errx:
  331.     mov    bx,dx
  332.     call    olMessage
  333.     mov    bx,0                ; close the input file
  334.     mov    ah,3eh
  335.     int    21h
  336.     mov    ax,4c00h
  337.     int    21h
  338.  
  339.     END    start