Improved x64 encoder

This commit is contained in:
Eduard Urbach 2024-07-20 00:58:39 +02:00
parent b776775f8f
commit 2c2b6e93db
Signed by: eduard
GPG key ID: 49226B848C78F6C8
15 changed files with 317 additions and 53 deletions

View file

@ -0,0 +1,27 @@
package x64
// encode is the core function that encodes an instruction.
func encode(code []byte, w byte, mod byte, reg byte, rm byte, opCodes ...byte) []byte {
r := byte(0) // Extension to the "reg" field in ModRM.
x := byte(0) // Extension to the SIB index field.
b := byte(0) // Extension to the "rm" field in ModRM or the SIB base (r8 up to r15 use this).
if reg > 0b111 {
r = 1
reg &= 0b111
}
if rm > 0b111 {
b = 1
rm &= 0b111
}
if w != 0 || r != 0 || x != 0 || b != 0 {
code = append(code, REX(w, r, x, b))
}
code = append(code, opCodes...)
code = append(code, ModRM(mod, reg, rm))
return code
}