Implement instructions 0x0 and 0x1
This commit is contained in:
parent
f27ac7b560
commit
601636a0ad
19
creature.go
19
creature.go
@ -8,13 +8,24 @@ type Machine struct {
|
||||
pointer int
|
||||
}
|
||||
|
||||
// Execute starts execution at the address specified by offset. The machine will
|
||||
// run until a HALT instruction is encountered, or the end of program memory is
|
||||
// reached.
|
||||
func (machine *Machine) Execute (offset int) {
|
||||
machine.counter = offset
|
||||
|
||||
for {
|
||||
switch machine.Program[machine.counter] {
|
||||
switch machine.instruction() {
|
||||
case 0x0:
|
||||
// PUSH
|
||||
// push the next word in program memory onto the stack
|
||||
machine.counter ++
|
||||
machine.Push(machine.instruction())
|
||||
|
||||
case 0x1:
|
||||
// POP
|
||||
// pop the top word off of the stack, and discard it
|
||||
machine.Pop()
|
||||
case 0x2:
|
||||
case 0x3:
|
||||
case 0x4:
|
||||
@ -34,6 +45,12 @@ func (machine *Machine) Execute (offset int) {
|
||||
}
|
||||
}
|
||||
|
||||
// Instruction returns the current instruction in program memory.
|
||||
func (machine *Machine) instruction () (instruction int) {
|
||||
instruction = machine.Program[machine.counter]
|
||||
return
|
||||
}
|
||||
|
||||
// reallocateStack changes the size of the stack to something reasonable. This
|
||||
// should be called then the stack pointer is really small compared to the
|
||||
// actual stack size, or the stack pointer is bigger than the stack.
|
||||
|
Loading…
Reference in New Issue
Block a user