Skip to content

Snippets

Supercolbat edited this page Feb 25, 2022 · 5 revisions

Binary++ might not be the most easy to use language at first, so here are a few snippets of very important concepts in Binary++ such as if statements and loops.

More snippets will be added over time.

If statement

This program checks if the value on the top of the stack is even. If it is, it will print "Even". Otherwise, it will print "Odd".

;; The value to check
00000100 00001010    PUSH_STACK 10

;; Modulo the number by 2
00000100 00000010    PUSH_STACK 2
10000100             BINARY_MODULO

;; Check if the result is 0
00000100 00000000    PUSH_STACK 0
11100001             EQUAL_TO

;; If it is equal to 0, run the next 3 instructions
10100000 00000011    IF_RUN_NEXT 3
                     PUSH_STRING_STACK "Even\n"
00000101 01000101 01110110 01100101 01101110 00001010 00000000
00001010 00000000    WRITE_TO 0 (stdout)
;; Skip the instructions for printing "Odd"
10100001 00000010    SKIP_NEXT 2

;; Otherwise, print "Odd"
                     PUSH_STRING_STACK "Odd\n"
00000101 01001111 01100100 01100100 00001010 00000000
00001010 00000000    WRITE_TO 0 (stdout)

Notes:

TBA

For loops

This program prints all the characters in ASCII.

[RESERVED, FOR_LOOP, i]

00000100 00000000    PUSH_STACK 0
00000011 00000010    STORE_MEMORY 2 [i]

;; Start of loop
00001110 00000001    MAKE_MARKER 1

;; Check if 'i' is less than 256
00000010 00000010    LOAD_MEMORY 2 [i]
00000100 11111111    PUSH_STACK 255
11100011             LESS_EQUAL_THAN

;; Print out 'i' if the condition is true and add one
10100000 00000111    IF_RUN_NEXT 7
00000010 00000010    | LOAD_MEMORY 2 [i]
00001010 00000000    | WRITE_TO 0
                     | 
                     | ;; Add one to 'i'
00000010 00000010    | LOAD_MEMORY 2 [i]
00000100 00000001    | PUSH_STACK 1
10000001             | BINARY_ADD
00000011 00000010    | STORE_MEMORY 2 [i]
                     | 
                     | ;; Loop back
00001111 00000001    | GOTO_MARKER 1

Notes: The first two lines that set 0 to i are redundant as variables are 0 by default. This is only done for readability reasons as this is not very obvious.

In this case, using the memory can be completely replaced with just the stack, but doing it this way more resembles real use cases.

Actually, this code is very inefficient, but it is purposely that way for readability reasons.

Functions

Yes

;; Function concept in Binary++ ;;

[RESERVED, BACK, FUNC1]

;; Make function
;;; Make sure the code doesn't actually run
10100001 00000100   SKIP_NEXT 4
;;; Takes a number and multiplies it by 3
00001110 00000010   MAKE_MARKER 2
00000100 00000011   PUSH_STACK 3
10000101            BINARY_MULTIPLY
00001111 00000000   GOTO_MARKER 0

;; Call the function
;;; Push arguments to stack and goto function
00000100 00000010   PUSH_STACK 2
00001111 00000010   GOTO_MARKER 
;;; Add 48 to the result to result in ASCII
00000100 00110000   PUSH_STACK 48
10000001            BINARY_ADD
00001010 00000000   WRITE_TO 0

;; Again but with another number
00000100 00000011   PUSH_STACK 3
00001111 00000010   GOTO_MARKER 2
00000100 00110000   PUSH_STACK 48
10000001            BINARY_ADD
00001010 00000000   WRITE_TO 0

Notes:

Doing GOTO_MARKER 0 will return back to the previous GOTO_MARKER call.

Clone this wiki locally