- Main Page
- Modules
- Data Structures
- Files

- Search for |

# Encoding Instructions

## Encoding

XED_DLL_EXPORT xed_error_enum_t | xed_encode (xed_encoder_request_t *r, xed_uint8_t *array, const unsigned int ilen, unsigned int *olen)
| This is the main interface to the encoder.
XED_DLL_EXPORT xed_error_enum_t | xed_encode_nop (xed_uint8_t *array, const unsigned int ilen)
| This function will attempt to encode a NOP of exactly ilen bytes.

## String Printing

XED_DLL_EXPORT void | xed_encode_request_print (const xed_encoder_request_t *p, char *buf, xed_uint_t buflen)

## Prefixes

XED_DLL_EXPORT void | xed_encoder_request_clear_rep (xed_encoder_request_t *p)
| clear the REP prefix indicator
XED_DLL_EXPORT void | xed_encoder_request_set_lock (xed_encoder_request_t *p)
| For locked (atomic read-modify-write) memops requests.
XED_DLL_EXPORT void | xed_encoder_request_set_rep (xed_encoder_request_t *p)
| for REP(F3) prefix on string ops
XED_DLL_EXPORT void | xed_encoder_request_set_repne (xed_encoder_request_t *p)
| for REPNE(F2) prefix on string ops

## Operand Order

XED_DLL_EXPORT xed_operand_enum_t | xed_encoder_request_get_operand_order (xed_encoder_request_t *p, xed_uint_t operand_index)
static XED_INLINE xed_uint_t | xed_encoder_request_operand_order_entries (xed_encoder_request_t *p)
| Retreive the number of entries in the encoder operand order array.
XED_DLL_EXPORT void | xed_encoder_request_set_operand_order (xed_encoder_request_t *p, xed_uint_t operand_index, xed_operand_enum_t name)

## Memory

XED_DLL_EXPORT void | xed_encoder_request_set_agen (xed_encoder_request_t *p)
XED_DLL_EXPORT void | xed_encoder_request_set_base0 (xed_encoder_request_t *p, xed_reg_enum_t base_reg)
XED_DLL_EXPORT void | xed_encoder_request_set_base1 (xed_encoder_request_t *p, xed_reg_enum_t base_reg)
XED_DLL_EXPORT void | xed_encoder_request_set_index (xed_encoder_request_t *p, xed_reg_enum_t index_reg)
XED_DLL_EXPORT void | xed_encoder_request_set_mem0 (xed_encoder_request_t *p)
XED_DLL_EXPORT void | xed_encoder_request_set_mem1 (xed_encoder_request_t *p)
XED_DLL_EXPORT void | xed_encoder_request_set_memory_displacement (xed_encoder_request_t *p, xed_int64_t memdisp, xed_uint_t nbytes)
XED_DLL_EXPORT void | xed_encoder_request_set_memory_operand_length (xed_encoder_request_t *p, xed_uint_t nbytes)
XED_DLL_EXPORT void | xed_encoder_request_set_scale (xed_encoder_request_t *p, xed_uint_t scale)
XED_DLL_EXPORT void | xed_encoder_request_set_seg0 (xed_encoder_request_t *p, xed_reg_enum_t seg_reg)
XED_DLL_EXPORT void | xed_encoder_request_set_seg1 (xed_encoder_request_t *p, xed_reg_enum_t seg_reg)

## branches and far pointers

XED_DLL_EXPORT void | xed_encoder_request_set_branch_displacement (xed_encoder_request_t *p, xed_int32_t brdisp, xed_uint_t nbytes)
XED_DLL_EXPORT void | xed_encoder_request_set_ptr (xed_encoder_request_t *p)
XED_DLL_EXPORT void | xed_encoder_request_set_relbr (xed_encoder_request_t *p)

## Primary Encode Functions

XED_DLL_EXPORT void | xed_encoder_request_set_effective_address_size (xed_encoder_request_t *p, xed_uint_t width_bits)
XED_DLL_EXPORT void | xed_encoder_request_set_effective_operand_width (xed_encoder_request_t *p, xed_uint_t width_bits)
XED_DLL_EXPORT void | xed_encoder_request_set_reg (xed_encoder_request_t *p, xed_operand_enum_t operand, xed_reg_enum_t reg)

