Simple Python Machine Language


Berikut ini script Python 3 sederhana untuk mengubah dari instruksi assembly sederhana menjadi bahasa mesin.


dict_op = {'ADD' : '0000', 'ADDC' : '0001', 'SUB' : '0010', 'SUBC' : '0011',
           'AND' : '0100', 'OR' : '0101', 'XOR' : '0110', 'NOT' : '0111',
           'SHL' : '1000', 'SHR' : '1001', 'ROL' : '1010', 'ROR' : '1011',
           'ADDI' : '0000', 'ADDCI' : '0001', 'SUBI' : '0010', 'SUBCI' : '0011',
           'SHLI' : '1000', 'SHRI' : '1001', 'ROLI' : '1010', 'RORI' : '1011',
           'LDRI' : '1100', 'STRI' : '1101', 'NOT' : '0000', 'MOV' : '0010',
           'SWP' : '0011', 'CMP' : '0100', 'LDR' : '0110', 'STR' : '0111',
           'MOVI' : '0000', 'CMPI' : '0010', 'B' : '0000', 'BEQ' : '0001',
           'BNE' : '0010', 'BLT' : '0011', 'BGT' : '0100', 'BR' : '1000',
           'BREQ' : '1001', 'BRNE' : '1010', 'BRLT' : '1011', 'BRGT' : '1100',
           'BRI' : '0000', 'BRIEQ' : '0001', 'BRINE' : '0010', 'BRILT' : '0011',
           'BRIGT' : '0100', 'MUL' : '0000', 'NOP' : '0000'}

dict_reg = {'R0' : '0000', 'R1' : '0001', 'R2' : '0010', 'R3' : '0011',
            'R4' : '0100', 'R5' : '0101', 'R6' : '0110', 'R7' : '0111',
            'R8' : '1000', 'R9' : '1001', 'R10' : '1010', 'R11' : '1011',
            'R12' : '1100', 'R13' : '1101', 'R14' : '1110', 'R15' : '1111'}

def main(filename):
        asmfile = open(filename,'r')
        a = asmfile.readlines()
        #print('asmfile: ', a)
        a = [i.upper() for i in [ x for x in a ]]
        #print('Upper case'a)
        a = [i.strip() for i in [ x for x in a ]]
        #print('Remove white space: ', a)
        a = filter(lambda x : x != '', a)
        #print('Remove empty element: ', a)
        a = [i.replace(' ',',') for i in [ x for x in a ]]
        #print('Change space with comma: ', a)
        a = [i.split(',') for i in [ x for x in a ]]
        #print('Split by comma: ', a)
        for ins in a:
                print('\nInstruction: %s %s, %s, %s' %(ins[0], ins[1], ins[2], ins[3]))
                if (ins[0] == 'ADD' or 'ADDC' or 'SUB' or 'SUBC' or 'AND' or 'OR' or 'XOR' or 'NOT' or 'SHL' or 'SHR' or 'ROL' or 'ROR'):
                        procedure1(ins)
                if (ins[0] == 'ADDI' or 'ADDCI' or 'SUBI' or 'SUBCI' or 'SHLI' or 'SHRI' or 'ROLI' or 'RORI'):
                        procedure2(ins)
                elif (ins[0] == 'NOT' or 'MOV' or 'SWP' or 'CMP' or 'LDR' or 'STR'):
                 #       procedure3(ins)
                elif (ins[0] == 'MOVI' or 'CMPI'):
                 #       procedure4(ins)
                elif (ins[0] == 'B' or 'BEQ' or 'BNE' or 'BLT' or 'BGT' 'BR' or 'BREQ' or 'BRNE' or 'BRLT' or 'BRGT'):
                 #       procedure5(ins)
                elif (ins[0] == 'BRI' or 'BRIEQ' or 'BRINE' or 'BRILT' or 'BRIGT'):
                 #       procedure6(ins)
                elif (ins[0] == 'MUL'):
                 #       procedure7(ins)
                elif (ins[0] == 'NOP'):
                        #dict_type = '0000'
                        #print('Opcode: ', dict_op[ins[0]])
                        #print('Destination operand: ', dict_reg[ins[1]])
                        #print('Source operand 1: ', dict_reg[ins[2]])
                        #print('Source operand 2: ', dict_reg[ins[3]])
                        #bincode_string = ' %s%s%s%s%s00000000' %(dict_type, dict_op[ins[0]], dict_reg[ins[1]], dict_reg[ins[2]], dict_reg[ins[3]])
                        #print('Binary code: %s' %bincode_string)
                        #hexcode_string = '0x'+'{0:0>8X}'.format(int(bincode_string, 2))
                        #print('Hex code: ', hexcode_string,'\n')
                        break

def procedure1(ins):
        print('Opcode: ', dict_op[ins[0]])
        print('Destination operand: ', dict_reg[ins[1]])
        print('Source operand 1: ', dict_reg[ins[2]])
        print('Source operand 2: ', dict_reg[ins[3]])
        dict_type = '0001'
        bincode_string = '%s%s%s%s%s' %(dict_type, dict_op[ins[0]], dict_reg[ins[1]], dict_reg[ins[2]], dict_reg[ins[3]])
        print('Binary code: %s' %bincode_string + 'XXXXXXXXXXXX')
        hexcode_string = '0x'+'{0:0>5X}'.format(int(bincode_string, 2))
        print('Hex code: ', hexcode_string + 'XXX')
        print('Let us assume X = 0, hence')
        print('Binary code: %s' %bincode_string + '000000000000')
        print('Hex code: ', hexcode_string + '000')

def procedure2(ins):
        print('Opcode: ', dict_op[ins[0]])
        print('Destination operand: ', dict_reg[ins[1]])
        print('Source operand: ', dict_reg[ins[2]])
        dict_type = '0010'
        bincode_string = ' %s%s%s%s' %(dict_type, dict_op[ins[0]], dict_reg[ins[1]], dict_reg[ins[2]])
        print('Binary code: %s' %bincode_string + 'imm16')
        hexcode_string = '0x'+'{0:0>4X}'.format(int(bincode_string, 2))
        print('Hex code: ', hexcode_string + 'imm16')
        print('Let us assume imm16 = 0, hence')
        print('Binary code: %s' %bincode_string + '0000000000000000')
        print('Hex code: ', hexcode_string + '000')
      

if __name__ == '__main__':
        main('demo.asm')


Dengan sample file code.asm yang diperlukan.

add r1,r2,r3
addc r3,r4,r5
sub r1,r2,r3
subc r13,r14,r15
and r12,r5,r6
or r7,r8,r9
xor r0,r10,r11