Something I wrote this morning, here is a more general version, the example reconstructs the same DFA as the original post.
use64 ; very important this line
; a future improvement would be to chain together multiple DFA's using procedure calls, where higher level elements
; are really arrays of lower level elements
actionsize equ 1
macro DeterministicFiniteAutomatonState Name, StateType, InvalidAction, [next, action]
{
common
Name:
cmp rcx, rdx ; compare curenrt Address, RCX, and end Address, RDX, if they are the same, we have reached the end of the string
je StateType ; jump to StateType (ST should tell us whether this state is an Accepting state or not)
match =1 , actionsize { movzx r9, byte [rcx] \}
match =2 , actionsize { movzx r9, word [rcx] \} ; retrieve the value pointed at by current address, put it in R9
match =4 , actionsize { movzx r9, dword [rcx] \}
match =8 , actionsize { movzx r9, qword [rcx] \}
add rcx, actionsize ; increment the current address pointer by size bytes
forward
cmp r9, action ; compare what's in R9 to the given action argument
je next ; if equal, jump to the next state, else pass through to the next comparison
common
jmp InvalidAction ; if we get to this point, the current action is invalid, so Jump to InvalidAction
}
DeterministicFiniteAutomatonState StartState, NotAccepting, InvalidAct, S0, 0, S1, 1
DeterministicFiniteAutomatonState S0, Accepting, InvalidAct, S0, 0, S1, 1
DeterministicFiniteAutomatonState S1, NotAccepting, InvalidAct, S10, 0, S11, 1
DeterministicFiniteAutomatonState S10, NotAccepting, InvalidAct, S100, 0, S0, 1
DeterministicFiniteAutomatonState S11, NotAccepting, InvalidAct, S1, 0, S10, 1
DeterministicFiniteAutomatonState S100, NotAccepting, InvalidAct, S11 , 0, S100, 1
Accepting:
NotAccepting:
InvalidAct:
edit: updated it for actions represented by more than just bytes.
I wanted to have it more general in the beginning but I didn't know you needed to put a \} when using a match inside a macro.
Although if you need more than 256 actions, you have quite the DFA on your hands.