Improved x64 documentation

This commit is contained in:
Eduard Urbach 2024-07-23 10:47:27 +02:00
parent eb160afd91
commit d6db1b1096
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 24 additions and 18 deletions

View File

@ -1,16 +1,19 @@
package x64 package x64
// AddressMode encodes the addressing mode.
type AddressMode = byte
const ( const (
AddressMemory = byte(0b00) AddressMemory = AddressMode(0b00)
AddressMemoryOffset8 = byte(0b01) AddressMemoryOffset8 = AddressMode(0b01)
AddressMemoryOffset32 = byte(0b10) AddressMemoryOffset32 = AddressMode(0b10)
AddressDirect = byte(0b11) AddressDirect = AddressMode(0b11)
) )
// ModRM is used to generate a ModRM suffix. // ModRM is used to generate a ModRM suffix.
// - mod: 2 bits // - mod: 2 bits. The addressing mode.
// - reg: 3 bits // - reg: 3 bits. Register reference or opcode extension.
// - rm: 3 bits // - rm: 3 bits. Register operand.
func ModRM(mod byte, reg byte, rm byte) byte { func ModRM(mod AddressMode, reg byte, rm byte) byte {
return (mod << 6) | (reg << 3) | rm return (mod << 6) | (reg << 3) | rm
} }

View File

@ -1,16 +1,19 @@
package x64 package x64
// ScaleFactor encodes the scale factor.
type ScaleFactor = byte
const ( const (
Scale1 = byte(0b00) Scale1 = ScaleFactor(0b00)
Scale2 = byte(0b01) Scale2 = ScaleFactor(0b01)
Scale4 = byte(0b10) Scale4 = ScaleFactor(0b10)
Scale8 = byte(0b11) Scale8 = ScaleFactor(0b11)
) )
// SIB is used to generate an SIB byte. // SIB is used to generate an SIB byte.
// - scale: 2 bits // - scale: 2 bits. Multiplies the value of the index.
// - index: 3 bits // - index: 3 bits. Specifies the index register.
// - base: 3 bits // - base: 3 bits. Specifies the base register.
func SIB(scale byte, index byte, base byte) byte { func SIB(scale ScaleFactor, index byte, base byte) byte {
return (scale << 6) | (index << 3) | base return (scale << 6) | (index << 3) | base
} }

View File

@ -3,7 +3,7 @@ package x64
import "git.akyoto.dev/cli/q/src/build/cpu" import "git.akyoto.dev/cli/q/src/build/cpu"
// encode is the core function that encodes an instruction. // encode is the core function that encodes an instruction.
func encode(code []byte, mod byte, reg cpu.Register, rm cpu.Register, numBytes byte, opCodes ...byte) []byte { func encode(code []byte, mod AddressMode, reg cpu.Register, rm cpu.Register, numBytes byte, opCodes ...byte) []byte {
w := byte(0) // Indicates a 64-bit register. w := byte(0) // Indicates a 64-bit register.
r := byte(0) // Extension to the "reg" field in ModRM. r := byte(0) // Extension to the "reg" field in ModRM.
x := byte(0) // Extension to the SIB index field. x := byte(0) // Extension to the SIB index field.

View File

@ -7,7 +7,7 @@ import (
) )
// encodeNum encodes an instruction with up to two registers and a number parameter. // encodeNum encodes an instruction with up to two registers and a number parameter.
func encodeNum(code []byte, mod byte, reg cpu.Register, rm cpu.Register, number int, opCode8 byte, opCode32 byte) []byte { func encodeNum(code []byte, mod AddressMode, reg cpu.Register, rm cpu.Register, number int, opCode8 byte, opCode32 byte) []byte {
if SizeOf(int64(number)) == 1 { if SizeOf(int64(number)) == 1 {
code = encode(code, mod, reg, rm, 8, opCode8) code = encode(code, mod, reg, rm, 8, opCode8)
return append(code, byte(number)) return append(code, byte(number))