Assembly language

From Esolang
Jump to navigation Jump to search
Not to be confused with assembly code, machine code, nor Assembly.

Assembly language is a Harvard-architectured computer machine code invented by User:A. It has all the basic features of a modern computer. The machine code is inspired by Little Man Computer. The machine code is extending.

Associate it with machine code, not assembly language. This is called assembly language because the example programs are written in an assembly language-like syntax.

There is an array with indexes from 00 to FF. Index 00 is the input/output number index. Each index in the array can only store a positive ASCII hex number(0~255).

Components (Very trivial)

Control Unit: DAT, MOV, JZ, JMP
Arithematic/Logic Unit: ADD. SUB
I/O:The 00 cell
Instruction memory:The loaded program
Data memory:The array

Commands

DAT XX YY(Hexadecimal: DA XX YY):
It loads the the hexadecimal number YY into XX.

ADD XX YY(Hexadecimal: AD XX YY):
XX+=YY
It increments the XXth index by the value in index YY.

SUB XX YY(Hexadecimal: 2C XX YY):
XX-=YY
It decrements the XXth index by the value in index YY.

SO XX YY(Hexadecimal: 20 XX YY):
Sequentially outputs all the characters between the index XX and YY, including XX and YY.

MOV XX YY(Hexadecimal: 30 XX YY):
XX=YY
Sets the XXth index as the value in the index YY. If 00 is set to anything, the content will be outputted.
If a value is taken from 00, the value will be a number from input.
Likewise with 11, yet a byte is inputted and outputted instead.

JZ XX YY(Hexadecimal: 12 XX YY):
Jumps to the XXth byte if the value of index YY is zero.

JMP XX(Hexadecimal:13 XX)
Jumps to the XXth byte.

HLT(Hexadecimal: 00):
End the program.

DB(Hexadecimal: DB)
Create a database. Usage: the 1st parameter is the beginning cell for the database. There are various numbers behind it.
DB tries to put every number sequentially before it scans to Hexadecimal 00(The "end the program" listed above).

Turing-completeness

Though the cells may seem bounded, they are actually not bounded. The 3-celled unbounded-celled Brainfuck in Collatz function can easily compile to it.

Examples

To execute them, you have to pretend to be the assembler and type the code in "machine code".

This program takes two inputs and outputs the difference.

MOV 01 00
MOV 02 00
SUB 01 02
MOV 00 01
HLT

This program will take a user input and count down to zero.

DAT 02 01
MOV 01 00
MOV 00 01
JZ 14 01
SUB 01 02
MOV 00 01
JMP 09
HLT

This program will take a user input, square it, output the answer and repeat. Entering a zero will end the program.

DAT 04 01
MOV 01 00
JZ 1F 01
MOV 02 01
MOV 03 01
SUB 02 04
ADD 01 03
JZ 1B 02
JMP 09
MOV 00 01
JMP 03
HLT

This program prints Hello World.

DAT 01 48
MOV 00 01
DAT 01 65
MOV 00 01
DAT 01 6C
MOV 00 01
MOV 00 01
DAT 02 6F
MOV 00 02
DAT 01 20
MOV 00 01
DAT 01 77
MOV 00 01
MOV 00 02
DAT 01 72
MOV 00 01
DAT 01 6C
MOV 00 01
DAT 01 64
MOV 00 01

This is a demonstration of comparison (If a is equal to b, print 0. Else, print 1).

MOV 01 00
MOV 02 00
MOV 03 01
SUB 03 02
JZ 15
DAT 01 01
MOV 00 01
HLT
DAT 01 00
MOV 00 01
HLT

This is a switch...case... procedure. Inputting 0 results a 15 output in hex. 1:24; 2:19;3:20; Otherwise nothing.

DAT 30 00
DAT 31 01
DAT 32 02
DAT 33 03
MOV 01 00
MOV 02 01
SUB 02 30
JZ 2F
MOV 02 01
SUB 02 31
JZ 36
MOV 02 01
SUB 02 32
JZ 3D
MOV 02 01
SUB 02 33
JZ 44
DAT 12 15
MOV 00 12
HLT
DAT 12 24
MOV 00 12
HLT
DAT 12 19
MOV 00 12
HLT
DAT 12 20
MOV 00 12
HLT

