MASM/Bitwise xnor

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

globals.masm

nine ptr-size
arg
arg1
ret
old-ipt

xnor.masm

tmp

lab xnor
        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 = nand(nand(arg, arg1), tmp)) = nand(nand(arg, arg1), or(arg, arg1))) = xnor(arg, arg1)
        alc one
        get arg
        nnd arg1
        nnd tmp
        fre tmp
        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

xnor
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 xnor
        alc ptr-size
        set tmp
        adr xnor
        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 xnor
        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