home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / assemblr / asm / wasm / case.asm < prev    next >
Assembly Source File  |  1987-05-05  |  5KB  |  177 lines

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