home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_GEN / ASM32.ZIP / DATA.DOC < prev    next >
Text File  |  1994-01-16  |  32KB  |  1,029 lines

  1.  
  2. *******************************  DATA  *************************************
  3.  
  4. ASM32 data manipulation subroutines Copyright (C) 1993 Douglas Herr
  5. all rights reserved
  6.  
  7. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  8.  
  9. CHRDEL:      deletes a character from a string
  10.              The resulting space is closed by moving the remaining
  11.              characters forward
  12. Source:      chrdel.asm (strlen.asm)
  13.  
  14. Call with:   EBX pointing to an ASCIIZ string
  15.              EAX = offset from EBX to character to delete
  16. Returns:     ECX = new string length
  17. Uses:        ECX
  18. Example:     lea   ebx,string         ; EBX points to string
  19.              mov   eax,3              ; delete character at [EBX+3]
  20.              call  chrdel             ; delete the character, shorten string
  21.  
  22.  
  23. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  24.  
  25. COLORATTR:   calculate a color attribute for ASM32's text-mode subroutines.
  26.              Intended for 16-color text modes.
  27. Source:      coloratt.asm
  28. Call with:   AL = foreground color (0 - 15)
  29.              AH = background color (0 - 15)
  30. Returns:     AH = color attribute
  31. Uses:        AX
  32. Example:
  33.  
  34. include codeseg.inc
  35.         .
  36.         .
  37.         .
  38.         mov    al,hired            ; bright red
  39.         mov    ah,blue             ; blue
  40.         call   colorattr           ; this should get their attention
  41.         mov    warning,ah          ; save the attribute
  42.  
  43. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  44.  
  45. CSET:        centers a string in a fixed field
  46. Source:      cset.asm (strlen.asm)
  47.  
  48. Call with:   EDI pointing to address of field string
  49.              ESI pointing address of string to be centered in the field
  50.              Both strings must be zero-terminated (ASCIIZ).  The field
  51.              string may not contain any nul characters except for the
  52.              terminator.
  53. Returns:     CF = 0 if no error
  54.              CF = 1 if string was truncated to fit in the field
  55. Uses:        CF; all other flags and registers are saved
  56. Example:
  57.  
  58.              lea   edi,field             ; field string
  59.              lea   esi,source            ; string to be centered in field
  60.              call  cset
  61.  
  62.  
  63. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  64.  
  65. DAYNAME:     returns a pointer to an ASCII string for specified day
  66.              of the week
  67. Source:      dname.asm ($mname.asm)
  68.  
  69. Call with:   AX = day of week (1 - 7, Monday = 1)
  70. Returns:     [EBX] = pointer to day name string
  71.              ECX = length of new string
  72.              Note that the day name string is not zero-terminated
  73.              strndup may be used to copy the string to the near heap
  74. Uses:        EBX, ECX
  75. Example:     mov    ax,day
  76.              call   dayname
  77.  
  78.  
  79.  
  80. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  81.  
  82. FILL4, FILL4b: fill a buffer with specified 4-byte data
  83. Source:        fill4.asm
  84.  
  85. Call with:   ESI pointing to 4-byte data (integer, float or other)
  86.              EDI pointing to buffer
  87.              ECX = number of 4-byte data blocks to fill in buffer
  88.              Fill4b: EBX = byte increment between data blocks
  89. Returns:     nothing
  90. Uses:        nothing
  91. Example:
  92.  
  93. include codeseg.inc
  94.  
  95. extrn   fill4:near
  96.  
  97. ; data
  98. wonowon dd 101.      ; float4 value
  99. buffer  dd ?         ; program allocates buffer, stores address here
  100.  
  101. ; code
  102.         .
  103.         .
  104.         .
  105.         lea   edi,buffer
  106.         lea   esi,wonowon  ; fill every other 4-byte block in far segment
  107.         mov   ebx,8        ; skip 4 bytes between data blocks
  108.         mov   cx,25        ; do it 25 times
  109.         cal   fill4
  110.  
  111.  
  112.  
  113. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  114.  
  115. FILL8, FILL8b: fill a buffer with specified 8-byte data
  116. Source:        fill8.asm
  117.  
  118. Call with:   ESI pointing to 8-byte data (integer, float or other)
  119.              EDI pointing to buffer
  120.              ECX = number of 8-byte data blocks to fill in buffer
  121.              Fill8b: EBX = byte increment between data blocks
  122. Returns:     nothing
  123. Uses:        nothing
  124. Example:     see Fill4
  125.  
  126.  
  127.  
  128.  
  129. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  130.  
  131. FSTRISTR:    search for a string in a disk file, case insensetive
  132. Source:      fstristr.asm (strlen.asm, $strstr.asm, strnupr.asm)
  133.  
  134. FSTRSTR:     search for a string in a disk file, case sensetive
  135. Source:      fstrstr.asm (strlen.asm, $strstr.asm)
  136.  
  137. Call with:   BX = file handle, ESI points to string to find
  138.              The file must have been opened with read access.
  139. Returns:     if CF = 0, EAX is the offset of the string in the file
  140.              if CF = 1: string not found if EAX = -1
  141.                         error reading file if EAX = -2
  142. Uses:        EAX, flags
  143.  
  144. Example:
  145.  
  146. include codeseg.inc
  147.  
  148. extrn   fstrstr:near
  149.  
  150. ; data
  151. string   db 'A String In The File',0
  152. filename db 'anyold.fil',0
  153.  
  154. ; code
  155. yoursub proc    near
  156.         mov     edx,offset filename
  157.         mov     al,0               ; open the file with read access
  158.         call    $fopen
  159.         jc      short no_good
  160.         mov     bx,ax
  161.         mov     esi,offset string  ; ESI points to the string
  162.         call    fstrstr
  163.         jc      short no_good      ; returns with EAX = string offset
  164.  
  165.  
  166. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  167.  
  168. GETCMD:      isolates command line parameters
  169.              GetCMD assumes that parameters are separated by
  170.              one or more spaces
  171. Source:      getcmd.asm
  172.  
  173. Call with:   AL = parameter number (first command line parameter = 0)
  174. Returns:     EBX pointing to copy of command line parameter
  175.                  parameter copy is in low heap
  176.              ECX = length of parameter string
  177.              ECX = 0 if no command parameter(AL)
  178. Uses:        EBX, ECX, flags
  179. Example:
  180.  
  181. include codeseg.inc
  182.  
  183. extrn   getcmd:near
  184.  
  185. ; code
  186.        .
  187.        .
  188.        .
  189.        mov     al,0        ; get first parameter
  190.        call    getcmd      ; returns with EBX pointing to parameter
  191.                            ; and ECX = length of parameter
  192.        jecxz   no_parameters
  193.        
  194.  
  195.  
  196. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  197.  
  198. I2TOSTR:     convert an integer value to an ASCIIZ string
  199. Source:      i2tostr.asm (i4tostr.asm)
  200.  
  201. I4TOSTR:     convert a long integer value to an ASCIIZ string
  202. Source:      i4tostr.asm
  203.  
  204.              ASM32's TPrint, GPrint and other 'print' subroutines require
  205.              a string argument.  Numeric data must be converted to a string
  206.              before printing.
  207.  
  208. Call with:   [ESI] pointing to a buffer space; must be 12 bytes or bigger
  209.              (i2tostr) AX = integer value
  210.              (i4tostr) EAX = long integer value
  211.  
  212. Returns:     ASCIIZ string at [ESI]; numerals are right-justified
  213. Uses:        nothing; all registers and flags are saved
  214. Supports:    (i2tostr) signed 2-byte integers
  215.              (i4tostr) signed 4-byte integers
  216. Example:
  217.  
  218. include codeseg.inc
  219.  
  220. extrn  i2tostr:near
  221.  
  222. ; data
  223. nbuffer  db 12 dup(?)
  224.  
  225. ; code
  226.  
  227.        .
  228.        .
  229.        .
  230.        lea   esi,nbuffer
  231.        mov   ax,32750      ; integer value
  232.        call  i2tostr
  233.  
  234.  
  235.  
  236. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  237.  
  238. LSET:        left-justifies a string in a fixed field
  239. Source:      lset.asm (strlen.asm)
  240.  
  241. Call with:   EDI = address of field string
  242.              ESI = address of string to be justified in the field
  243.              Both strings must be zero-terminated (ASCIIZ).  The field
  244.              string may not contain any NUL characters except for the
  245.              terminator.
  246. Returns:     CF = 0 if no error
  247.              CF = 1 if string was truncated to fit in the field
  248. Uses:        CF
  249.              all other flags and registers saved
  250. Example:
  251.              lea   edi,field             ; field string
  252.              lea   esi,source            ; string to be justified in field
  253.              call  lset
  254.  
  255.  
  256. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  257.  
  258. LTRIM:       remove leading blanks from an ASCIIZ string
  259. Source:      ltrim.asm (strlen.asm)
  260.  
  261. Call with:   EBX pointing to string
  262. Returns:     ECX = new string length
  263. Uses:        ECX
  264. Example:     lea    ebx,string
  265.              call   ltrim
  266.  
  267.  
  268.  
  269.  
  270. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  271.  
  272. MAXI2, MAXI2b: find maximum value in a signed integer array
  273. Source:        maxi2.asm
  274.  
  275. MINI2, MINI2b: find minimum value in a signed integer array
  276. Source:        mini2.asm
  277.  
  278. MAXU2, MINU2b: find maximum value in an unsigned integer array
  279. Source:        maxu2.asm
  280.  
  281. MINU2, MINU2b: find minimum value in an unsigned integer array
  282. Source:        minu2.asm
  283.  
  284. Call with:   EDI pointing to the array
  285.              ECX = number of array elements
  286.              For max/min?2b, call with EBX = byte increment between
  287.              array elements.  Max/min?2 assume increment = 2.
  288. Returns:     EAX = array element number with maximum value
  289.              see example to calculate address of maximum value
  290.              if subroutine was called with ECX = 0, CF = 1
  291. Uses:        EAX, CF
  292. Example:
  293.  
  294. include codeseg.inc
  295.  
  296. extrn  maxi2:near
  297.  
  298. ; data
  299.  
  300. integers     dw 140 dup(0)
  301.  
  302. ; code
  303.              .     ; program establishes array values
  304.              .
  305.              .
  306.              lea    edi,integers
  307.              mov    ecx,140        ; search the entire array
  308.              call   maxi2
  309.              shl    eax,1          ; convert word offset to byte offsest
  310.              add    edi,eax        ; EDI points to the maximum value
  311.                                    ; With max/min?2b, the offset of the
  312.                                    ; value is EDI + (EAX * EBX).
  313.  
  314. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  315.  
  316. MAXI4, MAXI4b: find maximum value in a signed 4-byte integer array
  317. Source:        maxi4.asm
  318.  
  319. MINI4, MINI4b: find minimum value in a signed 4-byte integer array
  320. Source:        mini4.asm
  321.  
  322. MAXU4, MAXU4b: find maximum value in an unsigned 4-byte integer array
  323. Source:        maxu4.asm
  324.  
  325. MINU4, MINU4b: find minimum value in an unsigned 4-byte integer array
  326. Source:        minu4.asm
  327.  
  328. Call with:   EDI pointing to the array
  329.              ECX = number of array elements
  330.              For max/min?4b, call with EBX = byte increment between
  331.              array elements.  Max/min?4 assume increment = 4.
  332. Returns:     EAX = array element number with maximum value
  333.              see example to calculate address of maximum value.
  334.              if subroutine was called with ECX = 0, CF = 1
  335. Uses:        EAX, CF
  336. Example:
  337.  
  338. include codeseg.inc
  339.  
  340. extrn  maxi4:near
  341.  
  342. ; data
  343.  
  344. int4         dd 140 dup(0)
  345.  
  346. ; code
  347.              .     ; program establishes array values
  348.              .
  349.              .
  350.              lea    edi,int4
  351.              mov    ecx,140        ; search the entire array
  352.              call   maxi4
  353.              shl    eax,2          ; convert dword offset to byte offset
  354.              add    edi,eax        ; EDI points to the maximum value
  355.                                    ; With max/min?4b, the offset of the
  356.                                    ; value is EDI + (EAX * EBX).
  357.  
  358. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  359.  
  360. MEMCOPY:     copy data, checking for overlap if ES = DS
  361. Source:      memcopy.asm
  362.  
  363. Call with:   DS:[ESI] = source address
  364.              ES:[EDI] = destination address
  365.              ECX = bytes to move
  366. Returns:     nothing
  367. Uses:        nothing
  368.  
  369. Example:
  370.  
  371. extrn   memcopy:near
  372.  
  373. include dataseg.inc
  374.  
  375. farmem      dw ?            ; selector of far memory block
  376. source      dd ?            ; offset in far memory block
  377. destination dd ?            ; destination offset
  378.  
  379. @curseg ends
  380.  
  381. include codeseg.inc
  382.  
  383.         .
  384.         .
  385.         .
  386.         push   ds
  387.         push   es
  388.         mov    esi,source
  389.         mov    edi,destination
  390.         mov    ax,farmem
  391.         mov    ds,ax
  392.         mov    es,ax
  393.         call   memcopy
  394.         pop    es
  395.         pop    ds
  396.  
  397.  
  398.  
  399. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  400.  
  401. MONTHNAME:   returns a pointer to an ASCII string for specified month
  402. Source:      mname.asm ($mname.asm)
  403.  
  404. Call with:   AX = month (1 - 12, January = 1)
  405. Returns:     [EBX] = pointer to month name string
  406.              ECX = length of month string
  407.              Note that the month name string is not zero-terminated
  408.              strndup may be used to copy the string to the near heap
  409. Uses:        EBX, ECX
  410. Example:     mov    ax,month
  411.              call   monthname
  412.  
  413.  
  414. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  415.  
  416. PATH:        finds path strings in program enviornment
  417.              isolates each path in "PATH=" part of enviornment
  418. Source:      path.asm (strlen.asm, heap.asm)
  419.  
  420. Call with:   EAX = path number
  421. Returns:     EBX points to a copy of path(EAX), ECX = length of path.  The
  422.              copy of the path string is located in the low memory area;
  423.              when you are done using it, release the memory used by the
  424.              string with HFREE.
  425.              If ECX = 0, path(EAX) does not exist.  The first path in the
  426.              enviornment is path(0).
  427. Uses:        EBX, ECX, flags
  428. Example:     
  429.  
  430. include codeseg.inc
  431.  
  432. extrn   path:proc
  433.  
  434. ; code
  435.          .
  436.          .
  437.          .
  438.         xor     eax,eax      ; start with the first path
  439. find_path:
  440.         call    path
  441.         jecxz   no_path      ; exit if no more paths, or else EBX points
  442.                              ; to a copy of the first path in the enviorment
  443.                              ;  strndup).
  444.         inc     eax          ; look for next path
  445.         jmp     find_path
  446.  
  447. no_path:
  448.          .
  449.          .
  450.  
  451. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  452.  
  453. RANDOM:      generates a near-random number
  454. Source:      random.asm
  455.  
  456. Call with:   no parameters
  457. Returns:     AX = near-random number between 0 and 65535
  458.              you may notice repeating patterns every few thousand calls
  459.              to Random.
  460. Uses:        AX
  461. Example:     call   random
  462.  
  463.  
  464.  
  465. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  466.  
  467. RSET:        right-justifies a string in a fixed field
  468. Source:      rset.asm (strlen.asm)
  469.  
  470. Call with:   EDI = address of field string
  471.              ESI = address of string to be justified in the field
  472.              Both strings must be zero-terminated (ASCIIZ).  The field
  473.              string may not contain any nul characters except for the
  474.              terminator.
  475. Returns:     CF = 0 if no error
  476.              CF = 1 if string was truncated to fit in the field
  477. Uses:        CF; all other flags and registers saved
  478. Example:
  479.              lea   edi,field             ; field string
  480.              lea   esi,source            ; string to be justified in field
  481.              call  rset
  482.  
  483.  
  484. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  485.  
  486. RTRIM:       removes trailing blanks from an ASCIIZ string
  487. Source:      rtrim.asm (strlen.asm)
  488.  
  489. Call with:   EBX pointing to string
  490. Returns:     ECX = new string length
  491. Uses:        ECX
  492. Example:     lea    ebx,string
  493.              call   rtrim
  494.  
  495.  
  496.  
  497. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  498.  
  499. SORTI4HI:    sort an array of 4-byte signed integers, highest first
  500. Source:      sorti4hi.asm
  501.  
  502. SORTI4LO:    sort an array of 4-byte signed integers, lowest first
  503. Source:      sorti4lo.asm
  504.  
  505. Call with:   EDI = address of first element of array to sort
  506.              ECX = number of array elements; assumes ECX < 1 Gigabyte
  507. Returns:     nothing
  508. Uses:        nothing; all registers and flags are saved
  509. Example:
  510.  
  511. extrn   sorti4hi:near
  512.  
  513. include dataseg.inc
  514. i4data  dd 1500 dup(0)
  515. @curseg ends
  516.  
  517. include codeseg.inc
  518.  
  519.              .      ; program establishes data values
  520.              .
  521.              .
  522.         lea    edi,i4data
  523.         mov    ecx,1500
  524.         call   sorti4hi
  525.  
  526.  
  527.  
  528. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  529.  
  530. STRTOI4:     converts an ASCII string to an integer value
  531. STRNTOI4:    converts n bytes of an ASCII string to an integer value
  532. Source:      strtoi4.asm
  533.  
  534. Call with:   ESI = address of string
  535.              strntoi4 only: ECX = number of bytes to read
  536. Returns:     EAX = integer value
  537.              strtoi4:  ESI points to character past terminating byte
  538.              strntoi4: if ECX = 0, ESI points to next character
  539.                        if ECX <> 0, ESI points to character past
  540.                        terminating byte
  541.              BL = error code
  542.                 bit 0 if set = CR read before reading any numeric characters
  543.                 bit 1 if set = CR was the terminating character
  544.                 bit 6 if set = overflow; result in EAX is unusable
  545.                 bit 7 if set = result is unsigned; result is unusable if the
  546.                                value represented by the string was negative
  547. Uses:        EAX, EBX, ECX, ESI, flags
  548. Example:
  549.  
  550. include codeseg.inc
  551.  
  552. ; data
  553.  
  554. number_string  db '1234567',0
  555.  
  556. ; code
  557.         .
  558.         .
  559.         .
  560.  
  561.         lea    esi,number_string   ; point to '1234567'
  562.         mov    ecx,7               ; 7-byte field
  563.         call   strntoi4            ; return result as an integer in EAX
  564.  
  565.  
  566.  
  567.  
  568. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  569.  
  570. STRCAT:      catenates (combines) two ASCIIZ strings
  571. Source:      strcat.asm (strdup.asm, strlen.asm, heap.asm)
  572.  
  573. Call with:   ESI = address of first string
  574.              EBX = address of second string
  575. Returns:     if CF = 0, EBX = address of combined ASCIIZ string
  576.              if CF = 1, insufficient memory available in low heap
  577. Uses:        EBX, ECX, CF
  578. Example:
  579.  
  580. include codeseg.inc
  581.  
  582. extrn   strcat:near
  583.  
  584. ; data
  585. string0 db 'this string goes first',0
  586. string1 db ' this one is added at the end of the first',0
  587.  
  588. ; code
  589.        .
  590.        .
  591.        .
  592.        lea    esi,string0       ; address of first string
  593.        lea    ebx,string1       ; address of second string
  594.        call   strcat            ; result returned at EBX
  595.        jc     heap_is_full      ; original strings are undisturbed
  596.  
  597.  
  598.  
  599. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  600.  
  601. STRCHR:      search an ASCIIZ string for a specified character
  602. STRNCHR:     search n bytes of an ASCII string for a specified character
  603. Source:      strchr.asm (strlen.asm)
  604.  
  605. Call with:   EBX = pointer to ASCIIZ string
  606.              AL = character to find
  607.              ECX = number of bytes to search (strnchr only)
  608. Returns:     ECX = string length
  609.              if CF = 0, EAX is the offset from EBX to matching character
  610.              in the source string
  611.              if CF = 1, no matching character found
  612. Uses:        ECX, EAX, CF
  613.  
  614. Example on next page
  615.  
  616. ; use STRNCHR to determine if a key pressed was a legal key
  617. include codeseg.inc
  618.  
  619. extrn   strnchr:proc, getkey:proc, toupper:proc
  620.  
  621. ; data
  622.  
  623. valid_string db 'ABC123',27      ; keys 1,2,3,A,B,C and Esc
  624. valid_len    equ $-valid_string  ; number of valid keys
  625.  
  626. dispatch_table label word
  627.         dd akey, bkey, ckey, onekey, twokey, threekey, esckey
  628.  
  629. ; code
  630.         .
  631.         .
  632.         .
  633. get_another:
  634.         lea   ebx,valid_string ; EBX points to a string of valid keys
  635.         call  getkey           ; keycode returned in EAX
  636.         shr   ah,1             ; test for extended keycode
  637.         jc    get_another      ; I'm not interested in extended keycodes today
  638.  
  639.         call  toupper          ; convert keycode to upper case
  640.         mov   ecx,valid_len
  641.         call  strnchr
  642.         jc    get_another      ; CF = 1 if key pressed is not among the
  643.                                ; keys in the validation string
  644.         mov   ebx,eax
  645.         shl   ebx,1            ; convert byte offset to word offset
  646.         jmp   dispatch_table[ebx]
  647.  
  648. akey:   .
  649.         .
  650.         .
  651.  
  652. bkey:   .
  653.         .
  654.         .
  655.  
  656. ckey:   .
  657.         .
  658.         .
  659.  
  660. ; etc
  661.  
  662.  
  663.  
  664. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  665.  
  666. STRCPY:      copy an ASCIIZ string to existing buffer
  667. Source:      strcpy.asm (strlen.asm, strncpy.asm)
  668.  
  669. Call with:   ES:[BX] pointing to ASCIIZ string
  670.              DS:[SI] pointing to destination buffer
  671.              STRCPY assumes that the buffer is long enough to hold the
  672.              entire string.  The string's terminating NUL byte is not
  673.              copied to the buffer.
  674. Returns:     CX = string length
  675. Uses:        CX
  676. Example:
  677.  
  678. include asm.inc
  679.  
  680. extrn   strcpy:proc
  681.  
  682. .fardata
  683. fstring        db 'a far string',0
  684.  
  685. .data
  686. string_buffer  db 128 dup (?)
  687.  
  688. .code
  689.        .
  690.        .
  691.        .
  692. ; copy far string to DGROUP for convenient manipulation
  693.        mov    ax,@fardata
  694.        mov    es,ax
  695.        assume es:@fardata
  696.        mov    bx,offset @fardata:fstring
  697.        call   strcpy
  698.  
  699.  
  700. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  701.  
  702. STRNCPY:     copy CX bytes to an existing buffer
  703. Source:      strncpy.asm
  704.  
  705. Call with:   ES:[BX] pointing to ASCII string
  706.              DS:[SI] pointing to destination buffer
  707.              CX = number of bytes to copy
  708.              STRNCPY assumes that the buffer is long enough to hold the
  709.              entire string
  710. Returns:     nothing
  711. Uses:        nothing; all registers and flags are saved
  712. Example:
  713.  
  714. ; I want to copy a command line parameter to DGROUP
  715.  
  716. include asm.inc
  717. extrn   getcmd:proc
  718. extrn   strncpy:proc
  719.  
  720. .data
  721. extrn   pspseg:word             ; PSP segment address was saved by STARTUP
  722. string_buffer   db 128 dup (?)
  723.  
  724. .code
  725.        .
  726.        .
  727.        .
  728.        mov      es,pspseg
  729.        xor      ax,ax           ; first command line parameter
  730.        call     getcmd          ; returns parameter at ES:[BX], length as CX
  731.        jcxz     no_parameters
  732.        lea      si,string_buffer
  733.        call     strncpy
  734.  
  735. ; make it zero-terminated
  736.        add      si,cx
  737.        mov      byte ptr [si],0
  738.  
  739.  
  740. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  741.  
  742. STRINS:      inserts string1 in string0 at specified offset.
  743.              Creates new string in low heap; first part of new string
  744.              is n bytes of string0; middle of new string is string1; end
  745.              of new string is remainder of string0.
  746. Source:      strins.asm (strlen.asm, heap.asm)
  747.  
  748. Call with:   ESI pointing to string0
  749.              EBX pointing to string1
  750.              EAX = offset in string0 to insert string1
  751. Returns:     if CF = 1, insufficient memory in hear heap
  752.              if CF = 0, EBX points to new string
  753. Uses:        EBX, CF
  754. Example:
  755.  
  756. include codeseg.inc
  757.  
  758. extrn  strins:near
  759.  
  760. ; data
  761. string0 db '1234567890',0
  762. string1 db 'abcdefghij',0
  763.  
  764. ; code
  765.        .
  766.        .
  767.        .
  768.        lea    esi,string0       ; address of first string
  769.        lea    ebx,string1       ; address of second string
  770.        mov    eax,3             ; string1 inserted after '123'
  771.        call   strins            ; result returned at EBX
  772.        jc     heap_is_full      ; original strings are undisturbed
  773.  
  774.  
  775.  
  776. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  777.  
  778. STRDUP:      duplicates an ASCIIZ string
  779. Source:      strdup.asm (strlen.asm)
  780.  
  781. STRNDUP:     duplicates n bytes of a string
  782. Source:      strdup.asm
  783.  
  784. Call with:   EBX = address of source string
  785.              (strndup) ECX = number of bytes to duplicate
  786.  
  787.              strdup requires an ASCIIZ string; strndup duplicates ECX
  788.              characters at [EBX] whether zero-terminated or not.  The
  789.              duplicate created by strdup or strndup will be an ASCIIZ
  790.              string.
  791.  
  792. Returns:     if CF = 0, EBX = address of string copy in low memory
  793.                         ECX = string length
  794.              if CF = 1, insufficient memory in low memory
  795. Uses:        EBX, ECX, flags
  796. Example:
  797.              lea  ebx,source         ; EBX = source address
  798.              call strdup
  799.              jc   oops               ; not enough memory if CF = 1
  800.                                      ; otherwise, EBX = address
  801.                                      ; of string copy
  802.  
  803.  
  804. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  805.              
  806. STRIPCHR:    remove all occurances of a character from an ASCIIZ string.
  807. Source:      stripchr.asm (strlen.asm)
  808.  
  809. Call with:   EBX = string address
  810.              AL = character to remove from the string
  811. Returns:     ECX = new string length
  812. Uses:        ECX
  813. Example:     lea    ebx,string        ; EBX -> string
  814.              mov    al,'$'            ; remove "$" character from string
  815.              call   stripchr
  816.  
  817.  
  818. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  819.  
  820. STRLEN:      finds length of an ASCIIZ string
  821. Source:      strlen.asm
  822.  
  823. Call with:   EBX = address of the string
  824. Returns:     ECX = length of string excluding the terminating NUL
  825. Uses:        ECX
  826. Example:     lea   ebx,string
  827.              call  strlen
  828.  
  829.  
  830. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  831.  
  832. STRLWR:      changes upper-case characters in a string to lower case
  833. Source:      strlwr.asm
  834.  
  835. STRNLWR:     changes a string of known length to lower case
  836. Source:      strnlwr.asm
  837.  
  838. Call with:   EBX = address of an ASCIIZ string
  839.              ECX = number of bytes (strnlwr only)
  840. Returns:     nothing
  841. Uses:        nothing
  842. Example:     lea    ebx,string
  843.              call   strlwr
  844.  
  845.  
  846.  
  847. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  848.  
  849. STRRCHR:     find the last byte in a string matching AL
  850. STRNRCHR:    find the last byte in n bytes matching AL
  851. Source:      strrchr.asm (strlen.asm)
  852.  
  853. Call with:   EBX pointing to the first character of the string
  854.              AL = byte to find
  855.              (strnrchr only) ECX = number of bytes to search
  856. Returns:     if CF = 1, no match
  857.              if CF = 0, EAX = offset from EBX of the last matching byte
  858. Uses:        EAX, CF; all other flags and registers are saved
  859. Example:
  860.  
  861. include model.inc
  862.  
  863. extrn   strrchr:near
  864.  
  865. dataseg.inc
  866. string  db 'my old computer was a real slug',0
  867. @curseg ends
  868.  
  869. include codeseg.inc
  870.         .
  871.         .
  872.         .
  873.  
  874.         mov   al,'w'         ; look for the lower-case "w"
  875.         lea   ebx,string
  876.         call  strrchr
  877.         jc    oops           ; cut outta here if not in the string
  878.                              ; else go on
  879.  
  880.  
  881.  
  882. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  883.  
  884. STRREV:      reverses all characters in a string
  885. STRNREV:     reverses n characters in a string
  886. Source:      strrev.asm (strlen.asm)
  887.  
  888. Call with:   EBX pointing to the first character of the string
  889.              ECX = number of bytes in string to reverse (strnrev only)
  890. Returns:     ECX = string length
  891. Uses:        ECX; all other registers and flags saved
  892. Example:     lea   ebx,string         ; EBX points to ASCIIZ string
  893.              call  strrev             ; also returns ECX = string length
  894.  
  895.  
  896.  
  897. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  898.  
  899. STRSPACE:    creates an ASCIIZ string filled with the space character,
  900.              terminated with NUL
  901. Source:      strspace.asm (heap.asm)
  902.  
  903. Call with:   EAX = string length (not including terminating NUL)
  904. Returns:     if CF = 1, insufficient memory in near heap
  905.              if CF = 0, EBX points to the new string
  906.                         ECX = string length (should be same as EAX)
  907. Uses:        ECX, EBX, CF
  908. Example:     mov   eax,14              ; make a new string 14 characters long
  909.              call  strspace
  910.              jc    short oops          ; not enough memory if CF = 1
  911.              mov   string14,ebx        ; else save pointer to string
  912.  
  913.  
  914. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  915.  
  916. STRSET:      sets all bytes of an ASCIIZ string to a specified character
  917. STRNSET:     sets n bytes of an ASCIIZ string to a specified character
  918. Source:      strset.asm (strlen.asm)
  919.  
  920. Call with:   EBX pointing to a valid ASCIIZ string
  921.              AL = character
  922.              ECX = number of bytes to set (strnset only)
  923. Returns:     ECX = string length
  924. Uses:        ECX
  925. Example:     lea   ebx,string          ; EBX points to an ASCIIZ string
  926.              mov   al,'*'
  927.              call  strset
  928.  
  929.  
  930.  
  931.  
  932. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  933.  
  934. STRSTR:      finds the first match with a target string in a source string
  935.              case sensetive
  936. Source:      strstr.asm ($strstr.asm, strlen.asm)
  937.  
  938. STRISTR:     finds the first match with a target string in a source string
  939.              case insensetive
  940. Source:      stristr.asm (strstr.asm, strdup.asm, strupr.asm, heap.asm)
  941.  
  942. STRRSTR:     finds the last match with a target string in a source string
  943.              case sensetive
  944. Source:      strrstr.asm (strrev.asm, $strstr.asm)
  945.  
  946. Call with:   EDI pointing to source string, ESI pointing to
  947.              target string.
  948. Returns:     if CF = 0, EAX = offset of target in source string.
  949.              if CF = 1, no match
  950.  
  951. Uses:        EAX, CF; all other flags and registers are saved
  952. Example:
  953.  
  954. include model.inc
  955.  
  956. include dataseg.inc
  957.  
  958. string     db 'Monday',0
  959. substring  db 'day',0
  960.  
  961. @curseg ends
  962.  
  963. include codeseg.inc
  964.         .
  965.         .
  966.         .
  967.         lea   edi,string       ; source = 'monday',0
  968.         lea   esi,substring    ; target = 'day',0
  969.                                ; find the offset of 'day' in 'Monday'
  970.         call  strstr           ; in this example, strstr returns EAX = 3
  971.  
  972.  
  973. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  974.  
  975. STRUPR:      changes lower-case characters in an ASCIIZ string to upper case
  976. Source:      strupr.asm
  977.  
  978. STRNUPR:     changes lower-case characters in an n-length string to upper case
  979. Source:      strnupr.asm
  980.  
  981. Call with:   EBX pointing to string
  982.              (strnupr only) ECX = number of bytes in string
  983. Returns:     nothing
  984. Uses:        nothing
  985. Example:     mov    ebx,offset string
  986.              mov    ecx,bytes
  987.              call   strnupr
  988.  
  989.  
  990.  
  991. ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
  992.  
  993. SWAPB:       swaps ECX bytes at DS:[ESI] with the bytes at ES:[EDI]
  994. Source:      swapb.asm
  995.  
  996. Call with:   ESI pointing to one of the data areas, EDI pointing
  997.              to the other
  998.              ECX = number of bytes to swap
  999. Returns:     nothing
  1000. Uses:        nothing; all registers and flags are saved
  1001. Example:
  1002.  
  1003. include model.inc
  1004.  
  1005. extrn   swapb:near
  1006.  
  1007. include dataseg.inc
  1008.  
  1009. string1 db 'this is string 1',0
  1010. string2 db 'this is string 2',0
  1011.  
  1012. strings dw string1, string2         ; addresses of the strings
  1013.                                     ; this trivial example will swap
  1014.                                     ; the string pointers
  1015. @curseg ends
  1016.  
  1017. include codeseg.inc
  1018.  
  1019. public  stringswap
  1020. stringswap   proc near
  1021.          .
  1022.          .
  1023.          .
  1024.          lea   esi,strings
  1025.          mov   edi,esi
  1026.          mov   eax,2                 ; each string pointer is 2 bytes
  1027.          add   edi,eax               ; point to 2nd pointer
  1028.          call  swapb
  1029.