## Immediates

XED_DLL_EXPORT void | xed_encoder_request_set_simm (xed_encoder_request_t *p, xed_int32_t simm, xed_uint_t nbytes)
| same storage as uimm0
XED_DLL_EXPORT void | xed_encoder_request_set_uimm0 (xed_encoder_request_t *p, xed_uint64_t uimm, xed_uint_t nbytes)
| Set the uimm0 using a BYTE width.
XED_DLL_EXPORT void | xed_encoder_request_set_uimm0_bits (xed_encoder_request_t *p, xed_uint64_t uimm, xed_uint_t nbits)
| Set the uimm0 using a BIT width.
XED_DLL_EXPORT void | xed_encoder_request_set_uimm1 (xed_encoder_request_t *p, xed_uint8_t uimm)

## Initialization

XED_DLL_EXPORT void | xed_encoder_request_zero (xed_encoder_request_t *p)
XED_DLL_EXPORT void | xed_encoder_request_zero_operand_order (xed_encoder_request_t *p)
XED_DLL_EXPORT void | xed_encoder_request_zero_set_mode (xed_encoder_request_t *p, const xed_state_t *dstate)

## Typedefs

typedef xed_decoded_inst_s | xed_encoder_request_s
typedef xed_decoded_inst_t | xed_encoder_request_t

## Functions

XED_DLL_EXPORT xed_iclass_enum_t | xed_encoder_request_get_iclass (const xed_encoder_request_t *p)
XED_DLL_EXPORT void | xed_encoder_request_init_from_decode (struct xed_decoded_inst_s *d)
| Converts an decoder request to a valid encoder request.
XED_DLL_EXPORT xed_operand_values_t * | xed_encoder_request_operands (xed_encoder_request_t *p)
XED_DLL_EXPORT const xed_operand_values_t * | xed_encoder_request_operands_const (const xed_encoder_request_t *p)
XED_DLL_EXPORT void | xed_encoder_request_set_iclass (xed_encoder_request_t *p, xed_iclass_enum_t iclass)

## Detailed Description

When you call xed_encode() to encode instruction you must pass:

- an encode structure that includes a machine state ( xed_state_t )
- a pointer to the instruction text
- a length of the text array

The class xed_encoder_request_t includes a xed_operand_values_t and that is where most of the information about the operands, resources etc. are stored.

To add a LOCK, REP or REPNE prefix to a an encoder request, set it in the encoder request directly by calling xed_encoder_request_set_lock(), xed_encoder_request_set_rep() or xed_encoder_request_set_repne().

To get nondefault width operands, during encoding, you have to call xed_encoder_request_set_effective_operand_width() .

To set nondefault addressing widths, you must call xed_encoder_request_set_effective_address_size().

To encode instructions you must set the following in the xed_encoder_request_t.

