Skip to content
Permalink
Browse files

Move emitters to x86 folder

  • Loading branch information...
charlesbetros committed Sep 5, 2019
1 parent 5d7adf5 commit e5393c1ef460bdb34118008e0d0baf43e185e656
@@ -0,0 +1,143 @@
; namespace DebugStub

; Location where INT3 has been injected.
; 0 if no INT3 is active.
; var AsmBreakEIP
DebugStub_AsmBreakEIP dd 0

; Old byte before INT3 was injected.
; Only 1 byte is used.
; var AsmOrigByte
DebugStub_AsmOrigByte dd 0

; function DoAsmBreak {
DebugStub_DoAsmBreak:
; Since our Int3 is temp, we need to adjust return EIP to return to it, not after it.
; ESI = .CallerESP
Mov ESI, DWORD [DebugStub_CallerESP]
; EAX = .AsmBreakEIP
Mov EAX, DWORD [DebugStub_AsmBreakEIP]
; [ESI-12] = EAX
Mov DWORD [ESI - 12], EAX

; ClearAsmBreak()
Call DebugStub_ClearAsmBreak
; Break()
Call DebugStub_Break
; }
DebugStub_DoAsmBreak_Exit:
Ret

; function SetAsmBreak {
DebugStub_SetAsmBreak:
; ClearAsmBreak()
Call DebugStub_ClearAsmBreak

; ComReadEAX()
Call DebugStub_ComReadEAX
; Save EIP of the break
; .AsmBreakEIP = EAX
Mov DWORD [DebugStub_AsmBreakEIP], EAX
; EDI = EAX
Mov EDI, EAX

; Save the old byte
; AL = [EDI]
Mov AL, BYTE [EDI]
; .AsmOrigByte = AL
Mov BYTE [DebugStub_AsmOrigByte], AL

; Inject INT3
; Do in 2 steps to force a byte move to RAM (till X# can do byte in one step)
; AL = $CC
Mov AL, 0xCC
; [EDI] = AL
Mov BYTE [EDI], AL
; }
DebugStub_SetAsmBreak_Exit:
Ret

; function ClearAsmBreak {
DebugStub_ClearAsmBreak:
; EDI = .AsmBreakEIP
Mov EDI, DWORD [DebugStub_AsmBreakEIP]
; If 0, we don't need to clear an older one.
; if EDI = 0 return
; Clear old break point and set back to original opcode / partial opcode
; AL = .AsmOrigByte
Mov AL, BYTE [DebugStub_AsmOrigByte]
; [EDI] = AL
Mov BYTE [EDI], AL

; .AsmBreakEIP = 0
Mov DWORD [DebugStub_AsmBreakEIP], 0x0
; }
DebugStub_ClearAsmBreak_Exit:
Ret

; function SetINT1_TrapFLAG {
DebugStub_SetINT1_TrapFLAG:
; Push EAX to make sure whatever we do below doesn't affect code afterwards
; +EBP
Push EBP
; +EAX
Push EAX

; Set base pointer to the caller ESP
; EBP = .CallerESP
Mov EBP, DWORD [DebugStub_CallerESP]
; Set the Trap Flag (http://en.wikipedia.org/wiki/Trap_flag)
; For EFLAGS we want - the interrupt frame = ESP + 12
; - The interrupt frame - 8 for correct byte = ESP + 12 - 8 = ESP + 4
; - Therefore, ESP - 4 to get to the correct position
; EBP -= 4
Sub EBP, 0x4
; EAX = [EBP]
Mov EAX, DWORD [EBP]
; EAX | $0100
; [EBP] = EAX
Mov DWORD [EBP], EAX

; Restore the base pointer
; Pop EAX - see +EAX at start of method
; -EAX
Pop EAX
; -EBP
Pop EBP
; }
DebugStub_SetINT1_TrapFLAG_Exit:
Ret

; function ResetINT1_TrapFLAG {
DebugStub_ResetINT1_TrapFLAG:
; Push EAX to make sure whatever we do below doesn't affect code afterwards
; +EBP
Push EBP
; +EAX
Push EAX

; Set base pointer to the caller ESP
; EBP = .CallerESP
Mov EBP, DWORD [DebugStub_CallerESP]
; Clear the Trap Flag (http://en.wikipedia.org/wiki/Trap_flag)
; See comment in SetINT1_TrapFlag
; EBP -= 4
Sub EBP, 0x4
; EAX = [EBP]
Mov EAX, DWORD [EBP]
; EAX & $FEFF
; [EBP] = EAX
Mov DWORD [EBP], EAX
; Pop EAX - see +EAX at start of method
; -EAX
Pop EAX
; -EBP
Pop EBP
; }
DebugStub_ResetINT1_TrapFLAG_Exit:
Ret
@@ -118,17 +118,17 @@ public Compiler(TextWriter aOut)
mNASM = new NASM(aOut);

mTokenMap = new Spruce.Tokens.Root();
mTokenMap.AddEmitter(new Emitters.Namespace(this, mNASM));
mTokenMap.AddEmitter(new Emitters.Comments(this, mNASM));
mTokenMap.AddEmitter(new Emitters.Ports(this, mNASM));
mTokenMap.AddEmitter(new Emitters.ZeroParamOps(this, mNASM)); // This should be above push/pop
mTokenMap.AddEmitter(new Emitters.IncrementDecrement(this, mNASM)); // This should be above + operator
mTokenMap.AddEmitter(new Emitters.PushPop(this, mNASM)); // This should be above + operator
mTokenMap.AddEmitter(new Emitters.Assignments(this, mNASM));
mTokenMap.AddEmitter(new Emitters.Test(this, mNASM));
mTokenMap.AddEmitter(new Emitters.Math(this, mNASM));
mTokenMap.AddEmitter(new Emitters.ShiftRotate(this, mNASM));
mTokenMap.AddEmitter(new Emitters.AllEmitters(this, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.Namespace(this, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.Comments(this, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.Ports(this, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.ZeroParamOps(this, mNASM)); // This should be above push/pop
mTokenMap.AddEmitter(new x86.Emitters.IncrementDecrement(this, mNASM)); // This should be above + operator
mTokenMap.AddEmitter(new x86.Emitters.PushPop(this, mNASM)); // This should be above + operator
mTokenMap.AddEmitter(new x86.Emitters.Assignments(this, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.Test(this, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.Math(this, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.ShiftRotate(this, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.AllEmitters(this, mNASM));
}

public void WriteLine(string aText = "")
@@ -4,7 +4,7 @@
using XSharp.Tokens;
using XSharp.x86;

namespace XSharp.Emitters
namespace XSharp.x86.Emitters
{
// Emitters does the actual translation from X# (via Spruce) to x86 (via Assemblers)
public class AllEmitters : Emitters
@@ -8,7 +8,7 @@
using Reg16 = XSharp.Tokens.Reg16;
using Reg32 = XSharp.Tokens.Reg32;

namespace XSharp.Emitters
namespace XSharp.x86.Emitters
{
/// <summary>
/// The class that provides assignments for different types.
@@ -2,7 +2,7 @@
using Spruce.Tokens;
using XSharp.Tokens;

namespace XSharp.Emitters
namespace XSharp.x86.Emitters
{
/// <summary>
/// Class that processes comments and literals for X#.
@@ -1,4 +1,4 @@
namespace XSharp.Emitters
namespace XSharp.x86.Emitters
{
public abstract class Emitters
{
@@ -3,7 +3,7 @@
using XSharp.Tokens;
using XSharp.x86;

namespace XSharp.Emitters
namespace XSharp.x86.Emitters
{
/// <summary>
/// Class that performs increment and decrement assmebly operations
@@ -8,7 +8,7 @@
using Reg16 = XSharp.Tokens.Reg16;
using Reg32 = XSharp.Tokens.Reg32;

namespace XSharp.Emitters
namespace XSharp.x86.Emitters
{
/// <summary>
/// The class that provides arithmetic for different types.
@@ -2,7 +2,7 @@
using Spruce.Tokens;
using XSharp.Tokens;

namespace XSharp.Emitters
namespace XSharp.x86.Emitters
{
/// <summary>
/// Namespaces for X# files. All files must start with a namespace.
@@ -3,7 +3,7 @@
using XSharp.Tokens;
using XSharp.x86;

namespace XSharp.Emitters
namespace XSharp.x86.Emitters
{
/// <summary>
/// IN and OUT functions from assembly <br/>
@@ -5,7 +5,7 @@
using XSharp.x86.Params;
using Reg = XSharp.Tokens.Reg;

namespace XSharp.Emitters
namespace XSharp.x86.Emitters
{
/// <summary>
/// Push and Pop values
@@ -3,7 +3,7 @@
using XSharp.Tokens;
using XSharp.x86;

namespace XSharp.Emitters
namespace XSharp.x86.Emitters
{
/// <summary>
/// Class that performs shift and rotate assmebly operations
@@ -2,7 +2,7 @@
using XSharp.Tokens;
using XSharp.x86;

namespace XSharp.Emitters
namespace XSharp.x86.Emitters
{
/// <summary>
/// TEST: Logical compare
@@ -2,7 +2,7 @@
using XSharp.Tokens;
using XSharp.x86;

namespace XSharp.Emitters
namespace XSharp.x86.Emitters
{
/// <summary>
/// Operations without any parameters
@@ -15,15 +15,15 @@ public TokenMap_Should()
mCompiler = new Compiler(TextWriter.Null);
mNASM = new NASM(TextWriter.Null);
mTokenMap = new Spruce.Tokens.Root();
mTokenMap.AddEmitter(new Emitters.Namespace(mCompiler, mNASM));
mTokenMap.AddEmitter(new Emitters.Comments(mCompiler, mNASM));
mTokenMap.AddEmitter(new Emitters.Ports(mCompiler, mNASM));
mTokenMap.AddEmitter(new Emitters.ZeroParamOps(mCompiler, mNASM)); // This should be above push/pop
mTokenMap.AddEmitter(new Emitters.IncrementDecrement(mCompiler, mNASM)); // This should be above + operator
mTokenMap.AddEmitter(new Emitters.PushPop(mCompiler, mNASM)); // This should be above + operator
mTokenMap.AddEmitter(new Emitters.Assignments(mCompiler, mNASM));
mTokenMap.AddEmitter(new Emitters.Test(mCompiler, mNASM));
mTokenMap.AddEmitter(new Emitters.AllEmitters(mCompiler, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.Namespace(mCompiler, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.Comments(mCompiler, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.Ports(mCompiler, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.ZeroParamOps(mCompiler, mNASM)); // This should be above push/pop
mTokenMap.AddEmitter(new x86.Emitters.IncrementDecrement(mCompiler, mNASM)); // This should be above + operator
mTokenMap.AddEmitter(new x86.Emitters.PushPop(mCompiler, mNASM)); // This should be above + operator
mTokenMap.AddEmitter(new x86.Emitters.Assignments(mCompiler, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.Test(mCompiler, mNASM));
mTokenMap.AddEmitter(new x86.Emitters.AllEmitters(mCompiler, mNASM));
}

[Test]

0 comments on commit e5393c1

Please sign in to comment.
You can’t perform that action at this time.