GiveMeZeny / fde32

Extended Length Disassembler Engine for x86 (1225 bytes in size)
BSD 2-Clause "Simplified" License
9 stars 6 forks source link

un[f]ancy [d]isassembler [e]ngine (fde32) v0.01

fde32 is an extended length disassembler engine it's written in x86 asm for use with x86 instructions and supports the following instruction sets:

how to compile:

use fasm (http://flatassembler.net/) to compile encode.asm and decode.asm, place the bin-files into bin/ and compile fde32.asm to a x86 coff obj-file. you can also compile and exec packtbl.c and redirect its output into a new decode.asm to get one that is a little smaller.

how to use:

int encode(void *dest, fde32s *cmd);
int decode(void *src,  fde32s *cmd);

mov     edx,fde32s_struct
mov     ecx,[destination]
call    encode

mov     edx,fde32s_struct
mov     ecx,[source]
call    decode

encode returns the number of bytes written to the buffer decode returns either 1 if the instruction is valid or 0 if any error-flag is set both functions preserve all registers but eax

fde32s-struct explanation:

len contains the instruction's size prefix specifies the prefix flags (PRE_LOCK, PRE_REP, PRE_SEG, PRE_66, PRE_67, PRE_VEX) prefix.lock equal to PREFIX_LOCK if PRE_LOCK is set, zero elsewise prefix.rep equal to PREFIX_REPNZ/PREFIX_REP if PRE_REP is set, zero elsewise prefix.seg equal to PREFIX_SEGMENT_XX if PRE_SEG is set, zero elsewise prefix.66 equal to PREFIX_OPERAND_SIZE if PRE_66 is set, zero elsewise prefix.67 equal to PREFIX_ADDRESS_SIZE if PRE_67 is set, zero elsewise vex vex escape-byte (C4h/C5h) vex2 full second vex-byte vex3 full third vex-byte vex.r extracted r-component of vex-byte vex.x extracted x-component of vex-byte vex.b extracted b-component of vex-byte vex.m_mmmm extracted m_mmmm-component of vex-byte vex.w extracted w-component of vex-byte vex.vvvv extracted vvvv-component of vex-byte vex.l extracted l-component of vex-byte vex.pp extracted pp-component of vex-byte opcode.len amount of opcode-bytes (1-3, necessary prefixes for various SSE instruction are not counted in) opcode first opcode opcode2 second opcode opcode3 third opcode modrm full modr/m-byte modrm.mod extracted mod-component of modr/m-byte modrm.reg extracted reg-component of modr/m-byte modrm.rm extracted rm-component of modr/m-byte sib full sib-byte sib.scale extracted scale-component of sib-byte sib.index extracted index-component of sib-byte sib.base extracted base-component of sib-byte dispXX displacement, check flags for size immXX immediate, check flags for size immXX_2 second immediate, only relevant to call/jmp ptr16:32 and enter imm16, imm8 flags see below

flags explanation:

F_MODRM, F_SIB, F_DISP8/16/32, F_IMM8/16/32 self-explanatory F_RELATIVE is for instructions with RIP-relative immediates like calls and jmps F_GROUP means modrm.reg is used as an opcode extension F_VEX_BAD_PREFIX error-flag: an avx instruction has illegally either a opsize-override-, rep- or lock-prefix F_ERROR_LOCK error-flag: the disassembled instruction is not allowed to have a lock-prefix F_ERROR_LENGTH error-flag: the instruction-size limit of 15 bytes is exceeded (the said instruction will cause an exception) F_ERROR_OPCODE error-flag: the disassembled opcode is undefined (aborts forthwith, so far disassembled prefixes are kept)

if any of those error-flags is set, the disassembled instruction will #UD

notes:

version 0.01

[!] Initial release (ported fde64 to x86 in like 15mins, may contain even more bugs)