assembler.py
def formatEncode(self,categorizedLine,line,lineNumber):
        """Encodes the instruction into the format provided by the target"""
        current_instruction = self.instructions[categorizedLine]
        current_format = self.formats[current_instruction[0]]
        current_element = 1
        instruction_length = current_format[0]
        max_elements = len(line)
        
        instruction_fields = {}
        
        for instField in current_instruction[1:]:
            if isinstance(instField[1],str): inputType = instField[1].upper()
            else: inputType = instField[1]
            if self.listGet(instField,2): is_pass = True
            else: is_pass = False
            
            if isinstance(inputType,int):
                selectedValue = inputType
            elif current_element >= max_elements:
                print(ERRORS[0].format(lineNumber,current_element))
                selectedValue = 0
            # Detect register
            elif inputType.startswith('REG'):
                if line[current_element] in self.registers:
                    selectedValue = self.registers[line[current_element]]
                    current_element += 1
                else:
                    print(ERRORS[1].format(lineNumber))
                    selectedValue = 0
            # Detect condition
            elif inputType.startswith('COND'):
                if line[current_element] in self.conditions:
                    selectedValue = self.conditions[line[current_element]]
                    current_element += 1
                else:
                    print(ERRORS[2].format(lineNumber))
                    selectedValue = 0
            # Detect numbers
            elif inputType.startswith('NUM'):
                if re.search('[\-]*\d+',line[current_element]):
                    selectedValue = int(line[current_element])
                    # Shift the inputs if needed
                    shiftAmount = self.listGet(instField,2)
                    if shiftAmount >= 0:
                        selectedValue = selectedValue >> shiftAmount
                    else:
                        selectedValue = selectedValue << -shiftAmount
                    current_element += 1
                else:
                    print(ERRORS[3].format(lineNumber))
                    selectedValue = 0
            else:
                print(ERRORS[4].format(lineNumber))
                selectedValue = 0
            
            # Skip over the next element if pass
            if is_pass:
                current_element += 1
            
            instruction_fields[instField[0]] = selectedValue
        
        format_fields = []
        
        for formatField in current_format[1:]:
            try:
                format_fields.append(
                    [instruction_fields[formatField[0]],formatField[1]])
            except:
                print(ERRORS[5].format(lineNumber))
                return []
        
        instruction = self.instFormatter(instruction_length,format_fields)
        
        # Post base-encoding shifting
        if current_instruction[0] in self.post_shift:
            instruction_length = self.post_shift[current_instruction[0]]
            shiftedInstruction = 0
            
            # Gradually shifts each field in the instruction and ORs it
            # the final instruction
            for shiftField in self.post_shift[current_instruction[0]][1:]:
                andOriginal = instruction & shiftField[0]
                shiftAmount = self.listGet(shiftField,1)
                shiftDirection = shiftAmount >= 0 # 1 = right, 0 = left
                shiftedInstruction = (shiftedInstruction
                    |(andOriginal >> shiftAmount if shiftDirection else andOriginal >> shiftAmount))
            
            instruction = shiftedInstruction
        
        # Returns the instruction in the form of bytes, reverses order
        # if target is little endian
        return [byte & 255 for byte in [
            instruction >> (8*x) for x in range(
            instruction_length//8)][::[1,-1][self.big_endian]]]
Encodes the instruction into the format provided by the target
Encodes the instruction into the format provided by the target
Encodes the instruction into the format provided by the target
def formatEncode(self,categorizedLine,line,lineNumber):
        current_instruction = self.instructions[categorizedLine]
        current_format = self.formats[current_instruction[0]]
        current_element = 1
        instruction_length = current_format[0]
        max_elements = len(line)
        instruction_fields = {}
        for instField in current_instruction[1:]:
            if isinstance(instField[1],str): inputType = instField[1].upper()
            else: inputType = instField[1]
            if self.listGet(instField,2): is_pass = True
            else: is_pass = False
            if isinstance(inputType,int):
                selectedValue = inputType
            elif current_element >= max_elements:
                print(ERRORS[0].format(lineNumber,current_element))
                selectedValue = 0
            elif inputType.startswith('REG'):
                if line[current_element] in self.registers:
                    selectedValue = self.registers[line[current_element]]
                    current_element += 1
                else:
                    print(ERRORS[1].format(lineNumber))
                    selectedValue = 0
            elif inputType.startswith('COND'):
                if line[current_element] in self.conditions:
                    selectedValue = self.conditions[line[current_element]]
                    current_element += 1
                else:
                    print(ERRORS[2].format(lineNumber))
                    selectedValue = 0
            elif inputType.startswith('NUM'):
                if re.search('[\-]*\d+',line[current_element]):
                    selectedValue = int(line[current_element])
                    shiftAmount = self.listGet(instField,2)
                    if shiftAmount >= 0:
                        selectedValue = selectedValue >> shiftAmount
                    else:
                        selectedValue = selectedValue << -shiftAmount
                    current_element += 1
                else:
                    print(ERRORS[3].format(lineNumber))
                    selectedValue = 0
            else:
                print(ERRORS[4].format(lineNumber))
                selectedValue = 0
            if is_pass:
                current_element += 1
            instruction_fields[instField[0]] = selectedValue
        format_fields = []
        for formatField in current_format[1:]:
            try:
                format_fields.append(
                    [instruction_fields[formatField[0]],formatField[1]])
            except:
                print(ERRORS[5].format(lineNumber))
                return []
        instruction = self.instFormatter(instruction_length,format_fields)
        if current_instruction[0] in self.post_shift:
            instruction_length = self.post_shift[current_instruction[0]]
            shiftedInstruction = 0
            for shiftField in self.post_shift[current_instruction[0]][1:]:
                andOriginal = instruction & shiftField[0]
                shiftAmount = self.listGet(shiftField,1)
                shiftDirection = shiftAmount >= 0 
                shiftedInstruction = (shiftedInstruction
                    |(andOriginal >> shiftAmount if shiftDirection else andOriginal >> shiftAmount))
            instruction = shiftedInstruction
        return [byte & 255 for byte in [
            instruction >> (8*x) for x in range(
            instruction_length//8)][::[1,-1][self.big_endian]]]
def formatEncode ( self , categorizedLine , line , lineNumber ) : current_instruction = self . instructions [ categorizedLine ] current_format = self . formats [ current_instruction [ 0 ] ] current_element = 1 instruction_length = current_format [ 0 ] max_elements = len ( line ) instruction_fields = { } for instField in current_instruction [ 1 : ] : if isinstance ( instField [ 1 ] , str ) : inputType = instField [ 1 ] . upper ( ) else : inputType = instField [ 1 ] if self . listGet ( instField , 2 ) : is_pass = True else : is_pass = False if isinstance ( inputType , int ) : selectedValue = inputType elif current_element >= max_elements : print ( ERRORS [ 0 ] . format ( lineNumber , current_element ) ) selectedValue = 0 elif inputType . startswith ( 'REG' ) : if line [ current_element ] in self . registers : selectedValue = self . registers [ line [ current_element ] ] current_element += 1 else : print ( ERRORS [ 1 ] . format ( lineNumber ) ) selectedValue = 0 elif inputType . startswith ( 'COND' ) : if line [ current_element ] in self . conditions : selectedValue = self . conditions [ line [ current_element ] ] current_element += 1 else : print ( ERRORS [ 2 ] . format ( lineNumber ) ) selectedValue = 0 elif inputType . startswith ( 'NUM' ) : if re . search ( '[\-]*\d+' , line [ current_element ] ) : selectedValue = int ( line [ current_element ] ) shiftAmount = self . listGet ( instField , 2 ) if shiftAmount >= 0 : selectedValue = selectedValue >> shiftAmount else : selectedValue = selectedValue << - shiftAmount current_element += 1 else : print ( ERRORS [ 3 ] . format ( lineNumber ) ) selectedValue = 0 else : print ( ERRORS [ 4 ] . format ( lineNumber ) ) selectedValue = 0 if is_pass : current_element += 1 instruction_fields [ instField [ 0 ] ] = selectedValue format_fields = [ ] for formatField in current_format [ 1 : ] : try : format_fields . append ( [ instruction_fields [ formatField [ 0 ] ] , formatField [ 1 ] ] ) except : print ( ERRORS [ 5 ] . format ( lineNumber ) ) return [ ] instruction = self . instFormatter ( instruction_length , format_fields ) if current_instruction [ 0 ] in self . post_shift : instruction_length = self . post_shift [ current_instruction [ 0 ] ] shiftedInstruction = 0 for shiftField in self . post_shift [ current_instruction [ 0 ] ] [ 1 : ] : andOriginal = instruction & shiftField [ 0 ] shiftAmount = self . listGet ( shiftField , 1 ) shiftDirection = shiftAmount >= 0 shiftedInstruction = ( shiftedInstruction | ( andOriginal >> shiftAmount if shiftDirection else andOriginal >> shiftAmount ) ) instruction = shiftedInstruction return [ byte & 255 for byte in [ instruction >> ( 8 * x ) for x in range ( instruction_length // 8 ) ] [ : : [ 1 , - 1 ] [ self . big_endian ] ] ]
Encodes the instruction into the format provided by the target
Encodes the instruction into the format provided by the target
"""Encodes the instruction into the format provided by the target""" # Detect register # Detect condition # Detect numbers # Shift the inputs if needed # Skip over the next element if pass # Post base-encoding shifting # Gradually shifts each field in the instruction and ORs it # the final instruction # 1 = right, 0 = left # Returns the instruction in the form of bytes, reverses order # if target is little endian
self
categorizedLine
line
lineNumber