1. the machine mode (machine width, and stack addressing width)
2. the effective operand width
3. the iclass
4. for some instructions you need to specify prefixes (like REP, REPNE or LOCK).
5. the operands:
   1. operand kind (XED_OPERAND_{AGEN,MEM0,MEM1,IMM0,IMM1,RELBR,PTR,REG0...REG15}

   2. operand order
      xed_encoder_request_set_operand_order(&req,operand_index, XED_OPERAND_*); where the operand_index is a sequential index starting at zero.

   3. operand details
      1. FOR MEMOPS: base,segment,index,scale,displacement for memops,
      2. FOR REGISTERS: register name
      3. FOR IMMEDIATES: immediate values

See An example of using the encoder for an example of using the encoder.

## Typedef Documentation

typedef struct xed_decoded_inst_s xed_encoder_request_s

| Definition at line 51 of file xed-encode.h.

typedef xed_decoded_inst_t xed_encoder_request_t

| Definition at line 53 of file xed-encode.h.

## Function Documentation

XED_DLL_EXPORT xed_error_enum_t xed_encode | ( | xed_encoder_request_t * | r,
|  | xed_uint8_t * | array,
|  | const unsigned int | ilen,
|  | unsigned int * | olen
| ) |

| This is the main interface to the encoder. The array should be at most 15 bytes long. The ilen parameter should indiciate this length. If the array is too short, the encoder may fail to encode the request. Failure is indicated by a return value of type xed_error_enum_t that is not equal to XED_ERROR_NONE. Otherwise, XED_ERROR_NONE is returned and the length of the encoded instruction is returned in olen.

Parameters:

  | r | encoder request description (xed_encoder_request_t), includes mode info
  | array | the encoded instruction bytes are stored here
  | ilen | the input length of array.
  | olen | the actual length of array used for encoding

Returns:
  success/failure as a xed_error_enum_t

XED_DLL_EXPORT xed_error_enum_t xed_encode_nop | ( | xed_uint8_t * | array,
|  | const unsigned int | ilen
| ) |

| This function will attempt to encode a NOP of exactly ilen bytes. If such a NOP is not encodeable, then false will be returned.

Parameters:

  | array | the encoded instruction bytes are stored here
  | ilen | the input length array.

Returns:
  success/failure as a xed_error_enum_t

XED_DLL_EXPORT void xed_encode_request_print | ( | const xed_encoder_request_t * | p,
|  | char * | buf,
|  | xed_uint_t | buflen
| ) |

XED_DLL_EXPORT void xed_encoder_request_clear_rep | ( | xed_encoder_request_t * | p | ) |

| clear the REP prefix indicator

XED_DLL_EXPORT xed_iclass_enum_t xed_encoder_request_get_iclass | ( | const xed_encoder_request_t * | p | ) |

XED_DLL_EXPORT xed_operand_enum_t xed_encoder_request_get_operand_order | ( | xed_encoder_request_t * | p,
|  | xed_uint_t | operand_index
| ) |

| Retreive the name of the n'th operand in the operand order.

Parameters:

  `[in]` | p | xed_encoder_request_t
  `[in]` | operand_index | xed_uint_t representing n'th operand position

Returns:
  The xed_operand_enum_t operand name.

XED_DLL_EXPORT void xed_encoder_request_init_from_decode | ( | struct xed_decoded_inst_s * | d | ) |

| Converts an decoder request to a valid encoder request.

static XED_INLINE xed_uint_t xed_encoder_request_operand_order_entries | ( | xed_encoder_request_t * | p | ) | `[static]`

| Retreive the number of entries in the encoder operand order array.

Returns:
  The number of entries in the encoder operand order array

Definition at line 139 of file xed-encode.h.

XED_DLL_EXPORT xed_operand_values_t* xed_encoder_request_operands | ( | xed_encoder_request_t * | p | ) |

XED_DLL_EXPORT const xed_operand_values_t* xed_encoder_request_operands_const | ( | const xed_encoder_request_t * | p | ) |

XED_DLL_EXPORT void xed_encoder_request_set_agen | ( | xed_encoder_request_t * | p | ) |

XED_DLL_EXPORT void xed_encoder_request_set_base0 | ( | xed_encoder_request_t * | p,
|  | xed_reg_enum_t | base_reg
| ) |

XED_DLL_EXPORT void xed_encoder_request_set_base1 | ( | xed_encoder_request_t * | p,
|  | xed_reg_enum_t | base_reg
| ) |

XED_DLL_EXPORT void xed_encoder_request_set_branch_displacement | ( | xed_encoder_request_t * | p,
|  | xed_int32_t | brdisp,
|  | xed_uint_t | nbytes
| ) |

XED_DLL_EXPORT void xed_encoder_request_set_effective_address_size | ( | xed_encoder_request_t * | p,
|  | xed_uint_t | width_bits
| ) |

XED_DLL_EXPORT void xed_encoder_request_set_effective_operand_width | ( | xed_encoder_request_t * | p,
|  | xed_uint_t | width_bits
| ) |

XED_DLL_EXPORT void xed_encoder_request_set_iclass | ( | xed_encoder_request_t * | p,
|  | xed_iclass_enum_t | iclass
| ) |

XED_DLL_EXPORT void xed_encoder_request_set_index | ( | xed_encoder_request_t * | p,
|  | xed_reg_enum_t | index_reg
| ) |

XED_DLL_EXPORT void xed_encoder_request_set_lock | ( | xed_encoder_request_t * | p | ) |

| For locked (atomic read-modify-write) memops requests.

XED_DLL_EXPORT void xed_encoder_request_set_mem0 | ( | xed_encoder_request_t * | p | ) |

XED_DLL_EXPORT void xed_encoder_request_set_mem1 | ( | xed_encoder_request_t * | p | ) |

XED_DLL_EXPORT void xed_encoder_request_set_memory_displacement | ( | xed_encoder_request_t * | p,
|  | xed_int64_t | memdisp,
|  | xed_uint_t | nbytes
| ) |

XED_DLL_EXPORT void xed_encoder_request_set_memory_operand_length | ( | xed_encoder_request_t * | p,
|  | xed_uint_t | nbytes
| ) |

XED_DLL_EXPORT void xed_encoder_request_set_operand_order | ( | xed_encoder_request_t * | p,
|  | xed_uint_t | operand_index,
|  | xed_operand_enum_t | name
| ) |

| Specify the name as the n'th operand in the operand order. The complication of this function is that the register operand names are specific to the position of the operand (REG0, REG1, REG2...). One can use this function for registers or one can use the xed_encoder_request_set_operand_name_reg() which takes integers instead of operand names.

Parameters:

  `[in]` | p | xed_encoder_request_t
  `[in]` | operand_index | xed_uint_t representing n'th operand position
  `[in]` | name | xed_operand_enum_t operand name.

XED_DLL_EXPORT void xed_encoder_request_set_ptr | ( | xed_encoder_request_t * | p | ) |

XED_DLL_EXPORT void xed_encoder_request_set_reg | ( | xed_encoder_request_t * | p,
|  | xed_operand_enum_t | operand,
|  | xed_reg_enum_t | reg
| ) |

| Set the operands array element indexed by operand to the actual register name reg.

Parameters:

  `[in]` | p | xed_encoder_request_t
  `[in]` | operand | indicates which register operand storage field to use
  `[in]` | reg | the actual register represented (EAX, etc.) to store.

XED_DLL_EXPORT void xed_encoder_request_set_relbr | ( | xed_encoder_request_t * | p | ) |

XED_DLL_EXPORT void xed_encoder_request_set_rep | ( | xed_encoder_request_t * | p | ) |

| for REP(F3) prefix on string ops

XED_DLL_EXPORT void xed_encoder_request_set_repne | ( | xed_encoder_request_t * | p | ) |

| for REPNE(F2) prefix on string ops

XED_DLL_EXPORT void xed_encoder_request_set_scale | ( | xed_encoder_request_t * | p,
|  | xed_uint_t | scale
| ) |

XED_DLL_EXPORT void xed_encoder_request_set_seg0 | ( | xed_encoder_request_t * | p,
|  | xed_reg_enum_t | seg_reg
| ) |

XED_DLL_EXPORT void xed_encoder_request_set_seg1 | ( | xed_encoder_request_t * | p,
|  | xed_reg_enum_t | seg_reg
| ) |

XED_DLL_EXPORT void xed_encoder_request_set_simm | ( | xed_encoder_request_t * | p,
|  | xed_int32_t | simm,
|  | xed_uint_t | nbytes
| ) |

| same storage as uimm0

XED_DLL_EXPORT void xed_encoder_request_set_uimm0 | ( | xed_encoder_request_t * | p,
|  | xed_uint64_t | uimm,
|  | xed_uint_t | nbytes
| ) |

| Set the uimm0 using a BYTE width.

XED_DLL_EXPORT void xed_encoder_request_set_uimm0_bits | ( | xed_encoder_request_t * | p,
|  | xed_uint64_t | uimm,
|  | xed_uint_t | nbits
| ) |

| Set the uimm0 using a BIT width.

XED_DLL_EXPORT void xed_encoder_request_set_uimm1 | ( | xed_encoder_request_t * | p,
|  | xed_uint8_t | uimm
| ) |

XED_DLL_EXPORT void xed_encoder_request_zero | ( | xed_encoder_request_t * | p | ) |

XED_DLL_EXPORT void xed_encoder_request_zero_operand_order | ( | xed_encoder_request_t * | p | ) |

| clear the operand order array

Parameters:

  `[in]` | p | xed_encoder_request_t

XED_DLL_EXPORT void xed_encoder_request_zero_set_mode | ( | xed_encoder_request_t * | p,
|  | const xed_state_t * | dstate
| ) |