wm: 5552

ref: a7be9c155a2bd5fae1b795c69695e3448d1778e9
dir: /5551a.c/

View raw version
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

/* ops */
enum
{
	DIE, /* drop dead */
	OUT,
	NOT,
	AND,
	NOR,
	XOR,
	SUB,
	ADD,
	SHR,
	SHL,
	PUT, /* stores something in memory */
	MOV, /* move stuff from a reg to memory */
	CPY, /* copy from a memory address to another */
	LDM, /* load from memory into register */
	NTH, /* returns the Nth bit */
};

int main(int argc, char* argv[])
{
	unsigned int inst, a, b, c;
	char string[32], *op;
	while(scanf("%s %[^\n;]%*c", op, string) > 0)
	{
		sscanf(string, " %u %u %u", &a, &b, &c);
		if(!strcmp(op, "DIE"))
			inst = DIE;
		if(!strcmp(op, "OUT"))
			inst = OUT;
		if(!strcmp(op, "NOT"))
			inst = NOT;
		if(!strcmp(op, "AND"))
			inst = AND;
		if(!strcmp(op, "NOR"))
			inst = NOR;
		if(!strcmp(op, "XOR"))
			inst = XOR;
		if(!strcmp(op, "SUB"))
			inst = SUB;
		if(!strcmp(op, "ADD"))
			inst = ADD;
		if(!strcmp(op, "SHR"))
			inst = SHR;
		if(!strcmp(op, "SHL"))
			inst = SHL;
		if(!strcmp(op, "PUT"))
			inst = PUT;
		if(!strcmp(op, "MOV"))
			inst = MOV;
		if(!strcmp(op, "LDM"))
			inst = LDM;
		if(!strcmp(op, "NTH"))
			inst = NTH;
	}
	switch(inst)
	{
		/* no args */
		case DIE:
				printf("%u\n", inst);
				break;
		/* one arg */
		case NOT:
		case OUT:
				printf("%u %u\n", inst, a);
				break;
		/* two args */
		case PUT:
		case MOV:
		case LDM:
			printf("%u %u %u\n", inst, a, b);
			break;
		/* three args */
		case AND:
		case NOR:
		case XOR:
		case SUB:
		case SHL:
		case SHR:
		case ADD:
		case NTH:
			printf("%u %u %u %u\n", inst, a, b, c);
			break;
		/* should this ever happen? */
		default:
		{
			fprintf(stderr, "unkown instr.\n");
			return -1;
		}
	}
	return 0;
}