Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. _
  2. _ __ ___ _ _ _ __(_)
  3. | '_ ` _ \| | | | '__| |
  4. | | | | | | |_| | | | |
  5. |_| |_| |_|\__,_|_| |_|
  6.  
  7. Muri is a minimalistic assembler for Nga.
  8.  
  9. The standard assembler for Nga is Naje. This is an attempt at making a much smaller assembler at a cost of requiring more manual knowledge of the Nga virtual machine and its encodings.
  10.  
  11. Input syntax
  12.  
  13. <directive> <data>
  14.  
  15. Directives are a single character. Muri recognizes:
  16.  
  17. * **i** for instructions
  18. * **d** for numeric data
  19. * **c** for character data
  20. * **s** for string data
  21. * **:** for creating a label
  22. * **r** for references to labels
  23.  
  24. Instructions are packed up to four instructions per location. You can specify them using the first two characters of the instruction name. For a non operation, use '..' instead of 'no'.
  25.  
  26. 0 nop 7 jump 14 gt 21 and
  27. 1 lit <v> 8 call 15 fetch 22 or
  28. 2 dup 9 ccall 16 store 23 xor
  29. 3 drop 10 return 17 add 24 shift
  30. 4 swap 11 eq 18 sub 25 zret
  31. 5 push 12 neq 19 mul 26 end
  32. 6 pop 13 lt 20 divmod
  33.  
  34. E.g., for a sequence of dup, multiply, no-op, drop:
  35.  
  36. i dupmu..dr
  37.  
  38. An example of a small program:
  39.  
  40. i liju....
  41. r main
  42. : square
  43. i dumure..
  44. : main
  45. i lilica..
  46. d 12
  47. r square
  48. i en......
  49.  
  50. As mentioned earlier this requires some knowledge of Nga architecture. While you can pack up to four instructions per location, you should not place anything after an instruction that modifies the instruction pointer. These are: ju, ca, cc, re, and zr.
  51.  
  52. ----
  53.  
  54. The code begins with the necessary C headers.
  55.  
  56. ````
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. ````
  61.  
  62. And then a couple of constants that determine overall memory usage.
  63.  
  64. ````
  65. #define KiB * 1024
  66. #define MAX_NAMES 1024
  67. #define STRING_LEN 64
  68. #define IMAGE_SIZE 128 KiB
  69. ````
  70.  
  71. Next, define the arrays for the reference handling.
  72.  
  73. ````
  74. char Labels[MAX_NAMES][STRING_LEN];
  75. int32_t Pointers[MAX_NAMES];
  76. int32_t np;
  77. ````
  78.  
  79. And then the variables and array for the target memory and source buffer:
  80.  
  81. ````
  82. char source[1 KiB];
  83. int32_t target[IMAGE_SIZE];
  84. int32_t here;
  85. ````
  86.  
  87. And that's the end of the data part. Now on to routines.
  88.  
  89. First up, something to save the generated image file.
  90.  
  91. ````
  92. void save() {
  93. FILE *fp;
  94.  
  95. if ((fp = fopen("ngaImage", "wb")) == NULL) {
  96. printf("Unable to save the image!\n");
  97. exit(2);
  98. }
  99.  
  100. fwrite(&target, sizeof(int32_t), here, fp);
  101. fclose(fp);
  102. }
  103. ````
  104.  
  105. Next, functions related to the reference tables. We have two. The `lookup()` searches the tables for a name and returns either -1 (if not found) or the address that corresponds to it.
  106.  
  107. ````
  108. int32_t lookup(char *name) {
  109. int32_t slice = -1;
  110. int32_t n = np;
  111. while (n > 0) {
  112. n--;
  113. if (strcmp(Labels[n], name) == 0)
  114. slice = Pointers[n];
  115. }
  116. return slice;
  117. }
  118. ````
  119.  
  120. The second, `add_label()` handles adding a new label to the table. It also terminates the build if the label already exists.
  121.  
  122. ````
  123. void add_label(char *name, int32_t slice) {
  124. if (lookup(name) == -1) {
  125. strcpy(Labels[np], name);
  126. Pointers[np] = slice;
  127. np++;
  128. } else {
  129. printf("Fatal error: %s already defined\n", name);
  130. exit(0);
  131. }
  132. }
  133. ````
  134.  
  135. This next routine reads a line from a file into the input buffer.
  136.  
  137. ````
  138. void read_line(FILE *file, char *line_buffer) {
  139. int ch = getc(file);
  140. int count = 0;
  141. while ((ch != '\n') && (ch != EOF)) {
  142. line_buffer[count] = ch;
  143. count++;
  144. ch = getc(file);
  145. }
  146. line_buffer[count] = '\0';
  147. }
  148. ````
  149.  
  150. This one is a little messy. It just checks a source string against the list of instructions and returns the corresponding opcode. It returns 0 (nop) for anything unrecognized.
  151.  
  152. ````
  153. int32_t opcode_for(char *s) {
  154. if (strcmp(s, "..") == 0) return 0; if (strcmp(s, "li") == 0) return 1;
  155. if (strcmp(s, "du") == 0) return 2; if (strcmp(s, "dr") == 0) return 3;
  156. if (strcmp(s, "sw") == 0) return 4; if (strcmp(s, "pu") == 0) return 5;
  157. if (strcmp(s, "po") == 0) return 6; if (strcmp(s, "ju") == 0) return 7;
  158. if (strcmp(s, "ca") == 0) return 8; if (strcmp(s, "cc") == 0) return 9;
  159. if (strcmp(s, "re") == 0) return 10; if (strcmp(s, "eq") == 0) return 11;
  160. if (strcmp(s, "ne") == 0) return 12; if (strcmp(s, "lt") == 0) return 13;
  161. if (strcmp(s, "gt") == 0) return 14; if (strcmp(s, "fe") == 0) return 15;
  162. if (strcmp(s, "st") == 0) return 16; if (strcmp(s, "ad") == 0) return 17;
  163. if (strcmp(s, "su") == 0) return 18; if (strcmp(s, "mu") == 0) return 19;
  164. if (strcmp(s, "di") == 0) return 20; if (strcmp(s, "an") == 0) return 21;
  165. if (strcmp(s, "or") == 0) return 22; if (strcmp(s, "xo") == 0) return 23;
  166. if (strcmp(s, "sh") == 0) return 24; if (strcmp(s, "zr") == 0) return 25;
  167. if (strcmp(s, "en") == 0) return 26;
  168. return 0;
  169. }
  170. ````
  171.  
  172. Now for the first pass. This lays down code, with dummy values for the references. They will be resolved in pass2().
  173.  
  174. ````
  175. void pass1(char *fname) {
  176. char *buffer = (char *)source;
  177. char command;
  178. unsigned int opcode;
  179. char inst[3];
  180. inst[2] = '\0';
  181. FILE *fp;
  182. here = 0;
  183. fp = fopen(fname, "r");
  184. if (fp == NULL)
  185. return;
  186. while (!feof(fp)) {
  187. read_line(fp, buffer);
  188. if (buffer[1] != '\t' && buffer[1] != ' ') {
  189. printf("ERROR: Invalid line\n");
  190. exit(2);
  191. }
  192. command = buffer[0];
  193. opcode = 0;
  194. switch (command) {
  195. case 'i': memcpy(inst, buffer + 8, 2);
  196. opcode = opcode_for(inst);
  197. opcode = opcode << 8;
  198. memcpy(inst, buffer + 6, 2);
  199. opcode += opcode_for(inst);
  200. opcode = opcode << 8;
  201. memcpy(inst, buffer + 4, 2);
  202. opcode += opcode_for(inst);
  203. opcode = opcode << 8;
  204. memcpy(inst, buffer + 2, 2);
  205. opcode += opcode_for(inst);
  206. target[here++] = opcode;
  207. break;
  208. case 'r': target[here++] = 9999;
  209. break;
  210. case 'd': target[here++] = atoi(buffer+2);
  211. break;
  212. case 'c': target[here++] = buffer[2];
  213. break;
  214. case 's': opcode = 2;
  215. while (opcode < strlen(buffer)) {
  216. target[here++] = buffer[opcode++];
  217. }
  218. target[here++] = 0;
  219. break;
  220. case ':': add_label(buffer+2, here);
  221. break;
  222. }
  223. }
  224. fclose(fp);
  225. }
  226. ````
  227.  
  228. The second pass skips over any instructions or data, but replaces the dummy values for each reference with the actual address (recorded as part of pass1()).
  229.  
  230. ````
  231. void pass2(char *fname) {
  232. char *buffer = (char *)source;
  233. char command;
  234. FILE *fp;
  235. here = 0;
  236. fp = fopen(fname, "r");
  237. if (fp == NULL)
  238. return;
  239. while (!feof(fp)) {
  240. read_line(fp, buffer);
  241. if (buffer[1] != '\t') {
  242. printf("ERROR: Invalid line\n");
  243. exit(2);
  244. }
  245. command = buffer[0];
  246. switch (command) {
  247. case 'i': here++; break;
  248. case 'r': target[here++] = lookup(buffer+2); break;
  249. case 'd': here++; break;
  250. case 'c': here++; break;
  251. case 's': here = here + strlen(buffer) - 1; break;
  252. case ':': break;
  253. }
  254. }
  255. fclose(fp);
  256. }
  257. ````
  258.  
  259. And then the top level wrapper.
  260.  
  261. ````
  262. int main(int argc, char **argv) {
  263. np = 0;
  264. if (argc > 1) {
  265. pass1(argv[1]);
  266. pass2(argv[1]);
  267. save();
  268. }
  269. else
  270. printf("muri\n(c) 2017 charles childers\n\nTry:\n %s filename\n", argv[0]);
  271. return 0;
  272. }
  273. ````
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement