Implemented subtraction

This commit is contained in:
2024-06-24 16:55:15 +02:00
parent 597cb9abed
commit b018d8de61
9 changed files with 92 additions and 28 deletions

View File

@ -1,18 +0,0 @@
package x64
import "git.akyoto.dev/cli/q/src/build/cpu"
// AddRegNum8 adds a byte to the given register.
func AddRegNum8(code []byte, destination cpu.Register, number uint8) []byte {
if destination >= 8 {
code = append(code, REX(0, 0, 0, 1))
destination -= 8
}
return append(
code,
0x83,
ModRM(0b11, 0b000, byte(destination)),
byte(number),
)
}

View File

@ -0,0 +1,15 @@
package x64
import (
"git.akyoto.dev/cli/q/src/build/cpu"
)
// AddRegNum adds a number to the given register.
func AddRegNum(code []byte, destination cpu.Register, number int) []byte {
return numberToRegister(0x83, 0x81, 0b000, code, destination, number)
}
// SubRegNum subtracts a number from the given register.
func SubRegNum(code []byte, destination cpu.Register, number int) []byte {
return numberToRegister(0x83, 0x81, 0b101, code, destination, number)
}

View File

@ -0,0 +1,41 @@
package x64
import (
"math"
"git.akyoto.dev/cli/q/src/build/cpu"
)
// numberToRegister encodes an instruction with a register and a number parameter.
func numberToRegister(opCode8 byte, opCode32 byte, reg byte, code []byte, destination cpu.Register, number int) []byte {
b := byte(0)
if destination >= 8 {
b = 1
destination -= 8
}
rex := REX(1, 0, 0, b)
modRM := ModRM(0b11, reg, byte(destination))
if number >= math.MinInt8 && number <= math.MaxInt8 {
return append(
code,
rex,
opCode8,
modRM,
byte(number),
)
}
return append(
code,
rex,
opCode32,
modRM,
byte(number),
byte(number>>8),
byte(number>>16),
byte(number>>24),
)
}