Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. # asm - assembler for hack computer
  2. # usage: awk -f asm.awk assembly-program-file.asm
  3.  
  4. BEGIN {
  5. src_file = ARGV[1]
  6. tmp_file = "asm.tmp"
  7. out_file = substr(src_file, 1, index(src_file, ".")) "hack"
  8. split("0 M D DM A AM AD ADM", x)
  9. split("0 JGT JEQ JGE JLT JNE JLE JMP", y)
  10. split("000 001 010 011 100 101 110 111", b)
  11. for (i = 1; i <= 8; i++) {
  12. destc[x[i]] = b[i]
  13. jumpc[y[i]] = b[i]
  14. }
  15. destc["MD"] = "011"
  16. destc["MA"] = "101"
  17. destc["DA"] = "110"
  18. destc["DAM"] = "111"
  19. destc["MAD"] = "111"
  20. destc["MDA"] = "111"
  21. destc["DMA"] = "111"
  22. destc["AMD"] = "111"
  23. n = split("0 1 -1 D A !D !A -D -A D+1 A+1 D-1 A-1 D+A D-A A-D D&A D|A", z)
  24. split("0101010 0111111 0111010 0001100 0110000 0001101 0110001 0001111 0110011 0011111 0110111 0001110 0110010 0000010 0010011 0000111 0000000 0010101", w)
  25. for (i = 1; i <= n; i++)
  26. opc[z[i]] = w[i]
  27. opc["M"] = "1110000"
  28. opc["!M"] = "1110001"
  29. opc["-M"] = "1110011"
  30. opc["M+1"] = "1110111"
  31. opc["M-1"] = "1110010"
  32. opc["D+M"] = "1000010"
  33. opc["D-M"] = "1010011"
  34. opc["M-D"] = "1000111"
  35. opc["D&M"] = "1000000"
  36. opc["D|M"] = "1010101"
  37. symtab[R0] = 0
  38. symtab[R1] = 1
  39. symtab[R2] = 2
  40. symtab[R3] = 3
  41. symtab[R4] = 4
  42. symtab[R5] = 5
  43. symtab[R6] = 6
  44. symtab[R7] = 7
  45. symtab[R8] = 8
  46. symtab[R9] = 9
  47. symtab[R10] = 10
  48. symtab[R11] = 11
  49. symtab[R12] = 12
  50. symtab[R13] = 13
  51. symtab[R14] = 14
  52. symtab[R15] = 15
  53. symtab[SP] = 0
  54. symtab[LCL] = 1
  55. symtab[ARG] = 2
  56. symtab[THIS] = 3
  57. symtab[THAT] = 4
  58. symtab[SCREEN] = 16384
  59. symtab[KBD] = 24576
  60.  
  61. # ASSEMBLER PASS 1
  62. while (getline <src_file > 0) {
  63. if (/^[[:space:]]*(\/\/.*)*$/)
  64. continue
  65. else if (/\(.*\)/) {
  66. symtab[substr($0, 2, length($0)-2)] = nl-ns+1
  67. ns++; nl++
  68. } else {
  69. # print sub(/[[:space:]]*/, "")
  70. print $0 >tmp_file
  71. nl++
  72. }
  73. }
  74. close(tmp_file)
  75.  
  76. # ASSEMBLER PASS 2
  77. nextram = 16
  78. while (getline <tmp_file > 0) {
  79. if (/^@/) { # A-instruction
  80. con = substr($0, 2)
  81. if (con !~ /^[:digit:]+$/)
  82. if (con in symtab) {
  83. con = symtab[con]
  84. } else {
  85. symtab[con] = nextram
  86. con = nextram
  87. nextram++
  88. }
  89. binstring = ""
  90. for (i = 0; i < 15; i++)
  91. if (and(con, lshift(1,i))) {
  92. binstring = "1" binstring
  93. } else {
  94. binstring = "0" binstring
  95. }
  96. print "0" binstring > out_file
  97. } else { # C-instruction
  98. m = index($0, "=")
  99. n = index($0, ";")
  100. dest = m ? substr($0, 1, m-1) : 0
  101. comm = n ? substr($0, m+1, n-m-1) : substr($0, m+1)
  102. jump = n ? substr($0, n+1) : 0
  103. print comm
  104. printf("comm %s; jump %s\n", comm, jump)
  105. printf("111%s%s%s\n", opc[op], destc[dest], jumpc[jump]) > out_file
  106. }
  107. }
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement