PZAB

From Esolang
Jump to navigation Jump to search

PZAB is a programming language created by Zac Herd in 2013. The idea was to create a language that forced the programmer to manipulate memory and registers on a binary level. PZAB, like all good esoteric languages, was designed to be essentially unreadable.

Language Overview

The language is based around a simple computer architecture, in which memory is arranged into a one dimensional array of 'cells' that each contain a value. The interpretation of those values is entirely up to the programmer.

The PZAB language is written in such a way that the programmer is addressing a virtual machine. This machine has 256 bytes of memory and five registers.

PZAB Machine

A diagram showing the PZAB machine

The P0 register points to a cell in memory. If P0 was set to 00000000(0) then it would be pointing to memory cell 0, the first cell of memory. Similarly, if it was set to 11111111(255) then it would be pointing to cell 255, the last.

The P1 register points to a bit in a memory cell. If P0 was set to 00000000(0) and P1 was set to 000(0), then we would be referencing bit 0 of memory cell 0. If P0 was set to 11111111(255) and P1 was set to 111(7), we would be referencing bit 7 of memory cell 255.

The Z register is used as an accumulator. It only holds one bit, and cannot be assigned to directly. Rather, it takes the value of the results of logical operations performed on it and the A and B registers. The Z register can be used to assign values to bit P1 of memory cell P0, or to assign values to A and B directly. Z can have the logical NOT operation performed on it.

The A and B registers are essentially the inverse of Z; they can be assigned values, but their contents cannot be assigned to anything else. A and B can have logical operations performed on them (AND, OR, XOR) and the results placed in Z.

All memory and registers are initialised with a value of zero.


PZAB Instruction Set

All PZAB instructions are single ASCII characters. All 'non PZAB' characters are ignored by the interpreter (this includes spaces, tabs, line breaks, Etc.).

The instructions for the PZAB interpreter are as follows:

+/- Increments/Decrements P0
</> Increments/Decrements P1
A Assigns bit P1 of cell P0 to A
B Assigns bit P1 of cell P0 to B
a Assigns Z to A
b Assigns Z to B
Z Assigns Z to bit P1 of cell P0
! Performs a logical NOT on Z
& Performs a logical AND on A and B (result is stored in Z)
| Performs a logical OR on A and B (result is stored in Z)
_ Performs a logical XOR on A and B (result is stored in Z)
. Gets integer input from user and stores it in cell P0
, Gets ASCII character input from user and stores it in cell P0
: Outputs cell P0 as integer
; Outputs cell P0 as ASCII character
? Only executes code up to matching '~' if Z == 0
~ See '?'
1-9 Executes code up to matching '0' this many times (1 loops forever)
0 See '1-9'

Example Code

Hello World:

<<<Z<<<Z;
>Z>>!Z!>Z>>Z;
!Z!<<Z<Z;;
>>Z>Z;
!Z<Z<<<<<Z;
>>>Z>Z!;
Z>Z>Z<<<<Z<<Z;
!>>Z!>Z;
!Z>Z>>Z!<<<<Z;
!Z!>Z>Z>!Z;
<<Z;
<<<Z>>>>Z!>>Z;

Adder (accepts two integers and calculates their sum):

.+.-
A+B_++Z-&Z
7--<A+B&+Z_a>B<_+Z&-aB|Z0
+:

Subber (accepts two integers and subtracts one from the other):

+++++.-----.
8A|!+Z-<0
++>>>>>>>ab_!Z-
A+B_++Z-&Z
7--<A+B&+Z_a>B<_+Z&-aB|Z0
>>>>>>>+A+B_++Z-&Z
7--<A+B&+Z_a>B<_+Z&-aB|Z0
+:

Implementations