home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / asmutil / wasm202.zip / CASE.ASM < prev    next >
Assembly Source File  |  1986-12-25  |  6KB  |  187 lines

  1.  
  2.  Title 'Wolfware Assembler Sample', 'Pascal String Functions'
  3.  
  4. ;===============================================;
  5. ;  Turbo Pascal String Functions Version 1.00   ;
  6. ;                                               ;
  7. ; This program contains three machine language  ;
  8. ; string routines for Turbo Pascal.  There is a ;
  9. ; routine for converting all the characters in  ;
  10. ; a string to lower-case, converting all the    ;
  11. ; characters to upper-case, and converting the  ;
  12. ; first character to upper and the remaining    ;
  13. ; characters to lower.                          ;
  14. ;                                               ;
  15. ; Assuming that the file is assembled to        ;
  16. ; CASE.COM, the routines should be declared in  ;
  17. ; a Pascal program in the following manner:     ;
  18. ;                                               ;
  19. ;   TYPE                                        ;
  20. ;     STR_TYP = STRING [255]; {any string type} ;
  21. ;                                               ;
  22. ;   FUNCTION LOWERCASE(S: STR_TYP): STR_TYP;    ;
  23. ;     EXTERNAL 'CASE.COM';                      ;
  24. ;   FUNCTION UPPERCASE(S: STR_TYP): STR_TYP;    ;
  25. ;     EXTERNAL LOWERCASE[2];                    ;
  26. ;   FUNCTION CAPITAL(S: STR_TYP): STR_TYP;      ;
  27. ;     EXTERNAL LOWERCASE[4];                    ;
  28. ;                                               ;
  29. ; Having done so, LOWERCASE, UPPERCASE, and     ;
  30. ; CAPITAL may be used as normal Pascal          ;
  31. ; functions.  Example:                          ;
  32. ;                                               ;
  33. ;   WRITE (LOWERCASE ('aBcD')); {writes 'abcd'} ;
  34. ;   WRITE (UPPERCASE ('aBcD')); {writes 'ABCD'} ;
  35. ;   WRITE (CAPITAL ('aBcD')); {writes 'Abcd'}   ;
  36. ;                                               ;
  37. ; All registers are saved, though the Turbo     ;
  38. ; Pascal manual says that only the registers    ;
  39. ; BP, CS, DS, and SS need to be saved.          ;
  40. ;===============================================;
  41.  
  42.  Proc Near
  43.  
  44. ;----- table of routines, each jumps to set a function code
  45. ;----- that is interpreted later for the individual routines
  46.  
  47.  Jmps Lowcase           ;lower-case
  48.  Jmps Uppcase           ;upper-case
  49.  Jmps Capital           ;capitalize
  50.  
  51. ;----- make the string lower-case
  52.  
  53. Lowcase
  54.  Push Ax
  55.  Mov Ax, 0              ;function code
  56.  Jmps Start
  57.  
  58. ;----- make the string upper-case
  59.  
  60. Uppcase
  61.  Push Ax
  62.  Mov Ax, 1              ;function code
  63.  Jmps Start
  64.  
  65. ;----- capitalize the string
  66.  
  67. Capital
  68.  Push Ax
  69.  Mov Ax, 2              ;function code
  70.  
  71. ;----- function code is loaded to AX, start execution
  72.  
  73. Start
  74.  Push Bx
  75.  Push Dx
  76.  Push Di
  77.  Push Si
  78.  Push Bp                ;save all modified regs
  79.  Mov Bp, Sp             ;set pointer into stack
  80.  
  81. ;----- set the location of the byte before the string in DI
  82. ;----- and the location of the byte after the string in SI
  83.  
  84.  Mov Di, 14             ;stack displacement to string parameter
  85.  Mov Bl, [Bp+Di]        ;get string length
  86.  Sub Bh, Bh             ;BX has length
  87.  Mov Si, Di
  88.  Add Si, Bx             ;pointer to end of string
  89.  
  90.  Mov Bl, 'a' - 'A'      ;translation value
  91.  
  92. ;----- branch for particular function based 
  93. ;----- on the function code set earlier
  94.  
  95.  Or Ax, Ax
  96.  Jz Lowcasedo           ;jump for lower-case
  97.  Dec Ax
  98.  Jz Uppcasedo           ;jump for upper-case
  99.  Dec Ax
  100.  Jz Capitaldo           ;jump for capitalize
  101.  Jmps Finish            ;invalid code (should not happen)
  102.  
  103. ;----- make all characters in the string lower-case
  104.  
  105. Lowcasedo
  106.  Mov Dx, 'AZ'           ;letter limits
  107.  Call Switchcase        ;switch the letters
  108.  Jmps Finish
  109.  
  110. ;----- make all characters in the string upper-case
  111.  
  112. Uppcasedo
  113.  Mov Dx, 'az'           ;letter limits
  114.  Neg Bl                 ;fix translation, lower to upper
  115.  Call Switchcase        ;switch the letters
  116.  Jmps Finish
  117.  
  118. ;----- make the first character upper-case and the remaining lower-case
  119.  
  120. Capitaldo
  121.  Cmp Si, Di             ;check if zero length string
  122.  Je Finish              ;jump if so, done
  123.  
  124.  Push Di
  125.  Push Si                ;save pointers for first letter
  126.  Mov Dx, 'AZ'           ;letter limits
  127.  Call Switchcase        ;switch the all letters to lower case
  128.  Pop Si
  129.  Pop Di
  130.  
  131.  Mov Si, Di
  132.  Inc Si                 ;make end pointer one byte past start
  133.  Mov Dx, 'az'           ;letter limits
  134.  Neg Bl                 ;fix translation, lower to upper
  135.  Call Switchcase        ;switch the first letter to upper-case
  136.  
  137. ;----- finished
  138.  
  139. Finish
  140.  Pop Bp
  141.  Pop Si
  142.  Pop Di
  143.  Pop Dx
  144.  Pop Bx
  145.  Pop Ax                 ;restore all regs
  146.  Ret                    ;return to main Pascal program
  147.  
  148. ;===============================================;
  149. ;                   Switchcase                  ;
  150. ; Switch the case of the letters from [BP+DI]   ;
  151. ; to [BP+SI] in the stack segment. DL should    ;
  152. ; have the lower limit of letters to change and ;
  153. ; DH the upper limit. BH should have the value  ;
  154. ; to add to make the switch.                    ;
  155. ;===============================================;
  156.  
  157. Switchcase Proc Near
  158.  
  159. ;----- check if done
  160.  
  161. Caseloop
  162.  Cmp Di, Si             ;check if at last byte
  163.  Je Casedone            ;jump if so, finished
  164.  
  165. ;----- load next byte
  166.  
  167.  Inc Di                 ;advance pointer
  168.  Mov Al, [Bp+Di]        ;get next character
  169.  Cmp Al, Dl             ;check lower limit
  170.  Jb Caseloop
  171.  Cmp Al, Dh             ;check upper limit
  172.  Ja Caseloop
  173.  
  174. ;----- switch byte
  175.  
  176.  Add Al, Bl             ;switch case
  177.  Mov [Bp+Di], Al        ;save result
  178.  Jmps Caseloop
  179.  
  180. ;----- finished
  181.  
  182. Casedone Ret            ;return
  183.  Endp                   ;Switchcase
  184.  
  185.  Endp                   ;main program
  186.  
  187.