Skip to content

Instantly share code, notes, and snippets.

@harrynull
Created October 30, 2021 13:25
Show Gist options
  • Save harrynull/c011c8949f4cba1551e3d840805347ed to your computer and use it in GitHub Desktop.
Save harrynull/c011c8949f4cba1551e3d840805347ed to your computer and use it in GitHub Desktop.
# goes down first
v*
#1
nop*
#2
nop*
#3
>
# === read a number ===
read
6
8
*
-
# === read a number done ===
@start
>
# read operator
read
# == check if it's 0 ==
dup
@end_of_program_check
|*
wrap up
# === read a number ===
read
6
8
*
-
# === read a number done ===
# swap last two
1
2
\
# now check if it's +
dup
"
+
"
-
@+
|*
# goes down if it is
# else goes up
wrap up
# now check if it's *
dup
"
*
"
-
@*
|*
wrap up
# now check if it's ^
dup
"
^
"
-
@^
|*
# goes down if it is
# else goes up
wrap up
# now check if it's |
nop 32
dup
"
|
"
-
@|
|*
wrap up
# now check if it's xor
nop 75
dup
"
x
"
-
@x
|*
wrap up
# it must be < shift
# =========== OPERATORS =============
# === handle < ===
pop
# [a,b] a<<b
# while b
@shift_while_cond
>
dup
not
@shift_while
|*
wrap down
1
-
1
2
\
2
*
1
2
\
goto 2 shift_while_cond
$shift_while
!w
!w
<*
!a
pop*
!a
goto 1 start
# === handle plus ===
$+
!s
nop*
!s
>
# pop the operator
pop
+
goto 10 start
# === handle * ===
$*
!s
nop*
!s
>
# pop the operator
pop
*
goto 10 start
# === handle ^ ===
$^
!s
nop*
!s
>
# pop the operator
pop
# do power e.g. 5^3 stack: [5,3]
1
-
dup
@power_check_0
|*
wrap up
# place a marker [5,3,0]
0
1
2
\
# [5,0,3]
2
3
\
# starts adding
@mul_add_parts
>
# [0,5,3]
1
2
\
# [0,3,5]
dup
# [0,3,5,5]
1
3
\
# [0,5,5,3]
1
-
# [0,5,5,2]
# repeats until this goes to 0
dup
@pow_add_until_0
|*
# ==0 . goes down: resume -> direction
wrap down
pop
# [0,5,5,5]
# now multiplying until its 0
@mul_start_multiplying
>
1
2
\
dup
not
@mul_is_0
|*
# if it's 0: goes up and continue
wrap up
pop
goto 10 start
# MULTIPLY if it's not 0: goes down
$mul_is_0
!s
<*
!a
*
goto 6 mul_start_multiplying
# REPEATING PART goes up
$pow_add_until_0
!w
goto 4 mul_add_parts
$power_check_0
!s
pop*
!s
goto 10 start
# === handle | ===
$|
!s
nop*
!s
>
# pop the operator
pop
# res = 0
# i = 1
0
1
# [op1, op2, res, i]
@or_while_cond
#while a + b != 0:
>
fetch 3
fetch 5
+
@or_while
|*
# a+b >= 0: UP
wrap up
# if a % 2 + b % 2 != 0:
fetch 3
2
%
fetch 5
2
%
+
@or_bit_not_zero
|*
wrap up
# res += i
# [op1, op2, res, i]
dup
# [op1, op2, res, i, i]
1
3
\
# [op1, op2, i, i, res]
+
# [op1, op2, i, RES]
1
2
\
# end if }
>
# [op1, op2, RE, i]
# a = a // 2
1
3
\
2
/
1
3
\
# b = b // 2
1
4
\
2
/
1
4
\
# i *= 2
2
*
goto 8 or_while_cond
# end loop }
>
# [op1, op2, RES, i]
2
4
\
pop
pop
pop
goto 10 start
$or_while
!s
nop*
!s
>
nop 50
^*
!w
^*
$or_bit_not_zero
!s
>
nop 9
^
# === handle xor ===
$x
!s
nop*
!s
>
# pop the operator
pop
# res = 0
# i = 1
0
1
# [op1, op2, res, i]
@xor_while_cond
#while a + b != 0:
>
fetch 3
fetch 5
+
@xor_while
|*
# a+b >= 0: UP
wrap up
# if !(a % 2 + b % 2-1)!=0:
fetch 3
2
%
fetch 5
2
%
+
1
-
not
@xor_bit_not_zero
|*
wrap up
# res += i
# [op1, op2, res, i]
dup
# [op1, op2, res, i, i]
1
3
\
# [op1, op2, i, i, res]
+
# [op1, op2, i, RES]
1
2
\
# end if }
>
# [op1, op2, RE, i]
# a = a // 2
1
3
\
2
/
1
3
\
# b = b // 2
1
4
\
2
/
1
4
\
# i *= 2
2
*
goto 8 xor_while_cond
# end loop }
>
# [op1, op2, RES, i]
2
4
\
pop
pop
pop
goto 10 start
$xor_while
!s
nop*
!s
>
nop 53
^*
!w
^*
$xor_bit_not_zero
!s
>
nop 9
^
$end_of_program_check
!s
>
pop
output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment