MASM/Bitwise xor

From Esolang
Jump to navigation Jump to search
Back to Minimal assembly language

globals.masm

nine ptr-size
arg
arg1
ret
old-ipt

xor.masm

tmp

lab xor
        cmt tmp = not(arg)
        alc one
        get arg
        nnd vpt
        set tmp

        cmt tmp = or(arg, arg1)
        alc one
        get arg1
        nnd vpt
        nnd tmp
        fre tmp
        set tmp

        cmt ret = not(nand(nand(arg, arg1), tmp))) = not(xnor(arg, arg1)) = xor(arg, arg1)
        alc one
        get arg
        nnd arg1
        nnd tmp
        fre tmp
        nnd vpt
        set ret

        cmt set ipt to old-ipt + 9 to skip function call
        alc nine
        set tmp
        adr old-ipt
        get nine
        drf vpt
        fre tmp
        set ipt

main.masm

xor
loop
tmp
two
new-out

lab loop
        cmt call loop
        alc ptr-size
        set tmp
        adr loop
        drf vpt
        fre tmp
        set ipt

lab main
        cmt init nine/ptr-size
        alc one
        get one
        rot one
        rot one
        rot one
        rot one
        rot one
        nnd vpt
        set tmp
        alc one
        get one
        nnd vpt
        nnd tmp
        fre tmp
        set nine

        cmt copies 0th byte of in to arg
        alc one
        get in
        set arg

        cmt copies 1st byte of in to arg1
        alc ptr-size
        set tmp
        adr in
        get one
        drf vpt
        fre tmp
        set tmp
        alc one
        get tmp
        set arg1

        cmt set old-ipt
        alc ptr-size
        set tmp
        adr ipt
        drf vpt
        fre tmp
        set old-ipt

        cmt call xor
        alc ptr-size
        set tmp
        adr xor
        drf vpt
        fre tmp
        set ipt

        cmt init two
        alc one
        get one
        rot one
        rot one
        rot one
        rot one
        rot one
        rot one
        rot one
        set two

        cmt alloc new-out and sets 0th byte to ret from xor
        alc two
        get ret
        set new-out

        cmt null terminates new-out
        alc ptr-size
        set tmp
        adr new-out
        get one
        drf vpt
        fre tmp
        cmt abusing the fact that out is currently 0
        get out

        cmt out = new-out
        alc ptr-size
        set tmp
        adr new-out
        drf vpt
        fre tmp
        set out

        cmt clean-up (in case we want to add more code)
        fre arg
        fre arg1
        fre ret
        fre two

        cmt call loop
        alc ptr-size
        set tmp
        adr loop
        drf vpt
        fre tmp
        set ipt