node/lib/metasm/metasm/cpu/ia32/encode.rb
def encode16(reg, endianness)
			if not b
				# imm only
				return [EncodedData.new << (6 | (reg << 3)) << @imm.encode(:u16, endianness)]
			end

			imm = @imm.reduce if self.imm
			imm = nil if imm == 0
			ret = EncodedData.new
			ret <<
			case [@b.val, (@i.val if i)]
			when [3, 6], [6, 3]; 0
			when [3, 7], [7, 3]; 1
			when [5, 6], [6, 5]; 2
			when [5, 7], [7, 5]; 3
			when [6, nil]; 4
			when [7, nil]; 5
			when [5, nil]
				imm ||= 0
				6
			when [3, nil]; 7
			else raise InvalidModRM, 'invalid modrm16'
			end

			# add bits in the first octet of ret.data (1.9 compatibility layer)
			or_bits = lambda { |v|	# rape me
				if ret.data[0].kind_of? Integer
					ret.data[0] |= v
				else
					ret.data[0] = (ret.data[0].unpack('C').first | v).chr
				end
			}

			or_bits[reg << 3]

			if imm
				case Expression.in_range?(imm, :i8)
				when true
					or_bits[1 << 6]
					[ret << Expression.encode_imm(imm, :i8, endianness)]
				when false
					or_bits[2 << 6]
					[ret << Expression.encode_imm(imm, :a16, endianness)]
				when nil
					rets = ret.dup
					or_bits[1<<6]
					ret << @imm.encode(:i8, endianness)
					ret, rets = rets, ret	# or_bits uses ret
					or_bits[2<<6]
					ret << @imm.encode(:a16, endianness)
					[ret, rets]
				end
			else
				[ret]
			end
		end
# returns the byte representing the register encoded as modrm
# works with Reg/SimdReg
returns the byte representing the register encoded as modrm
works with Reg/SimdReg
returns the byte representing the register encoded as modrm works with Reg / SimdReg
def encode16(reg, endianness)
			if not b
				return [EncodedData.new << (6 | (reg << 3)) << @imm.encode(:u16, endianness)]
			end
			imm = @imm.reduce if self.imm
			imm = nil if imm == 0
			ret = EncodedData.new
			ret <<
			case [@b.val, (@i.val if i)]
			when [3, 6], [6, 3]; 0
			when [3, 7], [7, 3]; 1
			when [5, 6], [6, 5]; 2
			when [5, 7], [7, 5]; 3
			when [6, nil]; 4
			when [7, nil]; 5
			when [5, nil]
				imm ||= 0
				6
			when [3, nil]; 7
			else raise InvalidModRM, 'invalid modrm16'
			end
			or_bits = lambda { |v|	
				if ret.data[0].kind_of? Integer
					ret.data[0] |= v
				else
					ret.data[0] = (ret.data[0].unpack('C').first | v).chr
				end
			}
			or_bits[reg << 3]
			if imm
				case Expression.in_range?(imm, :i8)
				when true
					or_bits[1 << 6]
					[ret << Expression.encode_imm(imm, :i8, endianness)]
				when false
					or_bits[2 << 6]
					[ret << Expression.encode_imm(imm, :a16, endianness)]
				when nil
					rets = ret.dup
					or_bits[1<<6]
					ret << @imm.encode(:i8, endianness)
					ret, rets = rets, ret	
					or_bits[2<<6]
					ret << @imm.encode(:a16, endianness)
					[ret, rets]
				end
			else
				[ret]
			end
		end
def encode16 ( reg , endianness ) if not b return [ EncodedData . new << ( 6 | ( reg << 3 ) ) << @imm . encode ( :u16 , endianness ) ] end imm = @imm . reduce if self . imm imm = nil if imm == 0 ret = EncodedData . new ret << case [ @b . val , ( @i . val if i ) ] when [ 3 , 6 ] , [ 6 , 3 ] ; 0 when [ 3 , 7 ] , [ 7 , 3 ] ; 1 when [ 5 , 6 ] , [ 6 , 5 ] ; 2 when [ 5 , 7 ] , [ 7 , 5 ] ; 3 when [ 6 , nil ] ; 4 when [ 7 , nil ] ; 5 when [ 5 , nil ] imm ||= 0 6 when [ 3 , nil ] ; 7 else raise InvalidModRM , 'invalid modrm16' end or_bits = lambda { | v | if ret . data [ 0 ] . kind_of? Integer ret . data [ 0 ] |= v else ret . data [ 0 ] = ( ret . data [ 0 ] . unpack ( 'C' ) . first | v ) . chr end } or_bits [ reg << 3 ] if imm case Expression . in_range? ( imm , :i8 ) when true or_bits [ 1 << 6 ] [ ret << Expression . encode_imm ( imm , :i8 , endianness ) ] when false or_bits [ 2 << 6 ] [ ret << Expression . encode_imm ( imm , :a16 , endianness ) ] when nil rets = ret . dup or_bits [ 1 << 6 ] ret << @imm . encode ( :i8 , endianness ) ret , rets = rets , ret or_bits [ 2 << 6 ] ret << @imm . encode ( :a16 , endianness ) [ ret , rets ] end else [ ret ] end end
returns the byte representing the register encoded as modrm
works with Reg/SimdReg
returns the byte representing the register encoded as modrm works with Reg / SimdReg
# imm only # add bits in the first octet of ret.data (1.9 compatibility layer) # rape me # or_bits uses ret
reg
endianness