home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0000 - 0009 / ibm0000-0009 / ibm0003.tar / ibm0003 / TPOWER53.ZIP / TPASM.ARC / TPASCIIZ.ASM < prev    next >
Encoding:
Assembly Source File  |  1989-07-10  |  7.0 KB  |  304 lines

  1. ;******************************************************
  2. ;           TPASCIIZ.ASM    5.07
  3. ;          ASCIIZ string manipulation
  4. ;     Copyright (c) TurboPower Software 1987.
  5. ; Portions copyright (c) Sunny Hill Software 1985, 1986
  6. ;     and used under license to    TurboPower Software
  7. ;         All rights reserved.
  8. ;******************************************************
  9.  
  10.     INCLUDE    TPCOMMON.ASM
  11.  
  12. ;******************************************************    Code
  13.  
  14. CODE    SEGMENT    BYTE PUBLIC
  15.  
  16.     ASSUME    CS:CODE
  17.  
  18.     PUBLIC    Search
  19.     PUBLIC    AscUpcase
  20.     PUBLIC    AscLocase
  21.     PUBLIC    CompAsc
  22.     PUBLIC    CompUCAsc
  23.  
  24.     EXTRN    UpCasePrim : FAR
  25.     EXTRN    LoCasePrim : FAR
  26.  
  27. Upcase    MACRO                ;UpCase    character in AL
  28.     PUSH   BX
  29.     CALL   UpCasePrim
  30.     POP    BX
  31.     ENDM
  32.  
  33. Locase    MACRO                ;LoCase    character in AL
  34.     PUSH   BX
  35.     CALL   LoCasePrim
  36.     POP    BX
  37.     ENDM
  38.  
  39. ;******************************************************    Search
  40.  
  41. ;  function Search(var Buffer; BufLength : Word;
  42. ;           var Match;  MatLength : Word) : Word; external;
  43. ;Search    through    Buffer for Match.
  44. ;BufLength is length of    range to search.
  45. ;MatLength is length of    string to match
  46. ;Returns number    of bytes searched to find St, FFFF if not found
  47.  
  48. ;equates for parameters:
  49. MatLength    EQU    WORD PTR [BP+6]
  50. Match        EQU    DWORD PTR [BP+8]
  51. BufLength    EQU    WORD PTR  [BP+0Ch]
  52. Buffer        EQU    DWORD PTR [BP+0Eh]
  53.  
  54. Search    PROC FAR
  55.  
  56.     StackFrameBP
  57.     PUSH    DS            ;Save DS
  58.     CLD                ;Go forward
  59.  
  60.     LES    DI,Buffer        ;ES:DI => Buffer
  61.     MOV    BX,DI            ;BX = Ofs(Buffer)
  62.  
  63.     MOV    CX,BufLength        ;CX = Length of    range to scan
  64.     MOV    DX,MatLength        ;DX = Length of    match string
  65.  
  66.     TEST    DX,DX            ;Length(Match) = 0?
  67.     JZ    Error            ;If so,    we're done
  68.  
  69.     LDS    SI,Match        ;DS:SI => Match    buffer
  70.     LODSB                ;AL = Match[1];    DS:SI => Match[2]
  71.     DEC    DX            ;DX = MatLength-1
  72.     SUB    CX,DX            ;CX = BufLength-(MatLength-1)
  73.     JBE    Error            ;Error if BufLength is less
  74.  
  75. ;Search    for first character in St
  76. Next:    REPNE    SCASB            ;Search    forward    for Match[1]
  77.     JNE    Error            ;Done if not found
  78.     TEST    DX,DX            ;If Length = 1 (DX = 0)    ...
  79.     JZ    Found            ; the "string" was found
  80.  
  81.     ;Search    for remainder of St
  82.  
  83.     PUSH    CX            ;Save CX
  84.     PUSH    DI            ;Save DI
  85.     PUSH    SI            ;Save SI
  86.  
  87.     MOV    CX,DX            ;CX = Length(St) - 1
  88.     REPE    CMPSB            ;Does rest of string match?
  89.  
  90.     POP    SI            ;Restore SI
  91.     POP    DI            ;Restore DI
  92.     POP    CX            ;Restore CX
  93.  
  94.     JNE    Next            ;Try again if no match
  95.  
  96. ;Calculate number of bytes searched and    return in St
  97. Found:    DEC    DI            ;DX = Offset where found
  98.     MOV    AX,DI            ;AX = Offset where found
  99.     SUB    AX,BX            ;Subtract starting offset
  100.     JMP    Short Done        ;Done
  101.  
  102. ;Match was not found
  103. Error:    XOR    AX,AX            ;Return
  104.     DEC    AX            ;Return    FFFF
  105.  
  106. Done:    POP    DS            ;Restore DS
  107.     ExitCode 10
  108.  
  109. Search    ENDP
  110.  
  111. ;******************************************************    AscUpcase
  112.  
  113. ; procedure AscUpcase{(var a, b    : asciiz)};
  114. ;  {-Uppercase the Asciiz in a,    returning b}
  115.  
  116. ;equates for parameters:
  117. A    EQU    DWORD PTR [BP+10]
  118. B    EQU    DWORD PTR [BP+6]
  119.  
  120. AscUpcase    PROC FAR
  121.  
  122.     StackFrameBP
  123.     PUSH    DS            ;Save DS
  124.     CLD                ;Go forward
  125.  
  126.     LES    DI,B            ;ES:DI => B
  127.     LDS    SI,A            ;DS:SI => A
  128.  
  129. AUPNext:
  130.     LODSB
  131.     OR    AL,AL            ;Termination of    string?
  132.     JZ    AUPDone
  133.     Upcase                ;convert to uppercase
  134.     STOSB                ;Store the converted character
  135.     JMP    AUPNext            ;Get the next character
  136.  
  137. AUPDone:
  138.     STOSB                ;Terminate output string
  139.     POP    DS            ;Restore DS
  140.     ExitCode 8
  141.  
  142. AscUpcase  ENDP
  143.  
  144. ;******************************************************    AscLocase
  145.  
  146. ;  procedure AscLocase{(var a, b : asciiz)};
  147. ;    {-Lowercase the Asciiz in a, returning b}
  148.  
  149. ;equates for parameters:
  150. A    EQU DWORD PTR [BP+10]
  151. B    EQU DWORD PTR [BP+6]
  152.  
  153. AscLocase  PROC    FAR
  154.  
  155.     StackFrameBP
  156.     PUSH    DS            ;Save DS
  157.     CLD                ;Go forward
  158.  
  159.     LES    DI,B            ;ES:DI => B
  160.     LDS    SI,A            ;DS:SI => A
  161.  
  162. ALONext:
  163.     LODSB
  164.     OR    AL,AL            ;Termination of    string?
  165.     JZ    ALODone
  166.     Locase                ;convert to lower case
  167.     STOSB                ;Store the converted character
  168.     JMP    ALONext            ;Get the next character
  169.  
  170. ALODone:
  171.     STOSB                ;Terminate output string
  172.  
  173.     POP    DS            ;Restore DS
  174.     ExitCode 8
  175.  
  176. AscLocase  ENDP
  177.  
  178. ;******************************************************    CompAsc
  179.  
  180. ;  function CompAsc {(var a1, a2 : Asciiz) : AscCompareType} ;
  181. ;    {-Return 0, 1, 2 if a1<a2,    a1=a2, or a1>a2}
  182.  
  183. CompAsc     PROC FAR
  184.  
  185.     StackFrameBP
  186.     PUSH    DS            ;Save DS
  187.     CLD                ;Go forward
  188.  
  189.     MOV    AL,0            ;look for null
  190.  
  191.     LES    DI,[BP+6]        ;ES:DI points to A2
  192.     MOV    BX,DI            ;store initial offset
  193.     MOV    CX,0FFFFh        ;check maximum length
  194.     REPNE    SCASB            ;scan while equal
  195.     SUB    DI,BX            ;get the number    of bytes scanned
  196.     MOV    DX,DI            ;lenasc(A2) in dx
  197.     DEC    DX            ;null doesn't count
  198.  
  199.     LES    DI,[BP+10]        ;ES:DI points to A1
  200.     MOV    BX,DI            ;store initial offset
  201.     MOV    CX,0FFFFh        ;check maximum length
  202.     REPNE    SCASB            ;scan while equal
  203.     SUB    DI,BX            ;get the number    of bytes scanned
  204.     MOV    CX,DI            ;lenasc(A1) in cx
  205.     DEC    CX            ;null doesn't count
  206.  
  207.     LES    DI,[BP+6]        ;ES:DI points to A2
  208.     LDS    SI,[BP+10]        ;DS:SI points to A1
  209.  
  210.     XOR    AX,AX            ;AX holds result
  211.  
  212.     CMP    CX,DX            ;Which string is longer?
  213.     JE    EqLen            ;Lengths equal
  214.     JB    Comp            ;Jump if A1 shorter than A2
  215.  
  216.     INC    AX            ;A1 longer than    A2
  217.     MOV    CX,DX            ;Shorter length    in CX
  218.  
  219. EqLen:    INC    AX            ;Equal or greater
  220.  
  221. Comp:    JCXZ    CDone            ;Done if either    is empty
  222.     REPE    CMPSB            ;Compare until no match    or CX =    0
  223.     JE    CDone            ;If Equal, result ready    based on length
  224.  
  225.     MOV    AL,2
  226.     JA    CDone            ;A1 Greater? Return 2
  227.     XOR    AX,AX            ;Else A1 Less, Return 0
  228.  
  229. CDone:    POP    DS            ;Restore DS
  230.     ExitCode 8
  231.  
  232. CompAsc         ENDP
  233.  
  234. ;******************************************************    CompUCAsc
  235.  
  236. ;  function CompUCAsc {(var a1,    a2 : Asciiz) : AscCompareType} ;
  237. ;    {-Return 0, 1, 2 if a1<a2,    a1=a2, or a1>a2}
  238. ;    {-Comparison is done in uppercase}
  239.  
  240. CompUCAsc    PROC FAR
  241.  
  242.     StackFrameBP
  243.     PUSH    DS            ;Save DS
  244.     CLD                ;Go forward
  245.  
  246.     MOV    AL,0            ;look for null
  247.  
  248.     LES    DI,[BP+6]        ;ES:DI points to A2
  249.     MOV    BX,DI            ;store initial offset
  250.     MOV    CX,0FFFFh        ;check maximum length
  251.     REPNE SCASB            ;scan while equal
  252.     SUB    DI,BX            ;get the number    of bytes scanned
  253.     MOV    DX,DI            ;lenasc(A2) in dx
  254.     DEC    DX            ;null doesn't count
  255.  
  256.     LES    DI,[BP+10]        ;ES:DI points to A1
  257.     MOV    BX,DI            ;store initial offset
  258.     MOV    CX,0FFFFh        ;check maximum length
  259.     REPNE SCASB            ;scan while equal
  260.     SUB    DI,BX            ;get the number    of bytes scanned
  261.     MOV    CX,DI            ;lenasc(A1) in cx
  262.     DEC    CX            ;null doesn't count
  263.  
  264.     LES    DI,[BP+6]        ;ES:DI points to A2
  265.     LDS    SI,[BP+10]        ;DS:SI points to A1
  266.  
  267.     XOR    BX,BX            ;BX holds result
  268.  
  269.     CMP    CX,DX            ;Which string is longer?
  270.     JE    UcEqLen            ;Lengths equal
  271.     JB    UcComp            ;Jump if A1 shorter than A2
  272.  
  273.     INC    BX            ;A1 longer than    A2
  274.     MOV    CX,DX            ;Shorter length    in CX
  275.  
  276. UcEqLen:INC    BX            ;Equal or greater
  277.  
  278. UcComp:    JCXZ    UcDone            ;Done if either    is empty
  279.  
  280. Start:    LODSB                ;S1[?] into AL
  281.     Upcase                ;convert to upper case
  282.     MOV    AH,ES:[DI]        ;S2[?] into AH
  283.     INC    DI            ;Point ES:DI to    next char in S2
  284.     XCHG    AH,AL            ;convert to upper case
  285.     Upcase
  286.     CMP    AH,AL            ;Compare until no match
  287.     LOOPE    Start
  288.  
  289.     JE    UcDone            ;If Equal, result ready    based on length
  290.  
  291.     MOV    BL,2
  292.     JA    UcDone            ;A1 Greater? Return 2
  293.     XOR    BX,BX            ;Else A1 Less, Return 0
  294.  
  295. UcDone:    MOV    AX,BX            ;Final result into AX
  296.     POP    DS            ;Restore DS
  297.     ExitCode 8
  298.  
  299. CompUCAsc    ENDP
  300.  
  301. CODE    ENDS
  302.  
  303.     END
  304.