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

Question:

I’m working on an assembler for my CPU I wrote in python. 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
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. What would be the best way to get this output.

Asked By: TheBearSlicer

||

Answers:

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))
Answered By: Vlad
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.