urthbound wrote
I'm having a little trouble conceptualizing how to implement the 'eq' function (and by extension, some others) in my VM translator.
If I wanted to create a loop to decrement both values until one of them is zero and then compare the two against each other (so that if they are both zero then the original values were equal) how do I set the points that the compiled loop will jump back to without knowing what lines they will end up being on? Or for multiple loops in one file? Is that something I can implement in the language I am writing my translator in?
I am certain the answer is staring me in the face in the form of temporary function variables or creating on the fly symbols, but I can't quite wrap my head around it and would appreciate a point in the right direction. Am I completely off base here?
You are going to want to write a routine that returns guaranteed unique labels that your code generators can use. I called mine UniqueLabel() and it returns "L$nnn" where nnn increments for each call.
Decrementing to zero would be really slow; you need to decrement -1 65535 times to get to 0!
Think back to basic algebra. If
x =
ythen
x -
y =
y -
y x -
y = 0.
The assembly language code your translator generates will need two unique labels so that it can implement something like
pop stack into D-reg
subtract top of stack from D-reg
if D-reg = 0
set top of stack to -1 (true)
else
set top of stack to 0 (false)
--Mark