NANDNZ

From Esolang
Jump to navigation Jump to search

NANDNZ is an OISC inspired by Subleq.

Overview

NANDNZ takes 3 arguments:

A B C

It NANDs values at A and B, stores the result in A, then jumps to C if the result is not 0, otherwise, it proceeds to the next instruction. If it jumps into a negative address, the program halts.

I/O extension

If A is -1, it inputs a character into B. If B is -1, it outputs A as a character.

Examples

Hello, world!

0 0 3
5 -1 72
8 -1 101
11 -1 108
14 -1 108
17 -1 111
20 -1 44
23 -1 32
26 -1 119
29 -1 111
32 -1 114
35 -1 108
38 -1 100
41 -1 33
0 0 -1

Cat program

-1 2 0
2 -1 1
5 8 0

Implementations

C

#include <stdio.h>
long int code[] = {-1, 2, 0, 2, -1, 1, 5, 8, 0}, ip;

int main(void)
{
  while (ip >= 0)
  {
    if (code[ip] >= 0 && code[ip + 1] >= 0)
    {
      code[code[ip]] = ~(code[code[ip]] & code[code[ip + 1]]);
      if (code[code[ip]] != 0)
        ip = code[ip + 2];
      else
        ip += 3;
    }
    else if (code[ip] == -1 && code[ip + 1] >= 0)
    {
      code[code[ip + 1]] = getchar();
      ip += 3;
    }
    else if (code[ip + 1] == -1 && code[ip] >= 0)
    {
      putchar(code[code[ip]] & 0xFF);
      ip += 3;
    }
    else
    {
      return 1;
    }
  }
  
  return 0;
}