home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l440 / 2.ddi / CHAP7 / COMMANDS.ASM < prev    next >
Encoding:
Assembly Source File  |  1990-06-17  |  14.9 KB  |  570 lines

  1.     page    78,132
  2.     title    Commands for Monitor
  3.  
  4.     .model    small
  5.     .code
  6.     .data
  7. InitSeg segment byte public
  8. InitSeg ends
  9.  
  10. DGroup    group    _TEXT,_DATA,InitSeg
  11.  
  12.     .data
  13.  
  14.     extrn    DsSave:word, CsSave:word, IpSave:word
  15.  
  16. DEFLEN    dw    ?            ;Default length of range
  17. DEFDUMP    dd    ?            ;Default dump address
  18. ListBuf    db    80 dup(?)
  19.  
  20.     .code
  21.     assume    cs:DGroup,ds:DGroup,es:DGroup,ss:DGroup
  22.  
  23.     public    Compare,Dump,Enter,Fill,Input,Move,Output,Search
  24.     public    Default,OutSi
  25.  
  26.     extrn    OutCh:near, CrLf:near, Command:near
  27.     extrn    InCh:near, Backup:near, Out16:near, Blank:near
  28.     extrn    ScanB:near, ScanP:near, Hex:near, Error:near
  29.     extrn    HexIn:near, HexChk:near, Tab:near, Address:near, GetHex:near
  30.     extrn    GetEol:near
  31.  
  32. ;Print the hex address of SI and DS
  33.  
  34. OUTSI:
  35.     MOV    DX,DS        ;Put DS where we can work with it
  36.     CALL    OUT16        ;Display segment
  37.     MOV    DX,SI
  38.     jmp    short OUTADD        ;Finish below
  39.  
  40. ;Print digit hex address of DI and ES
  41. ;Same as OUTSI above
  42.  
  43. OUTDI:
  44.     MOV    DX,ES
  45.     CALL    OUT16
  46.     MOV    DX,DI
  47. ;Finish OUTSI here too
  48. OUTADD:
  49.     MOV    AL,":"
  50.     CALL    OutCh
  51.     jmp    Out16
  52.  
  53.  
  54. ;RANGE - Looks for parameters defining an address range.
  55. ;The first parameter is a hex number of 5 or less digits
  56. ;which specifies the starting address. The second parameter
  57. ;may specify the ending address, or it may be preceded by
  58. ;"L" and specify a length (4 digits max), or it may be
  59. ;omitted and a length of 128 bytes is assumed. Returns with
  60. ;segment in AX, displacement in DX, length in CX.
  61.  
  62. DSRANGE:
  63.     MOV    BP,[DSSave]    ;Default segment is DS
  64.     MOV    [DEFLEN],128    ;and default length to 128 bytes
  65. RANGE:
  66.     CALL    ADDRESS
  67.     PUSH    AX        ;Save segment
  68.     PUSH    DX        ;Save offset
  69.     CALL    SCANP        ;Get to next parameter
  70.     CMP    AL,"L"        ;Length indicator?
  71.     JE    GETLEN
  72.     MOV    DX,[DEFLEN]    ;Default length
  73.     CALL    HEXIN        ;Second parameter present?
  74.     JC    RNGRET        ;If not, use default
  75.     MOV    CX,4        ;4 hex digits
  76.     CALL    GETHEX        ;Get ending address (same segment)
  77.     MOV    CX,DX        ;Low 16 bits of ending addr.
  78.     POP    DX        ;Low 16 bits of starting addr.
  79.     SUB    CX,DX        ;Compute range
  80.     INC    CX        ;Include last location
  81.     POP    AX        ;Segment of starting address
  82.     RET
  83.  
  84. GETLEN:
  85.     INC    SI        ;Skip over "L" to length
  86.     MOV    CX,4        ;Length may have 4 digits
  87.     CALL    GETHEX        ;Get the range
  88. RNGRET:
  89.     MOV    CX,DX        ;Length
  90.     POP    DX        ;Offset of starting addr.
  91.  
  92.     POP    AX        ;Segment of starting addr.
  93.     RET
  94.  
  95. DEFAULT:
  96. ;DI points to default address and CX has default length
  97.     CALL    SCANP
  98.     JZ    USEDEF        ;Use default if no parameters
  99.     CMP    AL,"L"
  100.     JZ    NEWLEN
  101.     MOV    [DEFLEN],CX
  102.     CALL    RANGE
  103.     JMP    GETEOL
  104.  
  105. NEWLEN:
  106.     INC    SI
  107.     MOV    CX,4
  108.     CALL    GETHEX
  109.     MOV    CX,DX        ;Get new length
  110. USEDEF:
  111.     MOV    SI,DI
  112.     LODSW            ;Get default displacement
  113.     MOV    DX,AX
  114.     LODSW            ;Get default segment
  115. Ret1:    RET
  116.  
  117.  
  118. ;************************************************************
  119. ; "C" command
  120. ;Compare one area of memory to another. 
  121.  
  122. COMPARE:
  123.     CALL    DSRANGE        ;Get range of first area
  124.     PUSH    CX        ;Save length
  125.     PUSH    AX        ;Save segment
  126.     PUSH    DX        ;Save offset
  127.     CALL    ADDRESS        ;Get second area
  128.     CALL    GETEOL        ;Check for errors
  129.     POP    SI
  130.     MOV    DI,DX
  131.     MOV    ES,AX
  132.     POP    DS
  133.     POP    CX        ; Length
  134.     DEC    CX
  135.     CALL    COMP        ; Do one less than total
  136.     INC    CX        ; CX=1 (do last one)
  137. COMP:
  138. REPE    CMPSB
  139.     JZ    RET1
  140. ; Compare error. Print address, value; value, address.
  141.     DEC    SI
  142.     CALL    OUTSI
  143.     CALL    BLANK
  144.     CALL    BLANK
  145.     LODSB
  146.     CALL    HEX
  147.     CALL    BLANK
  148.     CALL    BLANK
  149.     DEC    DI
  150.     MOV    AL,ES:[DI]
  151.     CALL    HEX
  152.     CALL    BLANK
  153.     CALL    BLANK
  154.     CALL    OUTDI
  155.     INC    DI
  156.     CALL    CRLF
  157.     XOR    AL,AL
  158.     jmp    COMP
  159.  
  160.  
  161. ;************************************************************
  162. ; "D" command
  163. ; Dump an area of memory in both hex and ASCII
  164.  
  165. DUMP:
  166.     MOV    BP,[DSSave]    ;Default segment is DS
  167.     MOV    CX,128
  168.     MOV    DI,offset DGroup:DEFDUMP
  169.     CALL    DEFAULT        ;Get range to dump
  170.     MOV    DS,AX        ;Set segment
  171.     MOV    SI,DX        ;SI has displacement in segment
  172. ROW:
  173.     CALL    OUTSI        ;Print address at start of line
  174.     PUSH    SI        ;Save address for ASCII dump
  175.     CALL    BLANK
  176. EachByte:
  177.     CALL    BLANK        ;Space between bytes
  178. BYTE1:
  179.     LODSB            ;Get byte to dump
  180.     CALL    HEX        ;and display it
  181.     POP    DX        ;DX has start addr. for ASCII dump
  182.     DEC    CX        ;Drop loop count
  183.     JZ    ASCII        ;If through do ASCII dump
  184.     MOV    AX,SI
  185.     TEST    AL,0FH        ;On 16-byte boundary?
  186.     JZ    ENDROW
  187.     PUSH    DX        ;Didn't need ASCII addr. yet
  188.     TEST    AL,7        ;On 8-byte boundary?
  189.     JNZ    EachByte
  190.     MOV    AL,"-"        ;Mark every 8 bytes
  191.     CALL    OutCh
  192.     jmp    BYTE1
  193. ENDROW:
  194.     CALL    ASCII        ;Show it in ASCII
  195.     jmp    ROW        ;Loop until count is zero
  196. ASCII:
  197.     PUSH    CX        ;Save byte count
  198.     MOV    AX,SI        ;Current dump address
  199.     MOV    SI,DX        ;ASCII dump address
  200.     SUB    AX,DX        ;AX=length of ASCII dump
  201. ;Compute tab length. ASCII dump always appears on right side
  202. ;screen regardless of how many bytes were dumped. Figure 3
  203. ;characters for each byte dumped and subtract from 51, which
  204. ;allows a minimum of 3 blanks after the last byte dumped.
  205.     MOV    BX,AX
  206.     SHL    AX,1        ;Length times 2
  207.     ADD    AX,BX        ;Length times 3
  208.     MOV    CX,51
  209.     SUB    CX,AX        ;Amount to tab in CX
  210.     CALL    TAB
  211.     MOV    CX,BX        ;ASCII dump length back in CX
  212. ASCDMP:
  213.     LODSB            ;Get ASCII byte to dump
  214.     AND    AL,7FH        ;ASCII uses 7 bits
  215.     CMP    AL,7FH        ;Don't try to print RUBOUT
  216.     JZ    NOPRT
  217.     CMP    AL," "        ;Check for control characters
  218.     JNC    PRIN
  219. NOPRT:
  220.     MOV    AL,"."        ;If unprintable character
  221. PRIN:
  222.     CALL    OutCh        ;Print ASCII character
  223.     LOOP    ASCDMP        ;CX times
  224.     POP    CX        ;Restore overall dump length
  225.     MOV    word ptr ES:[DEFDUMP],SI
  226.     MOV    word ptr ES:[DEFDUMP+2],DS    ;Remember last addrss as default
  227.     JMP    CRLF        ;Print CR/LF and return
  228.  
  229.  
  230. ;************************************************************
  231. ; "M" command
  232. ;Block move one area of memory to another. Overlapping moves
  233. ;are performed correctly, i.e., so that a source byte is not
  234. ;overwritten until after it has been moved.
  235.  
  236. MOVE:
  237.     CALL    DSRANGE        ;Get range of source area
  238.     PUSH    CX        ;Save length
  239.     PUSH    AX        ;Save segment
  240.     PUSH    DX        ;Save offset
  241.     CALL    ADDRESS        ;Get destination address
  242.     CALL    GETEOL        ;Check for errors
  243.     POP    SI
  244.     MOV    DI,DX        ;Set dest. displacement
  245.     POP    BX        ;Source segment
  246.     MOV    DS,BX
  247.     MOV    ES,AX        ;Destination segment
  248.     POP    CX        ;Length
  249.     CMP    DI,SI        ;Check direction of move
  250.     SBB    AX,BX        ;Extend the CMP to 32 bits
  251.     JB    COPYLIST    ;Move forward into lower mem.
  252. ;Otherwise, move backward. Figure end of source and destination
  253. ;areas and flip direction flag.
  254.     DEC    CX
  255.     ADD    SI,CX        ;End of source area
  256.     ADD    DI,CX        ;End of destination area
  257.     std            ;Reverse direction
  258.     INC    CX
  259. COPYLIST:
  260.     MOVSB            ;Do at least 1 - Range is 1-10000H not 0-FFFFH
  261.     DEC    CX
  262. REP    MOVSB            ;Block move
  263.     JMP    COMMAND        ;Jump in case stack got trashed by move
  264.  
  265.  
  266. ;************************************************************
  267. ; "F" command
  268. ;Fill an area of memory with a list values. If the list
  269. ;is bigger than the area, don't use the whole list. If the
  270. ;list is smaller, repeat it as many times as necessary.
  271.  
  272. FILL:
  273.     CALL    DSRANGE        ;Get range to fill
  274.     PUSH    CX        ;Save length
  275.     PUSH    AX        ;Save segment number
  276.     PUSH    DX        ;Save displacement
  277.     CALL    LIST        ;Get list of values to fill with
  278.     POP    DI        ;Displacement in segment
  279.     POP    ES        ;Segment
  280.     POP    CX        ;Length
  281.     CMP    BX,CX        ;BX is length of fill list
  282.     MOV    SI,offset DGroup:ListBuf    ;List is in line buffer
  283.     JCXZ    BIGRNG
  284.     JAE    COPYLIST    ;If list is big, copy part of it
  285. BIGRNG:
  286.     SUB    CX,BX        ;How much bigger is area than list?
  287.     XCHG    CX,BX        ;CX=length of list
  288.     PUSH    DI        ;Save starting addr. of area
  289. REP    MOVSB            ;Move list into area
  290.     POP    SI
  291. ;The list has been copied into the beginning of the 
  292. ;specified area of memory. SI is the first address
  293. ;of that area, DI is the end of the copy of the list
  294. ;plus one, which is where the list will begin to repeat.
  295. ;All we need to do now is copy [SI] to [DI] until the
  296. ;end of the memory area is reached. This will cause the
  297. ;list to repeat as many times as necessary.
  298.     MOV    CX,BX        ;Length of area minus list
  299.     PUSH    ES        ;Different index register
  300.     POP    DS        ;requires different segment reg.
  301.     jmp    COPYLIST    ;Do the block move
  302.  
  303.  
  304. ;************************************************************
  305. ; "S" command
  306. ;Search a specified area of memory for given list of bytes.
  307. ;Print address of first byte of each match.
  308.  
  309. SEARCH:
  310.     CALL    DSRANGE        ;Get area to be searched
  311.     PUSH    CX        ;Save count
  312.     PUSH    AX        ;Save segment number
  313.     PUSH    DX        ;Save displacement
  314.     CALL    LIST        ;Get search list
  315.     DEC    BX        ;No. of bytes in list-1
  316.     POP    DI        ;Displacement within segment
  317.     POP    ES        ;Segment
  318.     POP    CX        ;Length to be searched
  319.     SUB    CX,BX        ;  minus length of list
  320. SCAN:
  321.     MOV    SI,offset DGroup:ListBuf    ;List kept in line buffer
  322.     LODSB            ;Bring first byte into AL
  323. DOSCAN:
  324.     SCASB            ;Search for first byte
  325.     LOOPNE    DOSCAN        ;Do at least once by using LOOP
  326.     JNZ    RET2        ;Exit if not found
  327.     PUSH    BX        ;Length of list minus 1
  328.     XCHG    BX,CX
  329.     PUSH    DI        ;Will resume search here
  330. REPE    CMPSB            ;Compare rest of string
  331.     MOV    CX,BX        ;Area length back in CX
  332.     POP    DI        ;Next search location
  333.     POP    BX        ;Restore list length
  334.     JNZ    TestEndScan    ;Continue search if no match
  335.     DEC    DI        ;Match address
  336.     CALL    OUTDI        ;Print it
  337.     INC    DI        ;Restore search address
  338.     CALL    CRLF
  339. TestEndScan:
  340.     JCXZ    RET2
  341.     jmp    SCAN        ;Look for next occurrence
  342.  
  343.  
  344. ;Process one parameter when a list of bytes is
  345. ;required. Carry set if parameter bad. Called by LIST
  346.  
  347. LISTITEM:
  348.     CALL    SCANP        ;Scan to parameter
  349.     CALL    HEXIN        ;Is it in hex?
  350.     JC    STRINGCHK    ;If not, could be a string
  351.     MOV    CX,2        ;Only 2 hex digits for bytes
  352.     CALL    GETHEX        ;Get the byte value
  353.     MOV    [BX],DL        ;Add to list
  354.     INC    BX
  355. GRET:    CLC            ;Parameter was OK
  356. Ret2:    RET
  357.  
  358. STRINGCHK:
  359.     MOV    AL,[SI]        ;Get first character of param
  360.     CMP    AL,"'"        ;String?
  361.     JZ    STRING
  362.     CMP    AL,'"'        ;Either quote is all right
  363.     JZ    STRING
  364.     STC            ;Not string, not hex - bad
  365.     RET
  366.  
  367. STRING:
  368.     MOV    AH,AL        ;Save for closing quote
  369.     INC    SI
  370. STRNGLP:
  371.     LODSB            ;Next char of string
  372.     CMP    AL,13        ;Check for end of line
  373.     JZ    ErrorJ        ;Must find a close quote
  374.     CMP    AL,AH        ;Check for close quote
  375.     JNZ    STOSTRG        ;Add new character to list
  376.     CMP    AH,[SI]        ;Two quotes in a row?
  377.     JNZ    GRET        ;If not, we're done
  378.     INC    SI        ;Yes - skip second one
  379. STOSTRG:
  380.     MOV    [BX],AL        ;Put new char in list
  381.     INC    BX
  382.     jmp    STRNGLP        ;Get more characters
  383.  
  384. ErrorJ:    jmp    Error
  385.  
  386. ;Get a byte list for ENTER, FILL or SEARCH. Accepts any number
  387. ;of 2-digit hex values or character strings in either single
  388. ;(') or double (") quotes.
  389.  
  390. LIST:
  391.     MOV    BX,offset DGroup:ListBuf    ;Put byte list in the line buffer
  392. LISTLP:
  393.     CALL    LISTITEM    ;Process a parameter
  394.     JNC    LISTLP        ;If OK, try for more
  395.     SUB    BX,offset DGroup:ListBuf    ;BX now has no. of bytes in list
  396.     JZ    ErrorJ        ;List must not be empty
  397.     jmp    GetEol
  398.  
  399.  
  400. ;************************************************************
  401. ; "E" command
  402.  
  403. ;Short form of ENTER command. A list of values from the
  404. ;command line are put into memory without using normal
  405. ;ENTER mode.
  406.  
  407. GETLIST:
  408.     CALL    LIST        ;Get the bytes to enter
  409.     POP    DI        ;Displacement within segment
  410.     POP    ES        ;Segment to enter into
  411.     MOV    SI,offset DGroup:ListBuf    ;List of bytes is in line buffer
  412.     MOV    CX,BX        ;Count of bytes
  413. REP    MOVSB            ;Enter that byte list
  414.     RET
  415.  
  416. ;Enter values into memory at a specified address. If the
  417. ;line contains nothing but the address we go into "enter
  418. ;mode", where the address and its current value are printed
  419. ;and the user may change it if desired. To change, type in
  420. ;new value in hex. Backspace works to correct errors. If
  421. ;an illegal hex digit or too many digits are typed, the
  422. ;bell is sounded but it is otherwise ignored. To go to the
  423. ;next byte (with or without change), hit space bar. To
  424. ;back up to a previous address, type "-". On
  425. ;every 8-byte boundary a new line is started and the address
  426. ;is printed. To terminate command, type carriage return.
  427. ;   Alternatively, the list of bytes to be entered may be
  428. ;included on the original command line immediately following
  429. ;the address. This is in regular LIST format so any number
  430. ;of hex values or strings in quotes may be entered.
  431.  
  432. ENTER:
  433.     MOV    BP,[DSSave]    ;Default segment
  434.     CALL    ADDRESS        ;Get ENTER address
  435.     PUSH    AX        ;Save for later
  436.     PUSH    DX
  437.     CALL    SCANB        ;Any more parameters?
  438.     JNZ    GETLIST        ;If not end-of-line get list
  439.     POP    DI        ;Displacement of ENTER
  440.     POP    ES        ;Segment
  441. GETROW:
  442.     CALL    OUTDI        ;Print address of entry
  443.     CALL    BLANK        ;Leave a space
  444.     CALL    BLANK
  445. GETBYTE:
  446.     MOV    AL,ES:[DI]        ;Get current value
  447.     CALL    HEX        ;And display it
  448.     MOV    AL,"."
  449.     CALL    OutCh        ;Prompt for new value
  450.     MOV    CX,2        ;Max of 2 digits in new value
  451.     MOV    DX,0        ;Intial new value
  452. GETDIG:
  453.     CALL    InCh        ;Get digit from user
  454.     MOV    AH,AL        ;Save
  455.     CALL    HEXCHK        ;Hex digit?
  456.     XCHG    AH,AL        ;Need original for echo
  457.     JC    NOHEX        ;If not, try special command
  458.     CALL    OutCh        ;Echo to console
  459.     MOV    DH,DL        ;Rotate new value
  460.     MOV    DL,AH        ;And include new digit
  461.     LOOP    GETDIG        ;At most 2 digits
  462. ;We have two digits, so all we will accept now is a command.
  463. WaitCh:
  464.     CALL    InCh        ;Get command character
  465. NOHEX:
  466.     CMP    AL,8        ;Backspace
  467.     JZ    BS
  468.     CMP    AL,7FH        ;RUBOUT
  469.     JZ    BS
  470.     CMP    AL,"-"        ;Back up to previous address
  471.     JZ    PREV
  472.     CMP    AL,13        ;All done with command?
  473.     JZ    EOL
  474.     CMP    AL," "        ;Go to next address
  475.     JZ    NEXT
  476. ;If we got here, character was invalid. Sound bell.
  477.     MOV    AL,7
  478.     CALL    OutCh
  479.     JCXZ    WaitCh        ;CX=0 means no more digits
  480.     jmp    GETDIG        ;Don't have 2 digits yet
  481.  
  482. BS:
  483.     CMP    CL,2        ;CX=2 means nothing typed yet
  484.     JZ    GETDIG        ;Can't back up over nothing
  485.     INC    CL        ;Accept one more character
  486.     MOV    DL,DH        ;Rotate out last digit
  487.     MOV    DH,CH        ;Zero this digit
  488.     CALL    BACKUP        ;Physical backspace
  489.     jmp    GETDIG        ;Get more digits
  490.  
  491. ;If new value has been entered, convert it to binary and
  492. ;put into memory. Always bump pointer to next location
  493.  
  494. STORE:
  495.     CMP    CL,2        ;CX=2 means nothing typed yet
  496.     JZ    NOSTO        ;So no new value to store
  497. ;Rotate DH left 4 bits to combine with DL and make a byte value
  498.     PUSH    CX
  499.     MOV    CL,4
  500.     SHL    DH,CL
  501.     POP    CX
  502.     OR    DL,DH        ;Hex is now converted to binary
  503.     MOV    ES:[DI],DL        ;Store new value
  504. NOSTO:
  505.     INC    DI        ;Prepare for next location
  506.     RET
  507.  
  508. EOL:
  509.     CALL    STORE        ;Enter the new value
  510.     JMP    CRLF        ;CR/LF and terminate
  511.  
  512. NEXT:
  513.     CALL    STORE        ;Enter new value
  514.     INC    CX        ;Leave a space plus two for
  515.     INC    CX        ;  each digit not entered
  516.     CALL    TAB
  517.     MOV    AX,DI        ;Next memory address
  518.     AND    AL,7        ;Check for 8-byte boundary
  519.     JNZ    GETBYTE        ;Take 8 per line
  520. NEWROW:
  521.     CALL    CRLF        ;Terminate line
  522.     JMP    GETROW        ;Print address on new line
  523.  
  524. PREV:
  525.     CALL    STORE        ;Enter the new value
  526. ;DI has been bumped to next byte. Drop it 2 to go to previous addr
  527.     DEC    DI
  528.     DEC    DI
  529.     jmp    NEWROW        ;Terminate line after backing up
  530.  
  531.  
  532. ;************************************************************
  533. ; "I" command
  534. ;Input from the specified port and display result
  535.  
  536. INPUT:
  537.     MOV    CX,4        ;Port may have 4 digits
  538.     CALL    GETHEX        ;Get port number in DX
  539.     in    al,dx        ;Variable port input
  540.     CALL    HEX        ;And display
  541.     JMP    CRLF
  542.  
  543.  
  544. ;************************************************************
  545. ; "O" command
  546. ;Output a value to specified port.
  547.  
  548. OUTPUT:
  549.     MOV    CX,4        ;Port may have 4 digits
  550.     CALL    GETHEX        ;Get port number
  551.     PUSH    DX        ;Save while we get data
  552.     MOV    CX,2        ;Byte output only
  553.     CALL    GETHEX        ;Get data to output
  554.     XCHG    AX,DX        ;Output data in AL
  555.     POP    DX        ;Port in DX
  556.     out    dx,al        ;Variable port output
  557.     RET
  558.  
  559. ;************************************************************
  560.  
  561. InitSeg    segment
  562.     assume    cs:DGroup,ds:Dgroup
  563.  
  564.     mov    ax,[DsSave]
  565.     mov    word ptr [DefDump+2],ax
  566.     mov    word ptr [DefDump],100H
  567. InitSeg    ends
  568.  
  569. end
  570.