We need to take care of a procedure that would extract the correct address of the Assembly implementation of the virtual instruction from the tree prior to beginning the implementation of the virtual processor's loop.
The tree_lookup procedure requires two parameters:
- The address of the tree_root variable
- The byte opcode cast to double word
When this procedure is called, it "walks" the tree node by node (in accordance with the rule the tree was sorted by) and compares the opcode parameter to the opcode value of the instruction structure referred to by the current node. The procedure returns the address of the Assembly implementation of the operation code, or it returns a zero if no such opcode has been defined:
tree_lookup:
push ebp
mov ebp, esp
push ebx ecx
virtual at ebp + 8
.treePtr dd ? ; First parameter - pointer to tree_root
.code dd ? ; Second parameter - opcode value
end virtual
virtual at ecx
.node tnode ?,?,? ; Lets us treat ECX as a pointer
; to tnode structure
end virtual
virtual at eax
.instr instruction ?, ? ; Lets us treat EAX as a pointer
; to instruction structure
end virtual
mov ecx, [.treePtr] ; Load the pointer to tree_root
mov ecx, [ecx] ; Load the pointer to root node
mov ebx, [.code] ; Read current opcode
movzx ebx, bl ; Cast to unsigned int
@@:
or ecx, 0 ; Check whether ECX points to a node
jz .no_such_thing ; and return zero if not
mov eax, [.node.data] ; Load pointer to instruction structure
cmp ebx, [.instr.opcode] ; Compare opcode value
jz @f
ja .go_right ; If node contains lower opcode, then
; continue searching the right subtree
mov ecx, [.node.left] ; Otherwise continue searching the
jmp @b ; left subtree
.go_right:
mov ecx, [.node.right]
jmp @b
@@:
mov eax, [.instr.target] ; Relevant instruction structure has
; been found, so return the address
; of instruction implementation
@@:
pop ecx ebx ; We are done
leave
ret 8
.no_such_thing: ; Zero out EAX to denote an error
xor eax, eax
jmp @b