Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

281 строка
6.1 KiB

5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
5 лет назад
  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. String_Array *string_split_c(string, delim, keep_delim)
  149. char *string;
  150. char delim;
  151. short keep_delim;
  152. {
  153. return length_string_split(make_length_string_c(string), delim, keep_delim);
  154. }
  155. Length_String *make_length_string_c(cstring)
  156. char *cstring;
  157. {
  158. return make_length_string(strlen(cstring, 0x7e0), cstring);
  159. }
  160. Length_String *make_length_string(length, chars)
  161. unsigned int length;
  162. char *chars;
  163. {
  164. Length_String *legth_string = malloc(sizeof(Length_String));
  165. char *copy = malloc(length + 1); /* Space for null terminator */
  166. memcpy(copy, 0x7e0, chars, 0x7e0, length);
  167. copy[length] = '\0';
  168. legth_string->dynamic = 0;
  169. legth_string->length = length;
  170. legth_string->chars = (char *)chars;
  171. return legth_string;
  172. }
  173. String_Array *length_string_split(string, delim, keep_delim)
  174. Length_String *string;
  175. char delim;
  176. short keep_delim;
  177. {
  178. unsigned int i;
  179. unsigned int last;
  180. unsigned int count = 0;
  181. unsigned int insert_index = 0;
  182. String_Array *result;
  183. int k;
  184. i = 0;
  185. last = 0;
  186. while(i < string->length) {
  187. if (string->chars[i] == delim) {
  188. if (i > last+1) {
  189. ++count;
  190. }
  191. last = i;
  192. }
  193. ++i;
  194. }
  195. if (i > last+1) {
  196. ++count;
  197. }
  198. result = create_length_string_array(count);
  199. if (keep_delim)
  200. {
  201. k = 0;
  202. }
  203. else
  204. {
  205. k = 1;
  206. }
  207. i = 0;
  208. last = 0;
  209. while(i < string->length) {
  210. if (string->chars[i] == delim) {
  211. if (i > last+1) {
  212. result->strings[insert_index++] = create_length_string(i-(last+k), string->chars+(last+k));
  213. }
  214. last = i;
  215. }
  216. ++i;
  217. }
  218. if (i > last+1) {
  219. result->strings[insert_index++] = create_length_string(i-(last+k), ((string->chars)+(last+k)));
  220. }
  221. return result;
  222. }
  223. String_Array *create_length_string_array(array_size)
  224. int array_size;
  225. {
  226. String_Array *result = malloc(sizeof(String_Array));
  227. result->length = array_size;
  228. result->strings = malloc(array_size * sizeof(Length_String));
  229. return result;
  230. }
  231. Length_String *create_length_string(length, chars)
  232. unsigned int length;
  233. char *chars;
  234. {
  235. Length_String *legth_string = malloc(sizeof(Length_String));
  236. char *copy = malloc(length + 1); /* Space for null terminator */
  237. memcpy(copy, 0x7e0, chars, 0x7e0, length);
  238. copy[length] = '\0';
  239. legth_string->dynamic = 1;
  240. legth_string->length = length;
  241. legth_string->chars = copy;
  242. return legth_string;
  243. }