## The first program A simple example would be to print all $2^{16}$ possible colors to the screen. We make our lives easier, by mapping each index of the screen-buffer to the color which is encoded with the index. Here, we use the names of the opcodes instead of their numbers. ``` Set 501 1 0 // Write the value 1 to address 501 Set 502 65535 0 // Write the largest possible value to 502 Print 500 500 0 // Display color=@500 at screen-index=@500 Add 500 501 500 // Increment the color/screen-index Cmp 500 502 503 // See if we are not at the max number Xor 503 501 503 // Negate it Skip 0 4 503 // Unless we are at the max number, go back 4 instructions Sync 0 0 0 // Sync GoTo 0 0 0 // Repeat to keep the window open ``` We could rely on the fact that the value at index 500 starts at zero and we did not have to initialize it. To build a program that we can execute, we could use python: ```python import struct code = [ 0, 501, 1, 0, #Opcodes replaced with numbers 0, 502, 65535, 0, 11, 500, 500, 0, # ... ] with open("all_colors.svc16", "wb") as f: for value in code: f.write(struct.pack(" Add variable "1" variable Negate variable -> Xor variable "1" variable JumpTo location condition -> GoTo "0" location condition // where location is not a variable JumpAfter location condition -> GoTo "1" location condition // where location is not a variable ``` ### Labels As it stands, we still have to count or remember line numbers for the `Skip` and `GoTo` instructions. One simple Idea would be to allow a label (or multiple) for each line. ``` Print color color 0 Incr color Cmp color "65535" condition Negate condition JumpTo @start condition Sync 0 0 0 JumpTo @start "0" ``` References can then be resolved by counting the lines (and multiplying by 4 to get the memory address). ### Including Data We might want to include large sequences of data for things like sprites. This can be done in a way very similar to constants. The syntax could look something like this ``` Print "path/to/binary.dat" index 0 ``` This does two things: - 1. It places the content of the binary file into the program. - 2. It creates a constant that stores the first address of this content. This way, we can iterate over the data at runtime, using the `Deref` instruction. ### Enabling memory management So far, we can only use variables of fixed size. If we want to allocate memory at runtime, we have to know what part of memory is not used by the program (or variables, constants, data ...). This can be easily done by adding a constant called `"free"` which resolves to an address where the first free address is stored.