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