Skip to content

Commit

Permalink
start decoding modr/m
Browse files Browse the repository at this point in the history
  • Loading branch information
xem committed Jan 7, 2017
1 parent c16281b commit ce6ea15
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
2 changes: 1 addition & 1 deletion instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
| | 83 /7 | | CMP | r/m16/32 | imm8 | | | | |
+--------+----------+---+----------+---------------+---------------+---------------+---------------+----------+-------+----------------------
| | 84 /r | | TEST | r/m8 | r8 | | | | |
| | 85 /r | | TEST | r/m16/32 | r16/32
| | 85 /r | | TEST | r/m16/32 | r16/32 | | | | |
+--------+----------+---+----------+---------------+---------------+---------------+---------------+----------+-------+----------------------
| | 86 /r | L | XCHG | r8 | r/m8 | | | | |
| | 87 /r | L | XCHG | r16/32 | r/m16/32 | | | | |
Expand Down
75 changes: 42 additions & 33 deletions src/disassembler.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ <h2>Mini x86 MS-DOS .COM disassembler</h2>
"90+r":"xchg r16/32, ax",
"99":"cwd",
"B0+r":"mov r8, imm8",
"B8+r":"mov r16/32, imm8",
"B8+r":"mov r16/32, imm16/32",
"C3":"retn",
"C3":"ret",
"CD":"int imm8",
"F7+6":"div r/m16/32",

};

Expand All @@ -80,21 +80,6 @@ <h2>Mini x86 MS-DOS .COM disassembler</h2>
td_hex.innerHTML = "";
td_asm.innerHTML = "";

// Current byte
current_byte = 0;

// current address
address = 0x100;

// Current instruction
instruction = {};

// Current instruction's hex code
hex = 0;

// Current instruction's asm code
asm = "";

// Stop disassembling (after a ret* instruction)
stop = 0;

Expand All @@ -103,14 +88,21 @@ <h2>Mini x86 MS-DOS .COM disassembler</h2>

// Reset instruction fields
instruction_address = address;
instruction = 0;
r = -1;
op1 = 0;
op2 = 0;
op3 = 0;
op4 = 0;
imm8 = 0;
imm16 = 0;
current_byte = null;
modrm_byte = null;
instruction = null;
r = null;
op1 = null;
op2 = null;
op3 = null;
op4 = null;
imm8 = null;
imm16 = null;
modrm_mod = null;
modrm_reg = null;
modrm_rm = null;
hex = null;
asm = null;

// Read one byte
current_byte = bytes[address];
Expand All @@ -124,16 +116,15 @@ <h2>Mini x86 MS-DOS .COM disassembler</h2>
// Else, disassemble real instructions
else{

// If the byte is present in the instructions table (in the form XX), use it
// If the byte is present in the instructions table (in the form XX), use it.
if(instructions[b16(current_byte)]){
asm = instructions[b16(current_byte)];
}

// Else, if the closest multiple of 8 is present in the table (in the form XX+r), use it and save r
// Else, if the previous multiple of 8 is present in the table (in the form XX+r), use it and save r.
else if(instructions[b16(current_byte & 0b11111000) + "+r"]){
asm = instructions[b16(current_byte & 0b11111000) + "+r"];
r = current_byte & 0b00000111;
//asm = asm.replace("r16/32",r16[r]);
}

// Else use a db instruction for all the next bytes
Expand All @@ -150,7 +141,7 @@ <h2>Mini x86 MS-DOS .COM disassembler</h2>

// Read r8
asm[operand] = asm[operand].replace("r8", function(a){
if(r != -1){
if(r !== null){
current_byte = r;
}
else{
Expand All @@ -163,9 +154,13 @@ <h2>Mini x86 MS-DOS .COM disassembler</h2>

// Read r16/32
asm[operand] = asm[operand].replace("r16/32", function(a){
if(r != -1){
if(r !== null){
current_byte = r;
}
else if(op2 !== null){
current_byte = op2;
op2 == null;
}
else {
address++;
current_byte = bytes[address];
Expand All @@ -179,7 +174,8 @@ <h2>Mini x86 MS-DOS .COM disassembler</h2>
address++;
current_byte = bytes[address];
hex += b16(current_byte) + " ";
return r16[current_byte];
modrm(current_byte);
return r16[op1];
});

// Read imm8
Expand Down Expand Up @@ -209,12 +205,12 @@ <h2>Mini x86 MS-DOS .COM disassembler</h2>
}

// Stop disassembling after a ret*
if(asm[0] == "retn"){
if(asm[0] == "ret"){
stop = 1;
}
}

// Write the address
// Write the address
td_address.innerHTML += b16(instruction_address + 0x100, 2) + "<br>";

// Write hex
Expand All @@ -226,6 +222,19 @@ <h2>Mini x86 MS-DOS .COM disassembler</h2>
}
}

modrm = function(current_byte){
modrm_mod = (current_byte >> 6) & 0b11;
modrm_reg = (current_byte >> 3) & 0b111;
modrm_rm = current_byte & 0b111;

// Use only general-purpose registers
if(modrm_mod == 0b11){
op1 = modrm_rm;
op2 = modrm_reg;
}

}

/** Helpers **/

// @param s: sign
Expand Down

0 comments on commit ce6ea15

Please sign in to comment.