package x86

import (
	"encoding/binary"

	"git.akyoto.dev/cli/q/src/cpu"
)

// StoreNumber stores a number into the memory address included in the given register.
func StoreNumber(code []byte, register cpu.Register, offset int8, length byte, number int) []byte {
	code = memoryAccess(code, 0xC6, 0xC7, register, offset, length, 0b000)

	switch length {
	case 8, 4:
		return binary.LittleEndian.AppendUint32(code, uint32(number))

	case 2:
		return binary.LittleEndian.AppendUint16(code, uint16(number))
	}

	return append(code, byte(number))
}

// StoreRegister stores the contents of the `source` register into the memory address included in the given register.
func StoreRegister(code []byte, register cpu.Register, offset int8, length byte, source cpu.Register) []byte {
	return memoryAccess(code, 0x88, 0x89, register, offset, length, source)
}