99 bottles of beer program (currently incomplete)

A demonstration that this is usable (by implementing the 99 bottles of beer program)

The DB command is in newlines just for convenience.

;Cell 1 is reserved for the count variable
DB 02 20;' ', #2
62;'b', #3
6F;'o', #4
74;'t', #5
74;'t' #6
67;'l' #7
65;'e' #8
73;'s' #9
20;' ' #A
6F;'o' #B
66;'f' #C
20;' ' #D
62;'b' #E
65;'e' #F
65;'e' #10
72;'r' #11
20;' ' #12
6F;'o' #13
6E;'n' #14
20;' ' #15
74;'t' #16
68;'h' #17
65;'e' #18
20;' ' #19
77;'w' #1A
61;'a' #1B
6C;'l' #1C
6c;'l' #1D
2C;',' #1E
2E;'.' #1F
0A;'\n' #20
54;'T' #21
61;'a' #22
6B;'k' #23
65;'e' #24
20;' ' #25
6F;'o' #26
6E;'n' #27
65;'e' #28
20;' '#29
64;'d'#2A
6F;'o' #2B
77;'w' #2C
6E;'n' #2D
2C;',' #2E
20;' ' #2F
70;'p' #30
61;'a' #31
73;'s' #32
73;'s' #33
20;' ' #34
69;'i' #35
74;'t' #36
20;' ' #37
61;'a' #38
72;'r' #39
6F;'o' #3A
75;'u' #3B
6E;'n' #3C
64;'d' #3D
2C;',' #3E
0A;'\n' #3F
4E;'N' #40
6F;'o' #41
HLT ; Ends the database, not "end the program". And #42
DAT 01 63; initialize cell 1 as 99. #45
MOV 00 01; (integer) #48
SO 03 08;bottle #51
DAT 60 01;the minus-one variable. #54
MOV 59 01;#57
SUB 59 60;#5A
JZ 59 61;#5D
MOV 11 73;#60
SO 0A 1E; of beer on the wall, #63
MOV 11 20;\n #66

Machine code interpreter in C

This is in 84 lines.

#include <stdio.h>
int main(int argc, char *argv[])
{
	int code[99999],a[256];
	char c;
	FILE *fp=fopen(argv[1],"r");
	for(int i=0;(c=fgetc(fp))!=EOF;i++)
		code[i]=c;
	for(int i=0;code[i]!='\0';i++)
	{
		if(code[i]=='\x30')
		{
			i++;
			if(code[i]=='\0')
				printf("%d ",a[code[i+1]]);
			else if(code[i+1]=='\0')
				scanf("%d",&a[code[i]]);
			else if(code[i]=='\x11')
				printf("%c",a[code[i+1]]);
			else if(code[i+1]=='\x11')
				scanf("%c",&a[code[i]]);
			else
				a[code[i]]=a[code[i+1]];
			i++;
		}
		else if(code[i]=='\x20')
		{
			i++;
			int cnt;
			cnt=code[i];
			while(cnt<=code[i+1])
			{
				printf("%c",a[cnt]);
				cnt++;
			}
			i++;
		}
		else if(code[i]=='\xDA')
		{
			i++;
			a[code[i]]=code[i+1];
			i++;
		}
		else if(code[i]=='\xDB')
		{
			i++;
			int cnt=code[i];
			while(code[i]!='\x00')
			{
				a[cnt]=code[i+1];
				i++;
				cnt++;
			}
		}
		else if(code[i]=='\xAD')
		{
			i++;
			a[code[i]]+=a[code[i+1]];
			i++;
		}
		else if(code[i]=='\x2C')
		{
			i++;
			a[code[i]]-=a[code[i+1]];
			i++;
		}
		else if(code[i]=='\x13')
		{
			i=code[i+1];
			i--;
		}
		else if(code[i]=='\x12')
		{
			i++;
			if(!a[code[i+1]])
			{
				i=code[i];
				i--;
			}
			i++;
		}
	}
	return 0;
}