asm

package
v0.0.0-...-9fd96d6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2021 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssembleProgram

func AssembleProgram(lines []*Instruction) ([100]int, error)

Assembles the given /evaluated/ program into LMC machine code.

func IsValidAddress

func IsValidAddress(address int) bool

Establishes whether the given address is valid, noting that LMC address are only 3 digits.

Types

type AddressRef

type AddressRef struct {
	Type    AddressType `json:"type"`
	Address int         `json:"address"`
	Value   int         `json:"value"`
}

An address reference.

func CreateAddressRef

func CreateAddressRef(address int) *AddressRef

Creates an address reference, given a concrete address.

func CreateDataLabel

func CreateDataLabel(value int) *AddressRef

Creates an address reference for the use of data labels.

func CreatePositionLabel

func CreatePositionLabel() *AddressRef

Creates an address reference for the use of position labels.

func (*AddressRef) Apply

func (r *AddressRef) Apply(i *Instruction) *Instruction

Applies the label to an instruction to create a labeled instruction.

type AddressRegistry

type AddressRegistry map[string]*AddressRef

func (AddressRegistry) GetMapping

func (r AddressRegistry) GetMapping(labelName string) *AddressRef

type AddressType

type AddressType int

The type of an address reference, used for checking whether additional processing needs to happen later on.

const (
	ADDR_NORMAL AddressType = iota
	ADDR_LABEL_POSITION
	ADDR_LABEL_DATA
)

func (AddressType) IsLabel

func (t AddressType) IsLabel() bool

Establishes whether the address type is a label type, position or data.

type EvaluatedInstruction

type EvaluatedInstruction struct {
	Opcode  Opcode
	Address int
}

Represents an evaluated instruction.

func CreateEvaluatedInstruction

func CreateEvaluatedInstruction(opcode Opcode, address int) *EvaluatedInstruction

func DisassembleInstruction

func DisassembleInstruction(instruction int) (*EvaluatedInstruction, error)

Disassemble machine code into the form of an /evaluated/ instruction.

func (*EvaluatedInstruction) Compile

func (i *EvaluatedInstruction) Compile() int

Compiles the instruction.

type Instruction

type Instruction struct {
	Type       InstructionType `json:"type"`
	Opcode     Opcode          `json:"opcode"`
	AddressRef *AddressRef     `json:"ref"`

	// Labeled
	Label *AddressRef `json:"label"`
}

Represents un-evaluated instructions.

func ADD

func ADD(address *AddressRef) *Instruction

Creates a ADD instruction.

func BRA

func BRA(address *AddressRef) *Instruction

Creates a BRA instruction.

func BRP

func BRP(address *AddressRef) *Instruction

Creates a BRP instruction.

func BRZ

func BRZ(address *AddressRef) *Instruction

Creates a BRZ instruction.

func CreateInstruction

func CreateInstruction(opcode Opcode, address *AddressRef) *Instruction

func DAT

func DAT(address *AddressRef) *Instruction

Creates a DAT instruction.

func HLT

func HLT() *Instruction

Creates a HLT instruction.

func INP

func INP() *Instruction

Creates a INP instruction.

func LDA

func LDA(address *AddressRef) *Instruction

Creates a LDA instruction.

func OUT

func OUT() *Instruction

Creates a OUT instruction.

func STA

func STA(address *AddressRef) *Instruction

Creates a STA instruction.

func SUB

func SUB(address *AddressRef) *Instruction

Creates a SUB instruction.

func (*Instruction) Evaluate

func (i *Instruction) Evaluate() (*EvaluatedInstruction, error)

Evaluates an instruction. NOTE: The address reference should have /already/ been evaluated

type InstructionType

type InstructionType int

The type of an instruction, used for establishing whether the label of an instruction needs to be evaluated when assembling.

const (
	INST_NORMAL InstructionType = iota
	INST_LABELED
)

type Opcode

type Opcode int

Represents the operation code of an instruction, specifying what operation to perform.

const (
	// This is not actually an operation code, but rather a psuedo
	// Opcode for DAT instructions.
	OP_DAT Opcode = -1

	// The HLT operation code will stop the program when run.
	OP_HLT Opcode = 0

	// The ADD operation code will add the given argument to the
	// current value on the accumulator, the result will be stored
	// on the accumulator.
	OP_ADD Opcode = 1

	// The SUB operation code will subtract the given argument from
	// the current value on the accumulator, the result will be stored
	// on the accumulator.
	OP_SUB Opcode = 2

	// The STA operation code will store the value on the accumulator
	// to the address provided.
	OP_STA Opcode = 3

	// The LDA operation code will load the value from the provided
	// address into the accumulator.
	OP_LDA Opcode = 5

	// The BRA operation code will branch to the provided address.
	OP_BRA Opcode = 6

	// The BRZ operation code will branch to the provided address,
	// given that the value on the accumulator is zero.
	OP_BRZ Opcode = 7

	// The BRP operation code will branch to the provided address,
	// given that the value on the accumulator is positive.
	OP_BRP Opcode = 8

	// The INP_OUT operation code performs two operations depending
	// on the provided address.
	// If the address is 1, the program will ask for input to be stored
	//   in the accumulator.
	// If the address is 2, the program will output the value stored in
	//   the accumulator.
	OP_INP_OUT Opcode = 9
)

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

func CreateParser

func CreateParser() *Parser

func (*Parser) Assemble

func (p *Parser) Assemble() ([]*Instruction, error)

func (*Parser) ReadFromFile

func (p *Parser) ReadFromFile(file *os.File)

type Token

type Token struct {
	Type TokenType
	Name string
}

func CreateToken

func CreateToken(ttype TokenType, name string) *Token

func GetToken

func GetToken(str string) *Token

type TokenType

type TokenType int
const (
	// Instructions
	TKN_HLT TokenType = iota
	TKN_ADD
	TKN_SUB
	TKN_STA
	TKN_LDA
	TKN_BRA
	TKN_BRZ
	TKN_BRP
	TKN_INP
	TKN_OUT
	TKN_DAT

	// Other
	TKN_VALUE
	TKN_LABEL
)

func GetTokenType

func GetTokenType(str string) TokenType

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL