home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / ASM / GRDBDL17.ZIP / EXEC.ASM < prev    next >
Encoding:
Assembly Source File  |  1998-11-18  |  6.6 KB  |  338 lines

  1. ;
  2. ; GRDP
  3. ;
  4. ; Copyright(c) LADsoft
  5. ;
  6. ; David Lindauer, camille@bluegrass.net
  7. ;
  8. ;
  9. ; EXEC.ASM
  10. ;
  11. ; Function: Handle T,G,P commands
  12. ;
  13.     ;MASM MODE
  14.     .model small
  15.     .386
  16.  
  17.  
  18. include  eprints.inc 
  19. include  einput.inc 
  20. include  emtrap.inc 
  21. include  ebreaks.inc 
  22. include  edos.inc
  23. include  eints.inc
  24. include eregs.inc
  25. include eswift.inc
  26.  
  27. TRAPFLAG = 100h            ; 80386 trap enable flag
  28.  
  29.     PUBLIC go,trap, proceed, ReTrap, TrapCount, callcheck, cmpstring, wadeprefix
  30.  
  31.  
  32.     .data
  33. trapcount    dd    0    ;number of traps left to do or zero for none
  34. pfx67flag    dw    0     ;flag if 67h prefix found during call check
  35.  
  36. ;All possible prefixes
  37.  
  38. pretab    db    0f0h,0f2h,0f3h,026h,02eh,036h,03eh,064h,065h,066h,067h
  39.  
  40. ;String functions
  41.  
  42. stringtab db    0a6h,0a7h,6ch,6dh
  43.     db    0ach,0adh,0a4h,0a5h
  44.     db    06eh,06fh,0aeh,0afh
  45.     db    0aah,0abh
  46.  
  47.     .code
  48. ;
  49.     assume ds:nothing    ; going to call from where DS is moot
  50. ;
  51. callcheck    proc
  52.     mov    ah,fs:[bx]    ; Load the first byte of the instruction
  53.     cmp    ah,0ceh        ; into?
  54.     mov    al,1
  55.     jz    chx
  56.     cmp    ah,0cdh        ; Interrupt ?
  57.     mov    al,2        ; two bytes
  58.     jz    chx        ; I do this because some int calls are
  59.                 ; misbehaved and won't restore the trap flag
  60.     cmp    ah,0e8h        ; Near Call?
  61.     mov    al,3        ; Yes, this is 3 bytes
  62.     jz    chx        ; And execute it
  63.     cmp    ah,09ah        ; Far call
  64.     mov    al,5        ; This one is 5 bytes
  65.     jz    chx        ; Not either of these, just trap
  66.     mov    ax,fs:[bx]
  67.     and    ax,038FFH
  68.     cmp    ax,010ffh
  69.     jz    gotind
  70.     cmp    ax,018ffh
  71.     jz    gotind
  72.     ret
  73. gotind:
  74.     mov    al,2          ; two bytes base
  75.     mov    ah,fs:[bx+1]
  76.            and    ah,0c7h
  77.     cmp    ah,0c0h        ; mod 3, through reg is 2 bytes
  78.     jnc    chx
  79.     test    [pfx67flag],0ffh    ; check 16/32 address mode flag
  80.     jnz    call32
  81.     cmp    ah,8        ; 16 bit, MOD=0
  82.     jnc    chk16m1
  83.     cmp    ah,6        ; yes, direct offset?
  84.     jnz    chx
  85.     add    al,2        ; yes 4 bytes total
  86.     jmp    chx
  87. chk16m1:
  88.     and    ah,0c0h        ; check for mod 1 or 2
  89.     js    is16m2
  90.     inc    al        ; mod 1, add a byte for offset
  91.     jmp    chx
  92. is16m2:
  93.     add    al,2        ; else mod 2, two byte offset
  94.     jmp    chx
  95. call32:
  96.     cmp    ah,6        ; 32 bit mode, check for 32-bit offset
  97.     jnz    ch32m0
  98.     add    al,4        ; yep, add 4
  99.     jmp    ch32sib
  100. ch32m0:
  101.     cmp    ah,8           ; MOD = 0?
  102.     jc    ch32sib        ; yep, check for sib
  103. ch32m1:
  104.     or    ah,ah        ; MOD = 1?
  105.     js    ch32m2
  106.     add    al,1           ; yep, 1 byte offset
  107.     jmp    ch32sib
  108. ch32m2:
  109.     add    al,4           ; else mod 2
  110. ch32sib:
  111.     and    ah,7
  112.     cmp    ah,4
  113.     jnz    chx
  114.     inc    al        ; add in the SIB byte
  115.     mov    ah,byte ptr fs:[bx+1]    ; test for MOD 3
  116.     and    ah,0c0h
  117.     cmp    ah,0c0h
  118.     jz    chx        ; yep, no further bytes
  119.     mov    ah,byte ptr fs:[bx+2] ; get SIB byte
  120.     and    ah,7        ; check for EBP special cases
  121.     cmp    ah,5
  122.     jnz    chx
  123.     inc    al
  124.     test    byte ptr fs:[bx+1],40h ; check if one or 4 bytes disp
  125.     jz    chx
  126.     add    al,3        ; MOD 1,3, need 4 bytes
  127. chx:
  128.     add    al,dl        ; fix for prefixes
  129.     sub    ah,ah        ; clear Z flag...
  130.     ret
  131. callcheck    endp
  132. ; used by P command to wade through prefixes to find out if
  133. ; we have a string instruction
  134. ;
  135. wadeprefix:
  136.     mov    [pfx67flag],0
  137.     sub    dx,dx
  138. wp_lp:
  139.     mov    al,fs:[bx]
  140.     mov    di,offset pretab
  141.     mov    cx,11
  142.     repnz    scasb
  143.     jnz    wp_done
  144.     inc    bx
  145.     inc    dx
  146.     cmp    al,67h
  147.     jnz    wp_lp
  148.     bts    [pfx67flag],0        ; flag the addrsiz in case of call
  149.     jmp    wp_lp
  150. wp_done:
  151.     ret
  152.     
  153. cmpstring:
  154.     mov    al,fs:[bx]
  155.     mov    di,offset stringtab
  156.     mov    cx,14
  157.     repnz    scasb
  158.     ret
  159. ;
  160.     assume    ds:dgroup    ; DS ok again
  161. ;
  162. ; Execute program
  163. ;
  164. go    PROC    
  165.     Call    WadeSpace    ; Wade till address
  166.     jz    dogo
  167.     cmp    al,'='
  168.     jnz    checkbreak
  169.     inc    si
  170.     call    ReadAddress
  171.     jc    goerr
  172.     mov    word ptr [RegdumpEIP],bx    ; Fix CS:EIP for new routine
  173.     call    defCS        ; get CS
  174. checkbreak:
  175.     call    WadeSpace    ; Wade
  176.     jz    short dogo    ;
  177.     call    ReadAddress    ; Read break address
  178.     jc    goerr        ; Quit if errir
  179. dogo2:
  180.     sub    ax,ax        ; Break 0
  181.     call    SetBreak    ; Set the break
  182. dogo:
  183.     call    EnableBreaks    ; Enable breaks
  184.     xor    ax,ax        ; Not trapping
  185.     jmp    gotrap    ; Run the code
  186. goerr:
  187.     stc
  188.     ret
  189. go    ENDP    
  190. ;
  191. ;
  192. ; Limited and semi-buggy proced function
  193. ;
  194. PROCEED    PROC    
  195.     call    WadeSpace
  196.     jz    pok
  197.     cmp    al,'r'
  198.     jnz    perr
  199.     inc    si
  200.     call    WadeSpace
  201.     jnz    perr
  202.     mov    cs:[traceon],1
  203.     jmp    gotrap
  204. pok:
  205.     movzx    ebx,word ptr [RegdumpEIP]    ;
  206.     mov    fs,[RegdumpCS]    ;
  207.     call    wadeprefix    ; wade through all prefixes
  208.     call    callcheck      ; noew check for near  & far calls
  209.     jz    short pgo    ; and execute it
  210.     mov    ah,fs:[bx]
  211.     mov    al,2
  212.     cmp    ah,0e0h        ; Check for loop instructions
  213.     jz    short    pgo
  214.     cmp    ah,0e1h
  215.     jz    short    pgo
  216.     cmp    ah,0e2h
  217.     jz    short    pgo
  218.     call    cmpstring    ; see if is string instruction or loop
  219.     jnz    short dotrap    ; Not either of these, just trap
  220.     mov    ax,dx
  221.     inc    ax
  222. pgo:
  223.     sub    ah,ah
  224.     movzx    ebx,word ptr [RegdumpEIP]    ;
  225.     add    bx,ax        ; Ebx = breakpoint
  226.     mov    dx,[RegdumpCS]    ; DX:EBX = position to break at
  227.     sub    ax,ax        ; Use the scratch breakpoint
  228.     call    SetBreak    ; Set a break
  229.     call    EnableBreaks    ; Enable breakpoints
  230.     sub    ax,ax        ; No trapping
  231.     jmp    gotrap    ; Run the code
  232. perr:
  233.     stc
  234.     ret
  235. PROCEED    ENDP    
  236. ;
  237. ; Trap command
  238. ;
  239. trap    PROC    
  240.     call    WadeSpace
  241.     jz    dotrap
  242.     call    ReadNumber
  243.     jc    terr
  244.     push    eax
  245.     call    WadeSpace
  246.     pop    eax
  247.     jz    tnerr
  248. terr:
  249.     stc
  250.     ret
  251. tnerr:
  252.            or    eax,eax
  253.     jz    dotrap
  254.     dec    eax
  255.     mov    [trapcount],eax
  256. trap    ENDP
  257. dotrap     PROC
  258.     mov    fs,[RegdumpCS]    ;
  259.     mov    bx,word ptr [RegdumpEIP]    ;
  260.     mov    ah,fs:[bx]    ; Load the first byte of the instruction
  261.     cmp    BYTE PTR fs:[bx],0cdh
  262.     jnz    ReTrap
  263.     mov    al,byte ptr fs:[bx+1]
  264.     cmp    al,20h
  265.     jz    dosvec
  266.     cmp    al,21h
  267.     jnz    stepvec
  268.     mov    ebx,cs:[int21adr]
  269.     jmp    gotadr
  270. stepvec:
  271.     push    0              ; other int instructions do a step
  272.     pop    fs
  273.     jmp    gotadr
  274. dosvec:
  275.     mov    fs,[intpage]
  276. gotadr:
  277.     movzx    bx,al            ; trying to trace through a DOS
  278.     shl    bx,2
  279.     mov    ebx,fs:[bx]
  280.     push    es            ; vector bypasses our hook
  281.     mov    es,[RegdumpSS]
  282.     mov    di,word ptr [RegdumpESP]
  283.     sub    [RegdumpESP],6
  284.     std
  285.     scasw
  286.     mov    ax,word ptr [RegdumpFLAGS]
  287.     stosw
  288.     mov    ax,[RegdumpCS]
  289.     stosw
  290.     mov    ax,word ptr [RegdumpEIP]
  291.     add    ax,2
  292.     stosw
  293.     cld
  294.     pop    es
  295.     mov    word ptr [RegdumpEIP],bx
  296.     shr    ebx,16
  297.     mov    [RegdumpCS],bx
  298.     call    DisplayRegisters; Display registers
  299.     clc
  300.     ret
  301. dotrap    ENDP
  302. ReTrap    PROC
  303.     mov    ax,TRAPFLAG    ; Are trapping on instruction
  304. gotrap    PROC
  305.     push    ax
  306.     mov    si,offset dgroup : veclist    ; enable ints
  307.     call    SetRMInts
  308.     pop    ax
  309.     mov    ss,[RegdumpSS]
  310.     mov    esp,[RegdumpESP]    ; Load toss
  311.     or    ax,word ptr [RegdumpFLAGS]    ; Fill stack frame with FLAGS , CS:EIP
  312.     push    ax        ;
  313.     call    SetUserPSP
  314.     push    [RegdumpCS]
  315.     push    word ptr [RegdumpEIP]
  316.     push    [RegdumpDS]
  317.     mov    es,[RegdumpES]
  318.     mov    fs,[RegdumpFS]
  319.     mov    gs,[RegdumpGS]
  320.     mov    eax,[RegdumpEAX]    ; Load regs
  321.     mov    ebx,[RegdumpEBX]    ;
  322.     mov    ecx,[RegdumpECX]    ;
  323.     mov    edx,[RegdumpEDX]    ;
  324.     mov    esi,[RegdumpESI]    ;
  325.     mov    edi,[RegdumpEDI]    ;
  326.     mov    ebp,[RegdumpEBP]    ;
  327.     pop    ds        ; Load DS
  328.     test    cs:[traceon],1    ; if PR go handle first instruction
  329.     jz    doiret
  330.     call    swiftrace
  331.     call    DisplayRegisters; if it was an int 3 come back
  332.     clc
  333.     ret
  334. doiret:
  335.     iret
  336. gotrap    ENDP
  337. Retrap    ENDP    
  338. end