stage1_assembler-1.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of stage0.
  3. *
  4. * stage0 is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * stage0 is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with stage0. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <stdint.h>
  20. #include <stdbool.h>
  21. FILE* source_file;
  22. bool toggle;
  23. uint8_t holder;
  24. uint32_t ip;
  25. uint32_t* table;
  26. void storeLabel()
  27. {
  28. int c = fgetc(source_file);
  29. table[c] = ip;
  30. }
  31. void storePointer()
  32. {
  33. ip = ip + 2;
  34. int c = fgetc(source_file);
  35. int target = table[c];
  36. uint8_t first, second;
  37. first = (target - ip)/256;
  38. second = (target - ip)%256;
  39. printf("%c%c", first, second );
  40. }
  41. void line_Comment()
  42. {
  43. int c = fgetc(source_file);
  44. while(('\n' != c) && ('\r' != c))
  45. {
  46. c = fgetc(source_file);
  47. }
  48. }
  49. int8_t hex(int c)
  50. {
  51. switch(c)
  52. {
  53. case '0' ... '9':
  54. {
  55. return (c - 48);
  56. }
  57. case 'a' ... 'z':
  58. {
  59. return (c - 87);
  60. }
  61. case 'A' ... 'Z':
  62. {
  63. return (c - 55);
  64. }
  65. case '#':
  66. case ';':
  67. {
  68. line_Comment();
  69. return -1;
  70. }
  71. default: return -1;
  72. }
  73. }
  74. void first_pass()
  75. {
  76. int c;
  77. for(c = fgetc(source_file); EOF != c; c = fgetc(source_file))
  78. {
  79. /* Check for and deal with label */
  80. if(':' == c)
  81. {
  82. storeLabel();
  83. }
  84. /* check for and deal with pointers to labels */
  85. if('@' == c)
  86. {
  87. c = fgetc(source_file);
  88. ip = ip + 2;
  89. }
  90. else
  91. {
  92. if(0 <= hex(c))
  93. {
  94. if(toggle)
  95. {
  96. ip = ip + 1;
  97. }
  98. toggle = !toggle;
  99. }
  100. }
  101. }
  102. }
  103. void second_pass()
  104. {
  105. int c;
  106. for(c = fgetc(source_file); EOF != c; c = fgetc(source_file))
  107. {
  108. if(':' == c)
  109. {
  110. c = fgetc(source_file);
  111. }
  112. else if('@' == c)
  113. {
  114. storePointer();
  115. }
  116. else
  117. {
  118. if(0 <= hex(c))
  119. {
  120. if(toggle)
  121. {
  122. printf("%c",((holder * 16)) + hex(c));
  123. ip = ip + 1;
  124. holder = 0;
  125. }
  126. else
  127. {
  128. holder = hex(c);
  129. }
  130. toggle = !toggle;
  131. }
  132. }
  133. }
  134. }
  135. /* Standard C main program */
  136. int main(int argc, char **argv)
  137. {
  138. /* Make sure we have a program tape to run */
  139. if (argc < 2)
  140. {
  141. fprintf(stderr, "Usage: %s $FileName\nWhere $FileName is the name of the paper tape of the program being run\n", argv[0]);
  142. return EXIT_FAILURE;
  143. }
  144. source_file = fopen(argv[1], "r");
  145. table = calloc(256, sizeof(uint32_t));
  146. toggle = false;
  147. ip = 0;
  148. holder = 0;
  149. first_pass();
  150. rewind(source_file);
  151. toggle = false;
  152. ip = 0;
  153. holder = 0;
  154. second_pass();
  155. return EXIT_SUCCESS;
  156. }