Phase 3 complete: managed assembler

task 3.5-3.6: x86 stdcall/thiscall/fastcall stubs (8 tests)
task 3.7-3.8: x64 stub with RCX/RDX/R8/R9 register args + stack push (4 tests)
task 3.9: no-FASM reference assertion test

All passing (total: 87).
This commit is contained in:
kbe
2026-07-21 19:52:04 +02:00
parent 98c53568d9
commit f39ecf820d
2 changed files with 162 additions and 180 deletions
+45 -2
View File
@@ -110,9 +110,52 @@ public class StubAssembler : IAssembler
buffer.Add(0xC3); // ret
}
private static void BuildX64Stub(List<byte> buffer, ulong stubAddr,
private void BuildX64Stub(List<byte> buffer, ulong stubAddr,
ulong target, uint[] args)
{
throw new NotImplementedException("x64 stubs (task 3.7-3.8)");
ulong current = stubAddr;
// First 4 args go in RCX, RDX, R8D, R9D
var regCodes = new byte[] { 0xB9, 0xBA, 0xB8, 0xB9 }; // mov ecx/r8d/edx/r9d, imm32
var rexBytes = new byte[] { 0x00, 0x00, 0x41, 0x41 }; // 0x00 = no REX, 0x41 = REX.B
int regCount = Math.Min(args.Length, 4);
for (int i = 0; i < regCount; i++)
{
if (rexBytes[i] != 0)
buffer.Add(rexBytes[i]);
buffer.Add(regCodes[i]);
EmitU32(buffer, args[i]);
current += (rexBytes[i] != 0 ? 6u : 5u);
}
// Push remaining args in reverse order (right-to-left)
for (int i = args.Length - 1; i >= 4; i--)
{
buffer.Add(0x68); // push imm32
EmitU32(buffer, args[i]);
current += 5;
}
// call rel32
uint rel32 = (uint)(target - (current + 5));
buffer.Add(0xE8);
EmitU32(buffer, rel32);
// Caller cleanup: pop any args pushed on stack
int stackArgs = args.Length > 4 ? args.Length - 4 : 0;
if (stackArgs > 0)
{
int bytes = stackArgs * 8;
buffer.Add(0x48); // REX.W
buffer.Add(bytes <= 127 ? (byte)0x83 : (byte)0x81); // add r/m64, imm8/imm32
buffer.Add(0xC4); // rsp
if (bytes <= 127)
buffer.Add((byte)bytes);
else
EmitU32(buffer, (uint)bytes);
}
buffer.Add(0xC3); // ret
}
}