def _Mp(instr, assembler, index, modrm): if match(instr.tokens[index:index+3], '[', None, ']'): if instr.tokens[index+1].upper() not in REGS8 + REGS16 + SEGMENTS: modrm['mod'] = 0 modrm['rm'] = 6 return 3, _build_modrm(assembler, modrm) + pack_le16u(assembler.parse_integer_or_label( instr.tokens[index+1], size=2, bits_size=16, offset=1))
def _Ap(instr, assembler, index, modrm): arg = instr.tokens[index] if ':' in arg and arg.upper() not in REGS8 + REGS16 + SEGMENTS: segment, offset = arg.split(':') return 1, pack_le16u( assembler.parse_integer_or_label( offset, size=2, bits_size=16, offset=1), assembler.parse_integer_or_label(segment, size=2, bits_size=16, offset=3))
def _Jv(instr, assembler, index, modrm): arg = instr.tokens[index] if ':' not in arg and arg.upper() not in REGS8 + REGS16 + SEGMENTS: return 1, pack_le16u( assembler.parse_integer_or_label( arg, size=2, bits_size=16, offset=1, relative=assembler.pc+3))
def directive_dw(self, instr): for token in instr.tokens[1:]: blob = b'' if token[0] in ('"', '\''): blob = token[1:-1].encode('utf16') else: value = self.parse_integer(token) if value is None: self.add_label_translation(label=token, size=2, offset=0) blob = pack_le16u(value) self.assembled_bytes += blob self.org_counter += len(blob)
def directive_dw(self, instr): for token in instr.tokens[1:]: blob = b'' if token[0] in ('"', '\''): blob = token[1:-1].encode('utf16') else: value = self.parse_integer_or_label(label=token, bits_size=16, size=2) if self.big_endian: blob = pack_be16u(value) else: blob = pack_le16u(value) self.append_assembled_bytes(blob)
def directive_inccsv_le16(self, instr): if len(instr.tokens) != 2: raise InvalidArgumentsForDirective(instr) import csv filename = self.stringify(instr.tokens[1]) blob = b'' with open(filename, 'r') as f: csv_reader = csv.reader(f) for row in csv_reader: for column in row: value = self.parse_integer(column, 16, False) if value is None: raise LabelNotAllowed(instr) blob += pack_le16u(value) self.append_assembled_bytes(blob)
def _bl(self, instr): if not instr.match(LABEL): return None offset = instr.tokens[1] address = self.parse_integer(offset) if address: if not in_bit_range_signed(address, 23): raise NotInBitRange(address, 23) address >>= 1 address0 = address >> 11 address1 = address & 0x7ff return self._build_opcode(0b1111000000000000, ( (10, 0), address0)) + self._build_opcode( 0b1111100000000000, ((10, 0), address1)) self.add_label_translation(label=offset, bits=(10, 0), relative=True, size=2, offset=0, alignment=2, filter=lambda x: x >> 1, post_filter=lambda x: x >> 11, bits_check=23, start=self.current_org + self.org_counter + 4) self.add_label_translation( label=offset, bits=(10, 0), relative=True, size=2, offset=2, alignment=2, filter=lambda x: x >> 1, # get first 11 bits post_filter=lambda x: x & 0x7FF, bits_check=23, start=self.current_org + self.org_counter + 4) return pack_le16u(0b1111000000000000, 0b1111100000000000)
def _bl(self, instr): if not instr.match(LABEL): return None offset = instr.tokens[1] address = self.parse_integer(offset, 23, signed=True) if address: address >>= 1 address0 = address >> 11 address1 = address & 0x7ff return self._build_opcode(0b1111000000000000, ( (10, 0), address0)) + self._build_opcode( 0b1111100000000000, ((10, 0), address1)) self.add_label_translation( label=offset, size=2, bits_size=23, bits=(10, 0), alignment=2, relative=self.pc + 4, # get high 11 bits (after >> 1) filter=lambda x: x >> 12) self.add_label_translation( label=offset, offset=2, size=2, bits_size=23, bits=(10, 0), alignment=2, relative=self.pc + 4, # get low 11 bits (after >> 1) filter=lambda x: ((x >> 1) & 0x7FF)) return pack_le16u(0b1111000000000000, 0b1111100000000000)
def _Ov(instr, assembler, index, modrm): if match(instr.tokens[index:index+3], '[', None, ']'): if instr.tokens[index+1].upper() not in REGS8 + REGS16 + SEGMENTS: return 3, pack_le16u(assembler.parse_integer_or_label( instr.tokens[index+1], size=2, bits_size=16, offset=1))
def _Iv(instr, assembler, index, modrm): if instr.tokens[index].upper() not in REGS8 + REGS16 + SEGMENTS + ('[', ']'): return 1, pack_le16u(assembler.parse_integer_or_label( instr.tokens[index], size=2, bits_size=16, offset=1))
def _data16(self, arg): value = self.parse_integer(arg) # label ? if value is None: self.add_label_translation(label=arg, size=2) return pack_le16u(value)
def _data16(self, arg): value = self.parse_integer_or_label(label=arg, bits_size=16, offset=1, size=2) return pack_le16u(value)