Skip to content

Instantly share code, notes, and snippets.

@toptensoftware
Last active October 8, 2016 23:57
Show Gist options
  • Select an option

  • Save toptensoftware/98c6299487b3b7127a019fd3cf8b17ad to your computer and use it in GitHub Desktop.

Select an option

Save toptensoftware/98c6299487b3b7127a019fd3cf8b17ad to your computer and use it in GitHub Desktop.
List<Action> _systemThunkHanders = new List<Action>();
public uint CreateSystemThunk(Action handler, ushort popStack)
{
// Capture address of this thunk
ushort address = _systemCodeGenPos;
// Store the handler
ushort thunkIndex = (ushort)_systemThunkHanders.Count;
_systemThunkHanders.Add(handler);
// Get memory buffer
byte[] mem = _globalHeap.GetBuffer(_systemCodeSelector);
// MOV AX, thunk index
mem[_systemCodeGenPos++] = 0xb8;
mem[_systemCodeGenPos++] = (byte)(thunkIndex & 0xFF);
mem[_systemCodeGenPos++] = (byte)(thunkIndex >> 8);
// INT 80h
mem[_systemCodeGenPos++] = 0xCD;
mem[_systemCodeGenPos++] = 0x80;
// RETF
mem[_systemCodeGenPos++] = 0xCA;
mem[_systemCodeGenPos++] = (byte)(popStack & 0xFF);
mem[_systemCodeGenPos++] = (byte)(popStack >> 8);
// Return seg:offset address of thunk
return (uint)(_systemCodeSelector << 16 | address);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment