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

  1. ;
  2. ; GRDP
  3. ;
  4. ; Copyright(c) LADsoft
  5. ;
  6. ; David Lindauer, camille@bluegrass.net
  7. ;
  8. ;
  9. ; Regs.asm
  10. ;
  11. ; Function: Handle register display and input
  12. ;
  13.     ;MASM MODE
  14.     .model small
  15.     .386
  16.  
  17.  
  18. include  eprints.inc 
  19. include  emtrap.inc 
  20. include  einput.inc 
  21. include  edis.inc 
  22. include eoptions.inc
  23. include  ehistory.inc
  24.  
  25.     PUBLIC    DisplayRegisters, ModifyRegisters, ReadReg
  26.     .code
  27. ;
  28. ; This is a list corresponding ASCII names for general purpose regs
  29. ; with the address the value can be found at;
  30. ;
  31.  
  32. peax    dw    offset RegdumpEAX
  33.     db    13,10,"eax:",0
  34. pebx    dw    offset RegdumpEBX
  35.     db    "ebx:",0
  36. pecx    dw    offset RegdumpECX
  37.     db    "ecx:",0
  38. pedx    dw    offset RegdumpEDX
  39.     db    "edx:",0
  40. pesi    dw    offset RegdumpESI
  41.     db    "esi:",0
  42. pedi    dw    offset RegdumpEDI
  43.     db    "edi:",0
  44. pebp    dw    offset RegdumpEBP
  45.     db    13,10,"ebp:",0
  46. pesp    dw    offset RegdumpESP
  47.     db    "esp:",0
  48. peip    dw    offset RegdumpEIP
  49.     db    "eip:",0
  50.     dw    0
  51. ;
  52. ; a list of 8-bit register names
  53. ;
  54. pal    dw    offset RegdumpEAX
  55.     db    13,10,"al:",0
  56.     dw    offset RegdumpEAX+1
  57.     db    "ah:",0
  58.     dw    offset RegdumpEBX
  59.     db    "bl:",0
  60.     dw    offset RegdumpEBX+1
  61.     db    "bh:",0
  62.     dw    offset RegdumpECX
  63.     db    "cl:",0
  64.     dw    offset RegdumpECX+1
  65.     db    "ch:",0
  66.     dw    offset RegdumpEDX
  67.     db    13,10,"dl:",0
  68.     dw    offset RegdumpEDX+1
  69.     db    "dh:",0
  70.     dw    0
  71. ;
  72. ; a list of 16-bit register names
  73. ;
  74. pax    dw    offset RegdumpEAX
  75.     db    13,10,"ax:",0
  76.     dw    offset RegdumpEBX
  77.     db    "bx:",0
  78.     dw    offset RegdumpECX
  79.     db    "cx:",0
  80.     dw    offset RegdumpEDX
  81.     db    "dx:",0
  82.     dw    offset RegdumpESI
  83.     db    "si:",0
  84.     dw    offset RegdumpEDI
  85.     db    "di:",0
  86.     dw    offset RegdumpEBP
  87.     db    13,10,"bp:",0
  88.     dw    offset RegdumpESP
  89.     db    "sp:",0
  90.     dw    offset RegdumpEIP
  91.     db    "ip:",0
  92. pds    dw    offset RegdumpDS
  93.     db    13,10,"ds: ",0    
  94. PES    dw    offset RegdumpES
  95.     db    "es:",0
  96. pfs    dw    offset RegdumpFS
  97.     db    "fs:",0
  98. pgs    dw    offset RegdumpGS
  99.     db    "gs:",0
  100. Pss    dw    offset RegdumpSS
  101.     db    "ss:",0
  102. pcs    dw    offset RegdumpCS
  103.     db    "cs:",0
  104.     dw    0
  105. peflags    dw    offset RegdumpFLAGS
  106.     db    "eflags:",0
  107.     dw    0
  108. flm    label byte
  109.     db    11
  110.     dd    "NVOV"
  111.     db    10
  112.     dd    "UPDN"
  113.     db    9
  114.     dd    "DIEI"
  115.     db    7
  116.     dd    "PLMI"
  117.     db    6
  118.     dd    "NZZR"
  119.     db    4
  120.     dd    "NAAC"
  121.     db    2
  122.     dd    "POPE"
  123.     db    0
  124.     dd    "NCCY"
  125.     db    -1
  126.     db    0
  127. ;
  128. ; Print a general purpose reg and it's value
  129. ;
  130. rPutDword    PROC    
  131.     test    [optdword],0ffh
  132.     jz    rPutWord
  133.     lods    word ptr cs:[si]; Get pointer to val
  134.     mov    bx,ax
  135.     mov    eax,[bx]    ; Get val
  136.     push    eax        ;
  137.     mov    bx,si        ; Get text pointer
  138.     call    olMessage
  139.     pop    eax        ;
  140.     call    printdword    ; Print value
  141.     call    printspace    ;
  142.     ret
  143. rPutDword    ENDP    
  144. ;
  145. ; Print a segment reg and its value
  146. ;
  147. rPutWord    PROC    
  148.     lods    word ptr cs:[si]; Get pointer to value
  149.     mov    bx,ax
  150.     mov    ax,[bx]        ; Get value
  151.     push    ax        ;
  152.     mov    bx,si        ; Pointer to text
  153.     call    olMessage
  154.     pop    ax        ;
  155.     call    printword    ; Print value
  156.     call    printspace    ;
  157.     ret
  158. rPutWord    ENDP    
  159. ;
  160. ; Print either the GP regs or the SEG regs
  161. ; INPUT: DX has the address of the appropriate print routine
  162. ;    SI points to text
  163. ; By implication, two consecutive bytes of 0 exit the routine, else we
  164. ; print each string until we reach that 00 00
  165. ;
  166.  
  167. PrintaFew    PROC    
  168.     call    dx        ; Call the print routine
  169. pf_lp:
  170.     lods    byte ptr cs:[si]; Wade past the text
  171.     or    al,al        ;
  172.     jnz    pf_lp        ;
  173.     test    WORD PTR cs:[si],-1 ; See if trailer found
  174.     jnz    PrintAFew    ; Go print another
  175.     ret
  176. PrintAFew    ENDP    
  177. ;
  178. ; try to find a match for a register spec
  179. ; INPUT: SI points at the input line where we might find a register by name
  180. ;    DI points to an EAX structure consisting of a pointer to a bucket
  181. ;        to put EAX into, followed by a display string of 'EAX'
  182. ; OUTPUT:SI is moved past the register if we found one, else same
  183. ;    DI points to bucket to hold this register value if register found
  184. ;
  185. skimreg PROC
  186.     push    di            ;save address of structure
  187.     push    si            ;save address of input
  188.     xchg    si,di            ;di now input, si is structure
  189.     add    si,2            ;go past EAX bucket pointer to str
  190. srlp:
  191.     cmp    byte ptr cs:[si],' '    ;anything below a space
  192.     jnc    oktry            ;is OK, so skip 0D, 0A at EAX
  193.     inc    si            ;goto next character
  194.     jmp    srlp            ;until letter is found
  195. oktry:
  196.     cmp    byte ptr cs:[si],':'    ;colon follows all register names
  197.     jz    match            ;so if all alike, we found our reg
  198.     cmps    byte ptr cs:[si],es:[di] ;else do a character match
  199.     jz    oktry            ;if a match, keep going to colon
  200.     pop    di            ;else pop address of input to di
  201.     add    sp,2            ;clear struct address off stack
  202. srlp2:
  203.     lods    byte ptr cs:[si]    ;find terminating 0, following colon
  204.     or    al,al            ;is this it?
  205.     jnz    srlp2            ;keep looking until found
  206.     test     word ptr cs:[si],0ffffh    ;table ends with word of 0
  207.     xchg    si,di            ;get our regs straightened out
  208.     jnz    skimreg            ;and examine next struct
  209.     sub    al,al            ;no find, so
  210.     inc    al            ;return no carry
  211.     ret
  212. match:
  213.     add    sp,2            ;clear input address off stack
  214.     sub    eax,eax            ;clear out EAX
  215.     xchg    si,di            ;SI now input past found register
  216.     pop    di            ;di points to top struct
  217.     mov    di,cs:[di]        ;point to his bucket
  218.     stc                ;indicate register found
  219.     ret
  220. skimreg ENDP
  221. ;
  222. ; search all the name tables for a match
  223. ; INPUT: SI points at the input line where we expect to find a register
  224. ; OUTPUT: ZR if found a non-segment register, NZ if we didn't
  225. ;    CY if the request was NOT for a segment register, NC if it was
  226. ;    SI moved past the register on the input line
  227. ;
  228. FindRegister    PROC
  229.     mov    di,offset cs:peax    ;find pointer to EAX bucket
  230.     call    skimreg            ;look for register string match
  231.     mov    cl,4            ;used for dword length
  232.     jc    frnoseg            ;carry means we found dword reg
  233.     mov    di,offset cs:pal    ;else look for byte reg
  234.     call    skimreg            ;save technique
  235.     mov    cl,1            ;set byte length
  236.     jc    frnoseg            ;in case we found it
  237.     mov    di,offset cs:peflags    ;else maybe user doing flags?
  238.     call    skimreg            ;try 'eflags'
  239.     mov    cl,4            ;say 4 bytes for this
  240.     jc    frnoseg            ;if found
  241.     mov    bl,[si+1]        ;else bl=2d char in input string
  242.     mov    di,offset cs:pax    ;look for word regs
  243.     call    skimreg            ;in the table
  244.     mov    cl,2            ;assume word
  245.     jnc    frnotfound        ;if not found, bl is ???
  246.     cmp    bl,'s'            ;might it be a segment reg?
  247.     stc                ;assume not???
  248.     jnz    frnoseg            ;jmp if not
  249.     clc                ;else clear carry
  250. frnoseg:
  251.     mov    al,0ffH            ;set ZF without affecting carry flag
  252.     inc    al
  253.     ret                ;and return success
  254. frnotfound:
  255.     sub    ax,ax            ;set NZ
  256.     inc    ax
  257.     stc                ;and the carry
  258.     ret
  259. FindRegister    ENDP
  260. ;
  261. ; read the value of a reg (used by input routines)
  262. ; INPUT: SI points at the input line 
  263. ; OUTPUT:If we found a byte or word register, EAX holds the zero-extended
  264. ;    value that was in the corresponding bucket.
  265. ;    If we found a dword register, EAX holds the dword value
  266. ;    NZ if register not found by name, but still might be a number
  267. ;        Else, NC if segment reg, CY if non-segment reg found
  268. ;    SI moved past a register found by name, else not moved
  269. ;
  270. ReadReg        PROC
  271.     push    ecx            ;ECX holds ???
  272.     call    FindRegister        ;see if we found a non-segment reg
  273.     jnz    notreg            ;if not, jmp
  274.     pushf                ;save flags (carry?)
  275.     mov    eax,[di]        ;get value from bucket
  276.     cmp    cl,4            ;was it a dword?
  277.     jz    rr_exit            ;if so, exit
  278.     movzx    eax,ax            ;else zero extend into EAX
  279.     cmp    cl,2            ;was it a word register
  280.     jz    rr_exit            ;if so, we have it
  281.     movzx    eax,al            ;else zero extend EAX
  282. rr_exit:
  283.     popf                ;restore flags
  284. notreg:
  285.     pop    ecx            ;and ECX
  286.     ret
  287. ReadReg    ENDP
  288. ;
  289. ; Read value for a register back into memory (R) command
  290. ;
  291. ReadRegValue    PROC
  292.     call    WadeSpace
  293.     jz    doregprompt
  294.     inc    si
  295.     cmp    al,':'
  296.     jz    ReadRegValue
  297.     cmp    al,'='
  298.     jz    ReadRegValue
  299.     dec    si
  300.     call    ReadNumber
  301.     ret
  302. doregprompt:
  303.     push    bx            ; Else put up prompt
  304.     push    cx            ;
  305.     PRINT_MESSAGE    <13,10,": ">
  306.     call    histoff
  307.     call    GetInputLine        ; Get input line
  308.     call    histon
  309.     pop    cx            ;
  310.     pop    bx            ;
  311.     call    WadeSpace        ; Ignore spaces
  312.     jz    short rr_out        ; Quit if so
  313.     call    ReadNumber
  314.     ret
  315. rr_out:
  316.     stc
  317.     ret
  318. ReadRegValue    ENDP    
  319. ;
  320. ; main 'Reg' command
  321. ;
  322. ModifyRegisters    PROC    
  323.     call    wadespace        ; Wade through spaces
  324.     jz    DisplayRegisters    ; Display regs
  325.     call    FindRegister
  326.     jnz    badreg
  327.     push    di
  328.     call    ReadRegValue
  329.     pop    di
  330.     jc    badreg2
  331.     cmp    cl,4
  332.     jnz    wordreg
  333.     mov    [di],eax
  334.     clc
  335.     ret
  336. wordreg:
  337.     cmp    cl,2
  338.     jnz    bytereg
  339.     mov    [di],ax
  340. badreg2:
  341.     clc
  342.     ret
  343. badreg:
  344.     stc
  345.     ret
  346. bytereg:
  347.     mov    [di],al
  348.     ret
  349. ModifyRegisters ENDP
  350. putflags PROC
  351.     mov    si,offset cs:flm
  352. putflags2:
  353.     lods    byte ptr cs:[si]
  354.     or    al,al
  355.     js    pfdone
  356.     movzx    ax,al
  357.     bt    word ptr [RegdumpFLAGS],ax
  358.     lods    dword ptr cs:[si]
  359.     mov    edx,eax
  360.     jc    isclr
  361.     shr    edx,16
  362. isclr:
  363.     xchg    dh,dl
  364.     call    PutChar
  365.     xchg    dl,dh
  366.     call    PutChar
  367.     call    PrintSpace
  368.     jmp    putflags2
  369. pfdone:
  370.     ret
  371. putflags ENDP
  372. ;
  373. ; Display the processor regs
  374. ;
  375. DisplayRegisters    PROC    
  376.     mov    word ptr [RegdumpEIP+2],0    ;clear high word of EIP 
  377.     pushfd                    ;move eflags
  378.     pop    eax                ;to EAX
  379.     shr    eax,16                ;get upper 16 flagbits
  380.     mov    word ptr [RegdumpFLAGS+2],ax    ;save in high flag bucket
  381.     mov    si, offset cs:peax        ; Print GP regs
  382.     mov    dx,offset cs:rPutDword        ; with the DWORD function
  383.     call    PrintAFew            ; Print them
  384.     mov    si,offset cs:peflags        ;Put the flags
  385.     call    rPutDword        
  386.     call    putflags
  387.     mov    si,offset cs:pds        ; Now put the segs
  388.     mov    dx,offset cs:rPutWord    
  389.     call    PrintAFew        
  390.     mov    bx,word ptr [RegdumpEIP]    ; Dissassemble at current code pointer
  391.     mov    dx,[RegdumpCS]        
  392.     call    DisOneLine        
  393.     clc
  394.     ret
  395. DisplayRegisters    ENDP    
  396. END