porting an IAx86 disassembler

llee llee at goucher.edu
Mon Oct 8 14:37:43 PDT 2007


        I'm trying to port the distorm disassembly library over to D. The library was originally written in C, but I converted the header file into a D module. The library exports one main function distorm_decode (). This function is an alias for distorm_decode64(). This function takes an array of structures (DecodedInst). Each structure represents a single disassembled instruction. When the function is called it disassembles a code sequence and stores the results in this array. When I called this function it successfully disassembled the first instruction in the code sequence that I passed to it. Then it began acting erratically. In some of the remaining structures, the mnemonic string would be contained in the operand string, or the hex string would be in the operand string, etc. The data structure seemed to be jumbled.
        Source code illustrating a call to this function is given below, along with parameter information. The declaration is contained within an extern (C) block, and is located within an external module. 

The function declaration:
       ...
       _DecodeResult  distorm_decode64 
       (
	       _OffsetType codeOffset, 
	       ubyte *code, 
	       int codeLen, 
	       _DecodeType dt, 
	        _DecodedInst *result,
	        uint maxInstructions, 
	        uint *usedInstructionsCount
        );
        ...

The function call:
        ...
        dout.writeLine ("reading source file... ");
        ubyte[] source = cast (ubyte[]) std.file.read (commandline [1]);
        dout.writeLine ("source file read. ");

        _OffsetType			offset = 0;
        _DecodeType			decode_type = Decode32Bits;
        _DecodeResult 			decode_result;
        _DecodedInst[30]		decoded_instructions;
	uint decode_count;

	dout.writeLine ("disassembling source file... ");
	decode_result = distorm_decode64
	(
		offset				,
		&source[0]			,				
		source.length			,
		decode_type			,
		&decoded_instructions[0]	,
		decoded_instructions.length	,
		&decode_count
	);
        ...

The code used to display the results:
        ...
	foreach (_DecodedInst decoded_instruction; decoded_instructions)
	{
		dout.writefln ("instruction size: %s ", decoded_instruction.size);
		printf ("instruction Hex: %s \n", cast (char*) decoded_instruction.instructionHex.p);
		printf ("instruction mnemonic: %s \n", cast (char*) decoded_instruction.mnemonic.p);
		printf ("instruction operands: %s \n", cast (char*) decoded_instruction.operands.p);
	}
        ...

Any help would be appreciated. 



More information about the Digitalmars-d mailing list