简体   繁体   English

我应该如何使用字典作为 python 中的查找表从文本文件创建二进制文件

[英]How should I create a binary file from a text file using a dictionary as lookup table in python

I'm working on an assembler for my CPU I wrote in python.我正在为我在 python 中编写的 CPU 开发一个汇编程序。 I want to be able to store programs as binary files, and I'd like to be able to write simple text based programs.我希望能够将程序存储为二进制文件,并且我希望能够编写简单的基于文本的程序。 I have this example text file:我有这个示例文本文件:

LDA 0x1020 LDA 0x1020
STA 0x1030 STA 0x1030

I want my program to use this dictionary我希望我的程序使用这本词典

opcodes = {
"BRK": 0x01,
"RFG": 0x02,
"LFG": 0x03,
"JMP": 0x10,
"JMC": 0x11,
"JMZ": 0x12,
"JNZ": 0x13,
"JEQ": 0x14,
"JNE": 0x15,
"JPG": 0x16,
"JPL": 0x17,
"JSR": 0x18,
"RSR": 0x19,
"ADE": 0x20,
"ADI": 0x21,
"AAB": 0x22,
"AAX": 0x23,
"AAY": 0x24,
"ABX": 0x25,
"ABY": 0x26,
"AXY": 0x27,
"INC": 0x28,
"INB": 0x29,
"INX": 0x2A,
"INY": 0x2B,
"SUE": 0x2C,
"SUI": 0x2D,
"SAB": 0x2E,
"SAX": 0x2F,
"SAY": 0x30,
"SBX": 0x31,
"SBY": 0x32,
"SXY": 0x33,
"DEC": 0x34,
"DEB": 0x35,
"DEX": 0x36,
"DEY": 0x37,
"BSL": 0x38,
"BSR": 0x39,
"LDI": 0x40, 
"LDA": 0x41,
"LIB": 0x42, 
"LDB": 0x43,
"LIX": 0x44,
"LDX": 0x45,
"LIY": 0x46,
"LDY": 0x47,
"TAB": 0x48,
"TAX": 0x49,
"TAY": 0x4A,
"TBA": 0x4B,
"TBX": 0x4C,
"TBY": 0x4D,
"TXA": 0x4E,
"TXB": 0x4F,
"TXY": 0x50,
"TYA": 0x51,
"TYB": 0x52,
"TYX": 0x53,
"STA": 0x54,
"STB": 0x55,
"STX": 0x56,
"STY": 0x57,
"SCB": 0x58,
"SCX": 0x59,
"SCY": 0x5A,
"CAB": 0x60,
"CAX": 0x61,
"CAY": 0x62,
"CBA": 0x63,
"CBX": 0x64,
"CBY": 0x65,
"CXA": 0x66,
"CXB": 0x67,
"CXY": 0x68,
"CYA": 0x69,
"CYB": 0x6A,
"CYX": 0x6B

} }

To convert the text into a binary file the exact output should be: 0x0041, 0x1020, 0x0054, 0x1030.要将文本转换为二进制文件,确切的 output 应为:0x0041、0x1020、0x0054、0x1030。 What would be the best way to get this output.获得此 output 的最佳方法是什么。

You could do it like this but you may have to adjust for byte ordering:你可以这样做,但你可能需要调整字节顺序:

from sys import byteorder

opcodes = {
    "BRK": 0x01,
    "RFG": 0x02,
    "LFG": 0x03,
    "JMP": 0x10,
    "JMC": 0x11,
    "JMZ": 0x12,
    "JNZ": 0x13,
    "JEQ": 0x14,
    "JNE": 0x15,
    "JPG": 0x16,
    "JPL": 0x17,
    "JSR": 0x18,
    "RSR": 0x19,
    "ADE": 0x20,
    "ADI": 0x21,
    "AAB": 0x22,
    "AAX": 0x23,
    "AAY": 0x24,
    "ABX": 0x25,
    "ABY": 0x26,
    "AXY": 0x27,
    "INC": 0x28,
    "INB": 0x29,
    "INX": 0x2A,
    "INY": 0x2B,
    "SUE": 0x2C,
    "SUI": 0x2D,
    "SAB": 0x2E,
    "SAX": 0x2F,
    "SAY": 0x30,
    "SBX": 0x31,
    "SBY": 0x32,
    "SXY": 0x33,
    "DEC": 0x34,
    "DEB": 0x35,
    "DEX": 0x36,
    "DEY": 0x37,
    "BSL": 0x38,
    "BSR": 0x39,
    "LDI": 0x40,
    "LDA": 0x41,
    "LIB": 0x42,
    "LDB": 0x43,
    "LIX": 0x44,
    "LDX": 0x45,
    "LIY": 0x46,
    "LDY": 0x47,
    "TAB": 0x48,
    "TAX": 0x49,
    "TAY": 0x4A,
    "TBA": 0x4B,
    "TBX": 0x4C,
    "TBY": 0x4D,
    "TXA": 0x4E,
    "TXB": 0x4F,
    "TXY": 0x50,
    "TYA": 0x51,
    "TYB": 0x52,
    "TYX": 0x53,
    "STA": 0x54,
    "STB": 0x55,
    "STX": 0x56,
    "STY": 0x57,
    "SCB": 0x58,
    "SCX": 0x59,
    "SCY": 0x5A,
    "CAB": 0x60,
    "CAX": 0x61,
    "CAY": 0x62,
    "CBA": 0x63,
    "CBX": 0x64,
    "CBY": 0x65,
    "CXA": 0x66,
    "CXB": 0x67,
    "CXY": 0x68,
    "CYA": 0x69,
    "CYB": 0x6A,
    "CYX": 0x6B}

with open('infile.txt') as infile, open('outfile.bin', 'wb') as outfile:
    for line in infile:
        i, c = line.split()
        outfile.write(opcodes[i].to_bytes(2, byteorder=byteorder))
        outfile.write(int(c, 16).to_bytes(2, byteorder=byteorder))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM