Last active
December 28, 2017 19:14
-
-
Save aaronryank/378ee6f5739e2f57cb23e61847a586d1 to your computer and use it in GitHub Desktop.
arithmetic via bitwise operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COMMENT add $1 and $2 into $3 | |
.ADD: | |
LABEL &1 | |
AND $1 $2 *1 | |
XOR $1 $2 $1 | |
SL *1 &1 $2 | |
JMP @1 $2 | |
RET $1 | |
COMMENT increment $1 into $2 | |
.INC: | |
ADD $1 &1 *1 | |
RET *1 | |
COMMENT subtract $2 from $1 into $3 | |
.SUB: | |
LABEL &1 | |
NOT $1 *1 | |
AND *1 $2 *2 | |
XOR $1 $2 $1 | |
SL *2 &1 $2 | |
JMP @1 $2 | |
RET $1 | |
COMMENT decrement $1 into $2 | |
.DEC: | |
SUB $1 &1 *1 | |
RET *1 | |
COMMENT store !1 in $2 | |
.LNOT: | |
MOV *1 &1 &1 | |
MOV *1 &0 $1 | |
RET *1 | |
COMMENT store $1 == $2 in $3 | |
.EQ: | |
XOR $1 $2 *1 | |
MOV *2 &1 &1 | |
MOV *2 &0 *1 | |
RET *2 | |
COMMENT multiply $1 by $2 into $3 | |
.MUL: | |
LABEL &1 | |
ADD *1 $1 *1 | |
SUB $2 &1 $2 | |
JMP @1 $2 | |
RET *1 | |
COMMENT divide $1 by $2 into $3 | |
.DIV: | |
LABEL &1 | |
ADD *1 &1 *1 | |
SUB $1 $2 $1 | |
GT $1 &0 *2 | |
JMP @1 *2 | |
SUB *1 &1 *1 because flawed algorithm and too lazy to fix | |
RET *1 | |
COMMENT invert sign of $1 into $3 if $2 | |
.INV_SIGN: | |
JMP &1 $2 | |
JMP &2 &1 | |
NOT $1 $1 | |
INC $1 $1 | |
RET $1 | |
.LNOT: | |
MOV *1 &1 &1 | |
MOV *1 &0 $1 | |
RET *1 | |
.LAND: | |
MOV $1 &1 $1 | |
MOV $2 &1 $2 | |
AND $1 $2 *1 | |
RET *1 | |
.LOR: | |
MOV $1 &1 $1 | |
MOV $2 &1 $2 | |
OR $1 $2 *1 | |
RET *1 | |
.GT: | |
NOT $1 *1 | |
ADD $2 *1 *2 | |
ADD *2 &1 *2 | |
SR *2 &31 *2 | |
AND *2 &1 *2 | |
RET *2 | |
.LT: | |
GT $2 $1 *1 | |
RET *1 | |
.MOD: | |
DIV $1 $2 *1 | |
MUL *1 $2 *1 | |
SUB $1 *1 *1 | |
RET *1 | |
.FMP10: | |
MOV *1 &1 &1 | |
LABEL &1 | |
MUL *1 &10 *1 | |
DIV $1 &10 $1 | |
JMP @1 $1 | |
RET *1 | |
._XOR: | |
AND $1 $2 *1 | |
NOT *1 *1 | |
OR $1 $2 *2 | |
AND *1 *2 *3 | |
RET *3 | |
.READI: | |
LABEL &1 | |
IN *1 &1 *2 | |
SUB *1 &48 *1 | |
LNOT *2 *3 | |
JMP &2 *3 | |
MUL *4 &10 *4 | |
ADD *4 *1 *4 | |
JMP @1 *2 | |
RET *4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment