Bots and Simulations > DNA - General

Swapping the top two stack values?

(1/3) > >>

Trafalgar:
Has anyone thought of any clever ways to swap the top two stack variables without needing a temporary variable? I haven't been able to come up with anything (yet). (But I'm thinking it may just be impossible)

I'm trying to make branch conditionals, but I can't unless I can swap stack values. That's because I need to keep values on the stack for each conditional, dupe them, and multiply them times the addresses of each store. The problem is that the store command is 'value address store'. If it were 'address value store', I could just dup address mult value store.

This is what it would look like (python-like code on the left, translation and comments on the right after the #) if store commands were 'address value store':

--- Code: ---    #Branch conditionals:
    if (foo==2):        # *.foo 2 sub sgn abs - ++ (stack holds 1 value now)
        if (bar==3):    # dup *.bar 3 sub sgn abs - ++ & (stack holds 2 values now)
            #Note: The store commands here have the address (variable) and value backwards. It should be 'value address store', and we've written this using 'address value store'. Doing this with the way store really works is probably impossible (without a temporary variable, which would defeat half the point of having conditions on stores).
            sx = 1        # dup .sx mult 1 store (stack holds 2 values)
            up = 1        # dup .up mult 1 store (stack holds 2 values) (if there was no else, this should omit the dup?)
        else:            # - ++ (reverse the topmost stack value) (stack holds 2 values)
            sx = -1        # dup .sx mult -1 store (stack holds 2 values)
            up = -1        # dup .up mult -1 store (stack holds 2 values)
        #end of if/else - pop stack: dup xor inc (stack holds 1 value)
    elif (foo==3):        # dup - ++ dup *.foo 3 sub sgn abs - ++ & (stack holds 3 values - the first is the result of the if, the second is the !if, and the third is the result of this elif): (either TFF, FTF, or FTT)
        sx = 2            # dup .sx mult 2 store (stack holds 3 values)
    elif (foo==4):        # - ++ & (stack TF (if was true), FT (all conditions were false), or FF (a previous condition was true)) dup *.foo 4 sub sgn abs - ++ & (stack TFF, FTT, FTF, or FFF)
        sx = 3            # dup .sx mult 3 store (stack holds 3 values)
    elif (foo==5):        # - ++ & (stack TF (if was true), FF (a previous condition was true), FT (all conditions were false), or FF) dup *.foo 5 sub sgn abs - ++ & (stack TFF, FFF, FTT, FTF, or FFF)
        sx = 4            # dup .sx mult 4 store (stack holds 3 values)
    #end of elif:        # - ++ | - ++ (stack F (a previous condition, or the if, was true), T (all conditions were false))
    else:                # Note that it was "- ++ | - ++" when an elif ends, and "- ++" when an if ends.
        sx = 5            # dup .sx mult 3 store (stack holds 1 value)
    #end of if/else - pop stack: Since nothing else is on the stack: dup xor inc (stack holds 0 values)
    #That's the end of it
--- End code ---

Numsgil:
You can try an XOR swap maybe. Not sure how it would work with the stack, though.  XOR swap.

abyaly:
I wonder if I can write a proof for impossibility for the stack swap.

Trafalgar:
It looked pretty impossible to me - unless I put the value in twice, which would defeat the point of the thing (which is to reduce DNA size*).

I did get it implemented by using one memory location and inc and dec to flip it between 0 and 1, and it brought my Guardian bot down from a DNA length of 15 thousand to around 9 thousand, but the store-to-zero-not-popping-the-value-from-the-stack bug is keeping it from actually working. I might try making it write to 1000 instead of 0, but if that still costs energy, then the bot would probably rapidly energy hemorrhage from executing 140 store operations per cycle.

(Plus it's already going to be incurring the inc/dec energy cost from the workaround, which essentially involves using one inc or dec per if, elif, and else statement (unless they have no actual code in them besides more if/elif/else statements))

* I'm probably the only person writing ridiculously complicated bots with numerous conditions attached to almost every store, but I'm guessing that's probably because of the sheer infeasibility of doing such a thing directly in DB's bot language (as opposed to in an easier to understand language, which is translated to DB bot language).

Numsgil:
I thought about implementing a swap command.  I think it's a reasonable thing to try.  Maybe in a future version.

While we're on the subject, is there anything else that you'd like to see a feature for that's difficult to do with the language at the moment?

Navigation

[0] Message Index

[#] Next page

Go to full version