Search code examples
assemblyapache-flex

implementing JMP in flex


My task is to implement interpreter of assembly-like language using Flex.

There are such commands as:

add x y z, (z = x + y)

sub x y z, (z = x - y)

mul x y z, (z = x * y)

div x y z, (z = x / y)

let x 10, (x = 10)

mov x y, (y = x)

Implementing interpreter of this commands is pretty easy using Flex.

I have stuck with implementing JMP line, (current line = line) instruction. I have no idea how to reset Flex current parsing line or something like that.


Solution

  • A JMP instruction simply modifies the program counter with the address of the target instruction. So, this VM you are making needs a PC register.

    It really is that simple. Don't overthink this. Keep in mind, they created JMP instructions out of vacuum tubes half a century ago. That puts a severe limit on complexity.