Advertisement
Korotkodul

machine_code

Mar 20th, 2025 (edited)
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 KB | None | 0 0
  1. import pyparsing as pp
  2.  
  3. command_name = pp.Word(pp.alphas)
  4. argument_name = pp.Word(pp.printables, exclude_chars=';,')
  5. arguments = pp.Optional(argument_name) + pp.ZeroOrMore(pp.Suppress(',') + argument_name)
  6. label = pp.Word(pp.alphas) + pp.Optional(pp.Suppress(' ')) + pp.Suppress(":")
  7. comment = pp.Suppress(';') + pp.Optional(pp.Suppress(' ')) + pp.restOfLine()
  8. rule =  pp.Optional(label) + pp.Optional(command_name) + pp.Optional(arguments) + pp.Optional(comment)
  9. def parse(s):
  10.     s = s.lower().rstrip().lstrip()
  11.     if s[0] == '.':
  12.         s = s.replace(" ", "")
  13.         return ['start_from_address', s[2:]]
  14.     return rule.parseString(s).asList()
  15.  
  16.  
  17.  
  18. assembler_code = []
  19. filename = 'assembler_code.txt'
  20. #filename = 'pdp11_code.txt'
  21. with open(filename, 'r', encoding='utf-8') as file:
  22.     for line in file:
  23.         line = line.lower().rstrip().lstrip()
  24.         assembler_code.append(parse(line))
  25.  
  26. print("assembler_code", assembler_code)
  27.  
  28. opcode = {
  29.     'mov': '0001',
  30.     'add': '0110'
  31. }
  32.  
  33. def to3bit(x):
  34.     res = str(bin(int(x))[2:])
  35.     while len(res) < 3:
  36.         res = '0' + res
  37.     return res
  38.  
  39. #print(to3bit('3'))
  40.  
  41. def to_four_digit_hex_number(x):
  42.     res = str(hex(int(x)))[2:]
  43.     while len(res) < 4:
  44.         res = '0' + res
  45.     return res
  46.  
  47. #print(to_four_digit_hex_number('10'))
  48.  
  49. #для начала считаем, что сначала идёт команда. то есть без label
  50. #считаем, что у нас могут быть только 3 команды: move/add/halt
  51. #у этих 3х команд аргументы - это SSDD
  52. #вырианты: cmd R, R; cmd #, R;
  53.  
  54. #на случай # у нас есть переменные additional...
  55. def to_raw_machine_code(command):
  56.     if command[0] == 'halt':
  57.         return ["00", "00"]
  58.     if command[0] == 'start_from_address':
  59.         return [to_four_digit_hex_number(int(command[1], 8)), 'number of bytes in the resulting file']
  60.     command_code = opcode[command[0]]
  61.  
  62.     source = command[1]
  63.     source_mode = ''
  64.     source_R_num = ''
  65.     if source[0] == 'r':
  66.         source_mode = '000'
  67.         source_R_num = to3bit(source[1])
  68.     source_additional_word = ''
  69.     if source[0] == '#':
  70.         source_mode = '010'
  71.         source_R_num = '111'
  72.         source_additional_word = to_four_digit_hex_number(source[1:])
  73.  
  74.     destination = command[2]
  75.     destination_mode = ''
  76.     destination_R_num = ''
  77.     if destination[0] == 'r':
  78.         destination_mode = '000'
  79.         destination_R_num = to3bit(destination[1])
  80.  
  81.     raw_machine_command = command_code + source_mode + source_R_num + destination_mode + destination_R_num
  82.     return raw_machine_command, source_additional_word
  83.  
  84. raw_machine_code = []
  85. for cmd in assembler_code:
  86.     raw_machine_code.append(to_raw_machine_code(cmd))
  87.  
  88. print("raw_machine_code",  raw_machine_code)
  89.  
  90. machine_code = []
  91. def to_machine_code(raw_machine_command):
  92.     #print("raw_machine_command", raw_machine_command)
  93.     if len(raw_machine_command[0]) != 16:
  94.         return raw_machine_command
  95.  
  96.     raw_command_code = raw_machine_command[0]
  97.     piece1 = raw_command_code[:4]
  98.     piece2 = raw_command_code[4:8]
  99.     piece3 = raw_command_code[8:12]
  100.     piece4 = raw_command_code[12:]
  101.     #print("pieces", piece1, piece2, piece3, piece4)
  102.     num1 = hex(int(piece1,  2))[2:] + hex(int(piece2, 2))[2:]
  103.     num2 = hex(int(piece3,  2))[2:] + hex(int(piece4, 2))[2:]
  104.     machine_command = [num2, num1]
  105.  
  106.     additional_num1 = ''
  107.     additional_num2 = ''
  108.     if len(raw_machine_command[1]) == 4:
  109.         additional_num1 = raw_machine_command[1][:2]
  110.         additional_num2 = raw_machine_command[1][2:]
  111.         machine_command.append(additional_num2)
  112.         machine_command.append(additional_num1)
  113.  
  114.     return machine_command
  115.  
  116. for cmd in raw_machine_code:
  117.     cmd = to_machine_code(cmd)
  118.     machine_code.append(cmd)
  119.  
  120. print("machine_code", machine_code)
  121.  
  122. with open("machine_code.txt", "w", encoding="utf-8") as file:
  123.     for cmd in machine_code:
  124.         for line in cmd:
  125.             file.write(line + "\n")
  126.  
  127. print("Файл успешно создан и записан.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement