Added push and pop instructions

This commit is contained in:
Eduard Urbach 2024-06-27 12:59:41 +02:00
parent 8e64271f74
commit 735057ac74
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 45 additions and 0 deletions

View File

@ -91,6 +91,18 @@ func (a *Assembler) Finalize() ([]byte, []byte) {
code = x64.MoveRegisterRegister64(code, operands.Destination, operands.Source)
}
case POP:
switch operands := x.Data.(type) {
case *Register:
code = x64.PopRegister(code, operands.Register)
}
case PUSH:
switch operands := x.Data.(type) {
case *Register:
code = x64.PushRegister(code, operands.Register)
}
case RETURN:
code = x64.Return(code)

View File

@ -24,6 +24,16 @@ func (a *Assembler) RegisterRegister(mnemonic Mnemonic, left cpu.Register, right
})
}
// Register adds an instruction using a single register.
func (a *Assembler) Register(mnemonic Mnemonic, register cpu.Register) {
a.Instructions = append(a.Instructions, Instruction{
Mnemonic: mnemonic,
Data: &Register{
Register: register,
},
})
}
// Label adds a label at the current position.
func (a *Assembler) Label(name string) {
a.Instructions = append(a.Instructions, Instruction{

View File

@ -11,6 +11,8 @@ const (
MUL
LABEL
MOVE
POP
PUSH
RETURN
SUB
SYSCALL
@ -40,6 +42,12 @@ func (m Mnemonic) String() string {
case MUL:
return "mul"
case POP:
return "pop"
case PUSH:
return "push"
case RETURN:
return "return"

15
src/build/asm/Register.go Normal file
View File

@ -0,0 +1,15 @@
package asm
import (
"git.akyoto.dev/cli/q/src/build/cpu"
)
// Register operates with a single register.
type Register struct {
Register cpu.Register
}
// String returns a human readable version.
func (data *Register) String() string {
return data.Register.String()
}