82 lines
1.8 KiB
Text
82 lines
1.8 KiB
Text
Type Instruccion
|
|
opcode As String * 5
|
|
operand As Integer
|
|
has_operand As Integer
|
|
End Type
|
|
|
|
Dim Shared As Integer datasize = 1, nstrings = 2
|
|
|
|
Dim Shared As String strings(1)
|
|
strings(0) = "count is: "
|
|
strings(1) = Chr(10)
|
|
|
|
Dim Shared As Instruccion code(17)
|
|
code(0) = Type("push" , 1 , 1)
|
|
code(1) = Type("store", 0 , 1)
|
|
code(2) = Type("fetch", 0 , 1)
|
|
code(3) = Type("push" , 10, 1)
|
|
code(4) = Type("lt" , 0 , 0)
|
|
code(5) = Type("jz" , 17, 1)
|
|
code(6) = Type("push" , 0 , 1)
|
|
code(7) = Type("prts" , 0 , 0)
|
|
code(8) = Type("fetch", 0 , 1)
|
|
code(9) = Type("prti" , 0 , 0)
|
|
code(10) = Type("push" , 1 , 1)
|
|
code(11) = Type("prts" , 0 , 0)
|
|
code(12) = Type("fetch", 0 , 1)
|
|
code(13) = Type("push" , 1 , 1)
|
|
code(14) = Type("add" , 0 , 0)
|
|
code(15) = Type("store", 0 , 1)
|
|
code(16) = Type("jmp" , 2 , 1)
|
|
code(17) = Type("halt" , 0 , 0)
|
|
|
|
|
|
' Stack, memory data
|
|
Dim Shared As Integer stack(100), dato(10)
|
|
Dim Shared As Integer sp_ = 0, pc_ = 0
|
|
|
|
Function pop() As Integer
|
|
sp_ -= 1
|
|
Return stack(sp_)
|
|
End Function
|
|
|
|
Sub push(valor As Integer)
|
|
stack(sp_) = valor
|
|
sp_ += 1
|
|
End Sub
|
|
|
|
Dim As Boolean running = True
|
|
|
|
Do While running
|
|
Dim As Instruccion inst = code(pc_)
|
|
pc_ += 1
|
|
|
|
Select Case inst.opcode
|
|
Case "push"
|
|
push(inst.operand)
|
|
Case "store"
|
|
dato(inst.operand) = pop()
|
|
Case "fetch"
|
|
push(dato(inst.operand))
|
|
Case "add"
|
|
Dim As Integer b = pop(), a = pop()
|
|
push(a + b)
|
|
Case "lt"
|
|
Dim As Integer b = pop(), a = pop()
|
|
push(Iif(a < b, 1, 0))
|
|
Case "jz"
|
|
Dim As Integer cond = pop()
|
|
If cond = 0 Then pc_ = inst.operand
|
|
Case "jmp"
|
|
pc_ = inst.operand
|
|
Case "prts"
|
|
'Dim As Integer idx = pop()
|
|
Print strings(pop());
|
|
Case "prti"
|
|
Print pop();
|
|
Case "halt"
|
|
running = False
|
|
End Select
|
|
Loop
|
|
|
|
Sleep
|