string.asm 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. ; $Id$
  2. ; MegaZeux
  3. ;
  4. ; Copyright (C) 1996 Greg Janson
  5. ; Copyright (C) 1998 Matthew D. Williams - dbwilli@scsn.net
  6. ;
  7. ; This program is free software; you can redistribute it and/or
  8. ; modify it under the terms of the GNU General Public License as
  9. ; published by the Free Software Foundation; either version 2 of
  10. ; the License, or (at your option) any later version.
  11. ;
  12. ; This program is distributed in the hope that it will be useful,
  13. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ; General Public License for more details.
  16. ;
  17. ; You should have received a copy of the GNU General Public License
  18. ; along with this program; if not, write to the Free Software
  19. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. ;
  21. ; STRING.ASM- Simple functions to work with strings, including
  22. ; color strings for use with color_string in GRAPHICS.ASM.
  23. ;
  24. Ideal
  25. include "string.inc"
  26. p186
  27. JUMPS
  28. include "model.inc"
  29. Codeseg
  30. ;
  31. ; Function- str_len_color
  32. ;
  33. ; Reasonably optimized. (186)
  34. ;
  35. ; Get length of a color string- Don't count ~, @, or their following bytes,
  36. ; unless the following byte is itself a ~ or @.
  37. ; If a ~ or @ is followed by \0, end string count w/o counting either byte.
  38. ; These strings are the strings used with color_string in GRAPHICS.ASM.
  39. ;
  40. ; Arguments: string (char array)
  41. ;
  42. ; Returns: Length of string (in AX)
  43. ;
  44. proc str_len_color far
  45. arg string:dword
  46. push cx si ds
  47. lds si,[string]
  48. xor cx,cx ; Length count
  49. @@loop:
  50. lodsb ; Get char
  51. cmp al,0 ; EOS?
  52. je @@end ; YEP.
  53. cmp al,'~' ; Check for special characters
  54. je @@spec
  55. cmp al,'@'
  56. je @@spec
  57. cmp al,5
  58. je @@tab
  59. @@count:
  60. inc cx ; Size up by one
  61. jmp @@loop
  62. @@spec:
  63. lodsb ; Get next character
  64. cmp al,0 ; EOS?
  65. je @@end ; Yep.
  66. cmp al,'0'
  67. jb @@count
  68. cmp al,'9'
  69. jbe @@loop
  70. cmp al,'A'
  71. jb @@count
  72. cmp al,'F'
  73. jbe @@loop
  74. cmp al,'a'
  75. jb @@count
  76. cmp al,'f'
  77. jbe @@loop
  78. jmp @@count ; Blah.
  79. @@tab:
  80. add cx,5
  81. jmp @@loop
  82. @@end:
  83. mov ax,cx ; Return value
  84. pop ds si cx
  85. ret
  86. endp str_len_color
  87. ;
  88. ; Function- str_len
  89. ;
  90. ; Reasonably optimized. (186)
  91. ;
  92. ; Gets and returns the length of a normal string, until the first NUL.
  93. ;
  94. ; Arguments: string (char array)
  95. ;
  96. ; Returns: Length of string (in AX)
  97. ;
  98. proc str_len far
  99. arg string:dword
  100. push cx di es
  101. cld
  102. les di,[string]
  103. mov cx,65535 ; Search "forever"
  104. xor al,al
  105. repne scasb ; Find a 0
  106. flipflag cx,65535 ; Get length
  107. mov ax,cx
  108. dec ax ; Decrease, 'cause CX is -- even if 0 is found immed.
  109. pop es di cx
  110. ret
  111. endp str_len
  112. ;
  113. ; Function- str_cpy
  114. ;
  115. ; Reasonably optimized. (186)
  116. ;
  117. ; Copies one string to another.
  118. ;
  119. ; Arguments: destination (char array)
  120. ; source (char array)
  121. ;
  122. proc str_cpy far
  123. arg string_dest:dword,string_source:dword
  124. push ax cx si di es ds
  125. les di,[string_source]
  126. lds si,[string_source]
  127. cld
  128. mov cx,65535 ; Search "forever"
  129. xor al,al
  130. repne scasb ; Find a 0
  131. flipflag cx,65535 ; Get length
  132. les di,[string_dest]
  133. rep movsb ; Copy CX bytes
  134. pop ds es di si cx ax
  135. ret
  136. endp str_cpy
  137. ;
  138. ; Function- mem_cpy
  139. ;
  140. ; Reasonably optimized. (186)
  141. ;
  142. ; Copies one section of memory to another.
  143. ;
  144. ; Arguments: destination (char array)
  145. ; source (char array)
  146. ; length (word)
  147. ;
  148. ; Does it by words. No attempt to word-align is made- All large transfers
  149. ; (robots, screens) should already BE word aligned.
  150. ;
  151. proc mem_cpy far
  152. arg mem_dest:dword,mem_source:dword,len:word
  153. push ax cx si di es ds
  154. mov cx,[len]
  155. cld
  156. les di,[mem_dest]
  157. lds si,[mem_source]
  158. ; Half length and check for an extra byte (in carry flag)
  159. shr cx,1
  160. lahf
  161. rep movsw ; Copy CX words
  162. test ah,1
  163. jz @@end
  164. movsb ; Extra byte
  165. @@end:
  166. pop ds es di si cx ax
  167. ret
  168. endp mem_cpy
  169. ;
  170. ; Function- str_cat
  171. ;
  172. ; Optimization note- Could be optimized more at the expense of space by
  173. ; inlining the str_len functions.
  174. ;
  175. ; Copies one string onto the end of another.
  176. ;
  177. ; Arguments: destination (char array)
  178. ; source (char array)
  179. ;
  180. proc str_cat far
  181. arg string_dest:dword,string_source:dword
  182. push ax si di ds es cx
  183. les di,[string_dest]
  184. lds si,[string_source]
  185. call str_len,di,es ;Also does a CLD
  186. add di,ax ; Add length to dest.
  187. call str_len,si,ds
  188. mov cx,ax ; Use as count
  189. inc cx ; Copy 0 as well
  190. rep movsb ; Copy string
  191. pop cx es ds di si ax
  192. ret
  193. endp str_cat
  194. ;
  195. ; Function- mem_mov
  196. ;
  197. ; Reasonably optimized. (186)
  198. ;
  199. ; Copies one section of memory to another. The sections of memory can
  200. ; overlap and still be handled properly.
  201. ;
  202. ; Arguments: destination (char array)
  203. ; source (char array)
  204. ; length (word)
  205. ; code bit (byte) 0 if dest is lower in mem, 1 if dest is higher
  206. ;
  207. ; Done in words. No attempt to word-align is made, as the major use of this
  208. ; (robot re-allocation) should already be aligned.
  209. ;
  210. proc mem_mov far
  211. arg mem_dest:dword,mem_source:dword,len:word,code_bit:byte
  212. push ax cx si di es ds
  213. mov cx,[len]
  214. les di,[mem_dest]
  215. lds si,[mem_source]
  216. cmp [code_bit],1
  217. je @@mem_dest_high
  218. ; This code is fine if mem_dest is below mem_source in memory
  219. ; Get number of WORDS
  220. shr cx,1
  221. lahf
  222. cld
  223. rep movsw ; Copy CX words
  224. test ah,1
  225. jz @@end
  226. movsb
  227. @@end:
  228. pop ds es di si cx ax
  229. ret
  230. ; This code is for when mem_dest is higher in memory
  231. @@mem_dest_high:
  232. std
  233. dec cx
  234. add di,cx
  235. add si,cx
  236. inc cx
  237. rep movsb ; Copy CX bytes, backwards
  238. cld
  239. pop ds es di si cx ax
  240. ret
  241. endp mem_mov
  242. ;
  243. ; Function- mem_xor
  244. ;
  245. ; Reasonably optimized. (186)
  246. ;
  247. ; XOR a section of memory with a given code.
  248. ;
  249. ; Arguments: memory (char array)
  250. ; length (word)
  251. ; xor code (byte)
  252. ;
  253. proc mem_xor far
  254. arg mem:dword,len:word,xor_with:byte
  255. push ax cx si di es ds
  256. mov cx,[len]
  257. cld
  258. les di,[mem]
  259. lds si,[mem]
  260. mov ah,[xor_with]
  261. @@loop:
  262. lodsb ; Load a byte...
  263. xor al,ah ; ...XOR it...
  264. stosb ; ...Store it...
  265. loop @@loop ; ...and loop!
  266. pop ds es di si cx ax
  267. ret
  268. endp mem_xor
  269. ;
  270. ; Function- str_cmp
  271. ;
  272. ; Reasonably optimized (186)
  273. ;
  274. ; Compare two strings. Returns 0 if equal, non-0 if not equal
  275. ;
  276. ; Arguments: two strings
  277. ;
  278. ;proc _fstricmp far
  279. ;arg string_dest:dword,string_source:dword
  280. ;
  281. ; push bx si di es ds
  282. ;
  283. ; les di,[string_dest]
  284. ; lds si,[string_source]
  285. ;
  286. ; cld
  287. ;
  288. ;@@scnloop:
  289. ; lodsb
  290. ; mov bl,[es:di]
  291. ; inc di
  292. ; cmp al,'A'
  293. ; jb @@ok1
  294. ; cmp al,'Z'
  295. ; ja @@ok1
  296. ; xor al,32
  297. ;@@ok1:
  298. ; cmp bl,'A'
  299. ; jb @@ok2
  300. ; cmp bl,'Z'
  301. ; ja @@ok2
  302. ; xor bl,32
  303. ;@@ok2:
  304. ; cmp al,bl
  305. ; jne @@bad
  306. ; or al,al
  307. ; jz @@good
  308. ; jmp @@scnloop
  309. ;
  310. ;@@bad:
  311. ;
  312. ; mov ax,0FFFFh
  313. ; pop ds es di si bx
  314. ;
  315. ; ret
  316. ;
  317. ;@@good:
  318. ;
  319. ; xor ax,ax
  320. ; pop ds es di si
  321. ;
  322. ; ret
  323. ;
  324. ;endp _fstricmp
  325. proc _fstricmp far
  326. arg string_dest:dword,string_source:dword
  327. push ds es di si
  328. lds si,[string_source]
  329. les di,[string_dest]
  330. @@cloop:
  331. mov al,[ds:si]
  332. mov ah,al
  333. xor al,[es:di]
  334. and al,223
  335. jnz @@done
  336. or ah,ah
  337. jz @@done
  338. inc di
  339. inc si
  340. jmp @@cloop
  341. @@done:
  342. pop si di es ds
  343. ret
  344. endp
  345. ;
  346. ; Function- str_lwr
  347. ;
  348. ; Reasonably optimized. (186)
  349. ;
  350. ; Converts an entire string to lowercase
  351. ;
  352. ; Arguments: string (char array)
  353. ;
  354. proc _fstrlwr far
  355. arg string:dword
  356. push ax es ds si di
  357. cld
  358. les di,[string]
  359. lds si,[string]
  360. @@lwrloop:
  361. lodsb
  362. or al,al
  363. jz @@done
  364. cmp al,'A'
  365. jb @@ok
  366. cmp al,'Z'
  367. ja @@ok
  368. xor al,32
  369. @@ok:
  370. stosb
  371. jmp @@lwrloop
  372. @@done:
  373. pop di si ds es ax
  374. ret
  375. endp _fstrlwr
  376. proc _fstrupr far
  377. arg string:dword
  378. push ax es ds si di
  379. cld
  380. les di,[string]
  381. lds si,[string]
  382. @@uprloop:
  383. lodsb
  384. or al,al
  385. jz @@done
  386. cmp al,'a'
  387. jb @@ok
  388. cmp al,'z'
  389. ja @@ok
  390. xor al,32
  391. @@ok:
  392. stosb
  393. jmp @@uprloop
  394. @@done:
  395. pop di si ds es ax
  396. ret
  397. endp _fstrupr
  398. ends
  399. end