home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TOOLS4.ZIP / RIGHTSTR.ASM < prev    next >
Encoding:
Assembly Source File  |  1988-07-19  |  2.7 KB  |  95 lines

  1. ;***********************************************************
  2. ;
  3. ; RightStr.ASM - A FAR PROC for use in a TP4 unit as
  4. ;
  5. ; function RightStr(_S : String; Number : Word) : String;
  6. ;
  7. ; returns all the characters starting at Number to the
  8. ; end of string.  A NULL string is returned if Number is
  9. ; greater than the length of _S, or if _S = '', or if
  10. ; Number = 0.
  11. ; ex.
  12. ;  the pascal line
  13. ;
  14. ;  S := RightStr('hello world',7);
  15. ;  the variable S now contains the value
  16. ;  'world'
  17. ;
  18. ;  Note: since this routine is written for use in a Turbo
  19. ;  Pascal 4 program, they do not preserve AX,BX,CX,DX.ES,
  20. ;  DI,SI.  Turbo Pascal does not require that we save these
  21. ;  registers, so to speed things up, we don't!  As always,
  22. ;  BP,DS are preserved.
  23. ;
  24. ;
  25. ;***********************************************************
  26.  
  27.           INCLUDE MACROS.ASM
  28.  
  29.  
  30. CODE      SEGMENT BYTE PUBLIC
  31.           ASSUME CS:CODE
  32.  
  33.           PUBLIC RightStr
  34.  
  35. ; The parameters: TP4 always push the FunctionRes address first.
  36. ; Then comes the _S : String, forllowed by Number : Word.
  37. ; We declare this as far for use in a unit
  38.  
  39. FunctionRes      EQU DWORD PTR [BP+0Ch]
  40. _S               EQU DWORD PTR [BP+08h]
  41. Number           EQU [BP+06h]
  42.  
  43. ; if this function is to be used in the interface section of a
  44. ; unit, then FAR is correct.  Otherwise, if you need or want
  45. ; to use this function differently, you must understand how
  46. ; TP4 decides what size pointer to call a routine with, as
  47. ; well as how to adjust the paraneters on the stack to
  48. ; correspond to the proper PROC declaration.  This will be
  49. ; covered shortly in another upload.
  50.  
  51. RightStr         PROC FAR
  52.        StandEntry
  53.        MOV       DX,DS
  54.  
  55.        StringOps _S,FunctionRes,FORWARD
  56.  
  57.        MOV       CX,Number       ; starting position in str
  58.        XOR       CH,CH           ; zero out high byte of CX
  59.  
  60.        JCXZ      ReturnNullStr   ; if CX = 0 then nothing to do
  61.  
  62.        LODSB                     ; get length of _S
  63.        CMP       AL,CL           ; compare it to Number
  64.        JB        ReturnNullStr   ; Number > Length(S) so ret ''
  65.  
  66.        SUB       AL,CL           ; sub Number from Length(_S)
  67.  
  68.        XOR       AH,AH           ; zero out high byte of AX
  69.        MOV       BX,CX           ; copy Length(S) to BX
  70.        DEC       BX              ; subtract 1
  71.  
  72.        ADD       SI,BX           ; add BX to offset of _S
  73.  
  74.        INC       AL
  75.  
  76.        MOV       CL,AL           ; copy length to CL
  77.  
  78.        STOSB                     ; store length of result
  79.  
  80.        REP       MOVSB           ; copy CL chars
  81.        JMP       SHORT ExitCode  ; all done
  82.  
  83. ReturnNullStr:
  84.        XOR       AL,AL
  85.        STOSB
  86.  
  87. ExitCode:
  88.        MOV       DS,DX
  89.        StandExit 06h
  90. RightStr         ENDP
  91.  
  92. CODE   ENDS
  93.  
  94.        END
  95.