def test_hex_int(self): assert int_to_hex(5) == '5' assert int_to_hex(10) == 'a' assert int_to_hex(5, 2) == '05' assert int_to_hex(32, 2) == '20' assert hex_to_int('5') == 5 assert hex_to_int('a') == 10 assert hex_to_int('05') == 5 assert hex_to_int('20') == 32
def ih(number): u""" Gautąjį ``number`` konvertuoja į šešioliktainį skaičių dviejuose baituose. """ string = int_to_hex(number, 2) assert len(string) == 2 return string
def create_virtual_memory( self, code, code_size, data, data_size, stdin_handler=None, stdout_handler=None): u""" Išskiria virtualią atmintį ir į ją įkelia kodą bei duomenis. """ pager = Pager( self, C=code_size, D=data_size, stdin_handler=stdin_handler, stdout_handler=stdout_handler) # Įkeliamas kodo segmentas. vmcode = VirtualMemoryCode(self, pager) labels = {} clean_code = [] for i, line in enumerate(code): if ':' in line: label, command = line.split(':') labels[label.strip()] = i else: command = line clean_code.append(command.strip().decode('utf-8')) for i, command in enumerate(clean_code): if u'«' in command: label = command.split(u'«', 1)[1].split(u'»', 1)[0] command = command.replace( u'«{0}»'.format(label), int_to_hex(labels[label], 3)).encode('utf-8') else: command = command.encode('utf-8') command += ' ' * (WORD_SIZE - len(command)) vmcode[i] = command # Įkeliamas duomenų segmentas. vmdata = VirtualMemoryData(self, pager) clean_data = {} address = 0 for i, line in enumerate(data): result = re.search('^\[(?P<address>\w+)\]:', line) if result: hex_address = result.groupdict()['address'] line = line[len('[{0}]:'.format(hex_address)):] address = hex_to_int(hex_address) if not clean_data.has_key(address): clean_data[address] = [] line = line.replace('\n', '') clean_data[address].append(line) word_format = '{{0:<{0}}}'.format(WORD_SIZE) for address, lines in clean_data.items(): data = ''.join(lines) for i, j in enumerate(range(0, len(data), WORD_SIZE)): vmdata[address + i] = word_format.format( data[j:j + WORD_SIZE]) return vmcode, vmdata