You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

339 lines
7.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. typedef struct Length_String {
  2. short dynamic;
  3. unsigned int length;
  4. char *chars;
  5. } Length_String;
  6. typedef struct String_Array {
  7. unsigned int length;
  8. Length_String **strings;
  9. } String_Array;
  10. void strcpy (destination, destination_segment, source, source_segment );
  11. int strlen (source, source_segment);
  12. void memcpy (destination, destination_segment, source, source_segment, num_bytes );
  13. /* Additionals to standard c lib. */
  14. String_Array *string_split_c(string, delim, keep_delim);
  15. Length_String *make_length_string(length, chars);
  16. Length_String *make_length_string_c(cstring);
  17. String_Array *length_string_split(string, delim, keep_delim);
  18. String_Array *create_length_string_array(array_size);
  19. Length_String *create_length_string(length, chars);
  20. int strlen (source, source_segment)
  21. {
  22. #asm
  23. #define strlen_SOURCE 4[bp];
  24. #define strlen_SOURCE_SEGMENT 6[bp];
  25. push bp
  26. mov bp,sp
  27. push ds
  28. push bx
  29. mov ax, strlen_SOURCE_SEGMENT
  30. mov ds, ax
  31. mov bx, strlen_SOURCE
  32. label_strlen:
  33. mov cx, #0x0 ; Set counte to zero
  34. .label_strlen_loop:
  35. mov BYTE al, [bx]
  36. cmp al, #0x0
  37. je .label_strlen_done
  38. inc cx ; Count 1
  39. inc bx ; Look at next char
  40. jmp .label_strlen_loop
  41. .label_strlen_done:
  42. mov ax, cx
  43. pop bx
  44. pop ds
  45. pop bp
  46. #endasm
  47. }
  48. void strcpy (destination, destination_segment, source, source_segment )
  49. char *destination;
  50. int destination_segment;
  51. char *source;
  52. int source_segment;
  53. {
  54. #asm
  55. ; copy two strings
  56. ; IN si: the first (zero terminated) string
  57. ; IN di: the second (zero terminated) string
  58. ; OUT SF and ZF (same semantics as cmp)
  59. #define DESTINATION 4[bp];
  60. #define DESTINATION_SEGMENT 6[bp];
  61. #define SOURCE 8[bp];
  62. #define SOURCE_SEGMENT 10[bp];
  63. push bp
  64. mov bp,sp
  65. label_strcpy:
  66. push ax
  67. push bx
  68. push di
  69. push es
  70. push si
  71. push ds
  72. mov ax, DESTINATION;
  73. mov di, ax
  74. mov ax, DESTINATION_SEGMENT;
  75. mov es, ax
  76. mov ax, SOURCE;
  77. mov si, ax
  78. mov ax, SOURCE_SEGMENT;
  79. mov ds, ax
  80. mov cx, 0x050 ;TODO(Jørn) Hardcded number of bytes to copy
  81. .label_strcpy_loop:
  82. movsb
  83. cmp cx, 0x0
  84. je .label_strcpy_end
  85. dec cx
  86. jmp .label_strcpy_loop
  87. .label_strcpy_end:
  88. pop ds
  89. pop si
  90. pop es
  91. pop di
  92. pop bx
  93. pop ax
  94. pop bp
  95. #endasm
  96. }
  97. void memcpy (destination, destination_segment, source, source_segment, num_bytes)
  98. void *destination;
  99. int destination_segment;
  100. void *source;
  101. int source_segment;
  102. int num_bytes;
  103. {
  104. #asm
  105. ; copy two strings
  106. ; IN si: the first (zero terminated) string
  107. ; IN di: the second (zero terminated) string
  108. ; OUT SF and ZF (same semantics as cmp)
  109. #define DESTINATION 4[bp];
  110. #define DESTINATION_SEGMENT 6[bp];
  111. #define SOURCE 8[bp];
  112. #define SOURCE_SEGMENT 10[bp];
  113. #define NUM_BYTES 12[bp];
  114. push bp
  115. mov bp,sp
  116. label_memcpy:
  117. push ax
  118. push bx
  119. push di
  120. push es
  121. push si
  122. push ds
  123. mov ax, DESTINATION;
  124. mov di, ax
  125. mov ax, DESTINATION_SEGMENT;
  126. mov es, ax
  127. mov ax, SOURCE;
  128. mov si, ax
  129. mov ax, SOURCE_SEGMENT;
  130. mov ds, ax
  131. mov cx, NUM_BYTES
  132. .label_memcpy_loop:
  133. movsb
  134. cmp cx, 0x0
  135. je .label_memcpy_end
  136. dec cx
  137. jmp .label_memcpy_loop
  138. .label_memcpy_end:
  139. pop ds
  140. pop si
  141. pop es
  142. pop di
  143. pop bx
  144. pop ax
  145. pop bp
  146. #endasm
  147. }
  148. int strcmp(source_1, source_2)
  149. char* source_1;
  150. char* source_2;
  151. {
  152. int i;
  153. int count;
  154. count = strlen(source_1, 0x7e0);
  155. if (count != strlen(source_2, 0x7e0))
  156. {
  157. return -1;
  158. }
  159. for (i = 0; i < count; i++)
  160. {
  161. if(source_1[i] != source_2[i])
  162. {
  163. return -1;
  164. }
  165. }
  166. return 0;
  167. /*
  168. #asm
  169. #define DESTINATION 4[bp];
  170. #define DESTINATION_SEGMENT 6[bp];
  171. #define SOURCE 8[bp];
  172. #define SOURCE_SEGMENT 10[bp];
  173. ; Compares two strings
  174. ; IN si: the first (zero terminated) string
  175. ; IN di: the second (zero terminated) string
  176. ; OUT SF and ZF (same semantics as cmp)
  177. strcmp_stringcompare:
  178. push bx
  179. push si
  180. push di
  181. .strcmp_loop:
  182. mov bl, [si]
  183. mov bh, [di]
  184. cmp bl, bh
  185. jne .strcmp_end
  186. test bl, bl ; bl and bh are the same, so bl = 0 => dl = 0
  187. jz .strcmp_end
  188. inc si
  189. inc di
  190. jmp .strcmp_loop
  191. .strcmp_end:
  192. pop di
  193. pop si
  194. pop bx
  195. ret
  196. #endasm
  197. */
  198. }
  199. String_Array *string_split_c(string, delim, keep_delim)
  200. char *string;
  201. char delim;
  202. short keep_delim;
  203. {
  204. return length_string_split(make_length_string_c(string), delim, keep_delim);
  205. }
  206. Length_String *make_length_string_c(cstring)
  207. char *cstring;
  208. {
  209. return make_length_string(strlen(cstring, 0x7e0), cstring);
  210. }
  211. Length_String *make_length_string(length, chars)
  212. unsigned int length;
  213. char *chars;
  214. {
  215. Length_String *legth_string = malloc(sizeof(Length_String));
  216. char *copy = malloc(length + 1); /* Space for null terminator */
  217. memcpy(copy, 0x7e0, chars, 0x7e0, length);
  218. copy[length] = '\0';
  219. legth_string->dynamic = 0;
  220. legth_string->length = length;
  221. legth_string->chars = (char *)chars;
  222. return legth_string;
  223. }
  224. String_Array *length_string_split(string, delim, keep_delim)
  225. Length_String *string;
  226. char delim;
  227. short keep_delim;
  228. {
  229. unsigned int i;
  230. unsigned int last;
  231. unsigned int count = 0;
  232. unsigned int insert_index = 0;
  233. String_Array *result;
  234. int k;
  235. i = 0;
  236. last = 0;
  237. while(i < string->length) {
  238. if (string->chars[i] == delim) {
  239. if (i > last+1) {
  240. ++count;
  241. }
  242. last = i;
  243. }
  244. ++i;
  245. }
  246. if (i > last+1) {
  247. ++count;
  248. }
  249. result = create_length_string_array(count);
  250. if (keep_delim)
  251. {
  252. k = 0;
  253. }
  254. else
  255. {
  256. k = 1;
  257. }
  258. i = 0;
  259. last = 0;
  260. while(i < string->length) {
  261. if (string->chars[i] == delim) {
  262. if (i > last+1) {
  263. result->strings[insert_index++] = create_length_string(i-(last+k), string->chars+(last+k));
  264. }
  265. last = i;
  266. }
  267. ++i;
  268. }
  269. if (i > last+1) {
  270. result->strings[insert_index++] = create_length_string(i-(last+k), ((string->chars)+(last+k)));
  271. }
  272. return result;
  273. }
  274. String_Array *create_length_string_array(array_size)
  275. int array_size;
  276. {
  277. String_Array *result = malloc(sizeof(String_Array));
  278. result->length = array_size;
  279. result->strings = malloc(array_size * sizeof(Length_String));
  280. return result;
  281. }
  282. Length_String *create_length_string(length, chars)
  283. unsigned int length;
  284. char *chars;
  285. {
  286. Length_String *legth_string = malloc(sizeof(Length_String));
  287. char *copy = malloc(length + 1); /* Space for null terminator */
  288. memcpy(copy, 0x7e0, chars, 0x7e0, length);
  289. copy[length] = '\0';
  290. legth_string->dynamic = 1;
  291. legth_string->length = length;
  292. legth_string->chars = copy;
  293. return legth_string;
  294. }