Implementing loops independent of PC

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Implementing loops independent of PC

urthbound
Hi all,

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?

Thank you!
Reply | Threaded
Open this post in threaded view
|

Re: Implementing loops independent of PC

cadet1620
Administrator
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 = y
then
    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
Reply | Threaded
Open this post in threaded view
|

Re: Implementing loops independent of PC

urthbound
Ha, yes decrementing to 0 was a dumb idea, absolutely! But you answered my main question on how to create discrete looping point variables- for some reason I had it in my head that that should be taken care of using some crazy memory swapping or something.

I created a simple translator function and interpolated the resulting var into my assembly string and it works great. Thanks for the help!