Improved x64 encoder
This commit is contained in:
parent
b776775f8f
commit
2c2b6e93db
15 changed files with 317 additions and 53 deletions
27
src/build/arch/x64/encode.go
Normal file
27
src/build/arch/x64/encode.go
Normal 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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue