NetBytes

From Esolang
Jump to navigation Jump to search

NetBytes is a language created by User:iconmaster in 2016.

It severely limits the available memory and program length of programs, instead requiring networking to get more complex tasks done. Only through inter-process communication could it truly be a Turing-complete language.

Model

Programs in NetBytes are composed of up to 128 commands, each command on one line. Programs execute from the top down, looping back to the beginning if necessary. Commands take the form:

   [label:] cmd [arg1 arg2 ... argn]

Labels may be on their own line. Line comments start with #.

Programs are given 12 1-byte, unsigned registers. Only 5 of them are for general use; the other 7 are for special use with some commands.

The registers are:

Register Group Use
A0 to A5 The address registers. Used for forming IP addresses for communication. Initially, the lower 4 form the 4 parts of the IPv4 localhost.
T0 to T4 The temporary registers. For general programmer use. Initially all zero.
F0 The fork register. Used with the FRK command (see below).

Internally, there are two condition flags, called Z and N. This is used for conditional jumps. They are both initially cleared.

There is an internal IP mode, used to switch between IP protocols. It can be either in IPv4 mode or IPv6 mode. It is initially set to IPv4 mode.

There is an internal structure called the connection stack. It is a stack, maximally 2 items large. When a connection is made, the details are pushed onto the stack. When a connection is ended, it pops a value off. This way, you can have multiple connections, but only the most recent connection is used for sending an receiving data.

Commands

Command Result
MOV RD RS Sets the register RD to the value of RS.
MVI RD C Sets the register RD to C. C must be between 0 and 15.
ADD RD RS Sets the register RD to RD plus RS.
SUB RD RS Sets the register RD to RD minus RS.
MUL RD RS Sets the register RD to RD times RS.
DIV RD RS Sets the register RD to RD divided by RS.
MOD RD RS Sets the register RD to RD modulo RS.
NOR RD RS Sets the register RD to the bitwise NOR of RD and RS. This should suffice for all bitwise operations.
CMP R1 R2 Subtracts R2 from R1. Sets Z if the result is 0, and clears it otherwise. Sets N if the result would be negative, and clears it otherwise.
JMP L Jumps to the command at label L.
JPZ L Jumps to the command at label L, but only if Z is set.
JPN L Jumps to the command at label L, but only if N is set.
HLT Halts program execution.
INP R Gets a single character of system input, and places it in register R.
OUT R Prints a single character to system output from register R.
IP4 Switches the IP mode to IPv4 mode. It also clears A0 to A3, placing the 4 bytes of IPv4's localhost into it.
IP6 Switches the IP mode to IPv6 mode. It also clears A0 to A5, placing the 6 bytes of IPv6's localhost into it.
WFC Waits for a connection. The program blocks, and resumes execution when a connection has been established with another NetBytes program.
CON Initiate a connection. The program blocks, and resumes execution when a connection has been established with another NetBytes program. It sends a request on the IP specified by the address registers.
SND R Sends data over a connection. The data in register R is sent, and the value is received by the other program's RCV command.
RCV R Retrieves data over a connection. It blocks until data is sent by the other program, and then stores the received value in register R.
END Terminates a connection.
FRK R Forks this program, creating a new process. It takes the value in register R, and places it in the new process's F0 register. The forked program begins at the beginning, and with all data initialized. A root program has an initial F0 of 0.

Examples

Cat

    INP T0
    OUT T0

Fork Cat

Creates a process that takes input. Output is displayed on the other instance.

    MVI T0 1
    CMP F0 T0
    JPZ SERVER
    WFC
CLIENT:
    INP T1
    SND T1
    JMP CLIENT
SERVER:
    FRK T0
    CON
SERVLOOP:
    RCV T1
    OUT T1
    JMP SERVLOOP