From Esolang
(Redirected from M-code)
Jump to navigation Jump to search

M-Code is a Turing-complete esoteric programming language designed by User:Madk.

M-Code takes a lot of inspiration from 6502 assembly, brainfuck, and then it tosses a handful of features of its own. Every character in a source file represents a single value, and numerical values can be specified inside [] brackets. There are three variations: 8 bit M-Code, 16 bit M-Code, and 32 bit M-Code. The higher bit variations were introduced to allow a larger tape size than a mere 256 bytes. All values in each bit mode are that number of bits. 32 bit values are signed.

Key Features

  • Source code is stored in an accessible memory bank, making self-modification important to many functions.
  • 3 value register
  • File access
  • Relatively robust arithmetic and logic operands

Instruction Set


These are the single-character commands recognized by the interpreter. All commands and symbols use 1 value, arguments must immediately follow commands. Program execution starts at memory address 0.

Register handling for the A, B, C registers. > puts a value into the register and < takes it out.
> < 
} { 
) ( 
arguments: [memory address]

Store a literal value in the A, B, C registers
4 5 6
arguments: [number]

Handle the string in memory by printing it, getting it from input, or swapping it with the secondary (inactive) memory string
; \ ~

Clear the string in memory

Read characters from the memory string and put them in the A, B, C registers
^ ` '
arguments: [position in memory string]

Treat the memory string as a number and put it in the A, B, C register.

Write characters to the end of the memory string from the A, B, C registers
: . ,

Output the numerical value onto the end of the memory string
1 2 3

Store the length in characters of the string in memory to register A, B, C
7 8 9
Copy a byte, swap two bytes in memory from one place to another
# S
arguments: [origin memory address] [second memory address]

Increment, decrement a byte in memory
i d

increment, decrement the value in the A, B, C registers
o n
u t
f e

Jump unconditionally
arguments: [jump to address]

Jump if A > B
arguments: [jump to address]

Jump if A < B
arguments: [jump to address]

Jump if A = B
arguments: [jump to address]

Jump if A != B
arguments: [jump to address]

Swap A and B registers

Swap B and C registers

Swap C and A registers

Set A, B, C register to 0
a b c

Perform a logical NOT operation on registers A, B, C

Copy the value in the A register into both B and C

Shift a value in memory left, right by one bit
arguments: [memory address]

Perform an operation on A and B and place the result in C
Add:			+
Subtract:		-
Divide: 		/
Multiply:		*
Modulo:		%
Raise to power:	v
Bit shift left:	l
Bit shift right:	r
Mean:			m
Logical OR:		O
Logical AND:		&
Logical XOR:		X 

Creates a new external data file if one doesn't already exist. It opens it into memory if it does.
arguments: [file #] 

Write a value from a program memory address into a file
arguments: [write from memory address][write to file address]

Read a byte from a file and store it in program memory
arguments: [write to memory address][read from file address]

Read a file's entire contents and store it all in program memory starting at a given location
arguments: [write to memory address]

Print memory string to console without a newline (with the exception of the inclusion of an actual newline character)

Print a newline to the console

Delay program execution for some number of milliseconds
arguments: [milliseconds]

Output a number, character to the end of the memory string from a memory address
0 @
arguments: [memory address]

Kill program

Non-runtime parser instructions

These are interpreter instructions. They aren't processed in memory at runtime, but aid in the writing of code and pre-runtime memory management.

// (...) // 

Reference a label and store its position as a value in this memory address
Define a label mid-line

Define a value numerically instead of an ASCII character. For example, [65] and "A" and also [35] and "#" would be interchangeable.

Set string memory contents before runtime - first instance sets first string, second instance sets second string.

Set the current location in memory that the following data will start being written at. It must be numerical.

Define a label based on this line's position in the source file. 
It cannot start with a digit (0-9), "$", or a quote ("). It cannot contain "=". Labels are case-insensitive.

Simultaneously define a label and the contents of the immediate memory address. 
The assignment should be formatted no differently than typical program data. Example: $pound = # OR $pound = [35]

Count whitespace as valid characters in this line of code, with the exception of preceeding and following space. 
(...) $

Self-reference this address in memory - if [$$] were at the very beginning of a program, 
it would be equivalent to [0]. At $42, [$$] would be [42].

Put a $ character where the parser would normally see it as a non-runtime command 
(at the very beginning or very end of a line, excluding whitespace)

Put a [ character where the parser would normally see it as a non-runtime command

Put a | character where the parser would normally see it as a non-runtime command


Hello, world!

// Print "Hello, world!" to the console. //

4|data end|	// Store the location of the last character, "!", in register A. //
j|main|         // Jump to the main loop start // 

$ main		// This is a label signifying the beginning of the main loop. //
}|$pos||data|	// Push "H" into register B from the start of the "Hello, world!" string. $pos is a label definition. //
.		// Add "H" from B to the end of the memory string. //
i|pos|		// Increment the number in }|data| by one, so next loop it'll push "e", then "l", (...) //
}|pos|		// Push that same number into B to get ready for a comparison. //
?|main|         // Jump back to |main| to create a loop if the value in register A is > B to know if we've reached the end of "Hello, world!". //

;		// After the above loops has finished, ; prints the accumulated string to the console. //
_		// Terminate program execution. //

$data		// Label the location of the string data in memory //
Hello,[32]world!// Whitespace is not recognized by the parser, [32] is the ASCII code for a space. //
$data end	// This is a label to provide the ending point of the "Hello, world!" data. //

Much less elegant Hello, world!

$"Hello, world! I'm a big fat cheater.

99 bottles of beer

// This program prints the lyrics to "99 bottles of beer". 
   It exceeds the program memory space offered by 8-bit
   M-Code and therefore is incompatible. //


Fibonacci sequence

// Calculate the first several numbers of the Fibonacci sequence. //

// A & B are added together each loop to calculate C, a member of the sequence. 
   A then becomes B and B becomes C and the next number is calculated next cycle. //

5[1]			// The A and C registers start at 0. The B register needs to be set to 1. //

$ loop start		// This label indicates the main loop initiation. //
3;s			// Output the value in C to the console. //
< |memoryA| { |memoryB|	// Store the values in the A and B registers into memory so we can do an
			   end-of-loop check. //
d |sequence|		// |sequence| is the label dictating how many numbers of the sequence to show.
			   Here it is decremented by 1 each loop. //
> |sequence| b		// Store |sequence| in the A register and 0 in B for comparison. //
= |program end|		// If A=B (sequence=0) jump to the program's end. //
> |memoryB| } |memoryA|	// Restore the previous states of A and B, but also swap them in the process. //
y			// Swap the contents of the B and C registers. //
+			// Add A and B and put the result in C. //
j |loop start|		// Go back to the loop start label and do it all again. // 

$ memoryA		// Used to temporarily store the A register during the loop. //
$ memoryB		// Used to store the B register. //
$ sequence		// Iterate the loop |sequence| number of times. The 15th member of the //
[14]			// sequence exceeds 255, so only the first 14 are calculated. //

$ program end		// The loop jumps here when it has completed |sequence| cycles. //
_			// Program termination command //

Collatz sequence

// Calculate a Collatz sequence from a starting amount. //
// This program is _not_ intended to be run in the 8-bit version of M-Code. //

$"Input a number to determine its Collatz sequence.
$"Number of cycles:



Output ASCII table

// Prints ASCII table to the console //



In order to keep readable ASCII characters for memory address representations, the program memory storage starts at 35.

// This program outputs its source to the console. 
   To maintain readable characters, the first symbol 
   is located at $35 within program memory. //








Brainfuck Interpreter

While this interpreter is designed with 8-bit M-Code in mind and has a maximum code size of 256 commands and a tape size of only 32, these values can be expanded into greater than 60,000 or over a million commands and tape by changing the wraparound code at the end and using the 16-bit or 32-bit M-Code interpreters, respectively. It shows through simulation that M-Code, if given infinite memory, is Turing-complete.

// Interpret a brainfuck script. //


$"Please wait, this may take a while :P

j |main loop|

	$ >
	$ <
	$ +
	$ -
	$ .
	$ ,
	$ [
	$ ]
	$ #

$ tape

$ main loop 

$ code

$ check >
} |>|
N |check <|
i |tape|
j |end|

$ check <
} |<|
N |check +|
d |tape|
j |end| 

$ check +
} |+|
N |check -|
# |tape||+arg|
$ +arg
[ 0 ]
j |end|

$ check -
} |-|
N |check .|
# |tape||-arg|
$ -arg
[ 0 ]
j |end|

$ check .
} |.|
N |check ,|
# |tape||.arg|
$ .arg
[ 0 ]
j |end|

$ check ,
} |,|
N |check [|
# |tape||,arg|
$ ,arg
[ 0 ]
j |end|

$ check [
} |[|
N |check ]|
# |tape||[mod|
$ [mod
[ 0 ]
4 [0]
N |end|
4 [1]
< |nest|
$ [start
4 [0]
} |nest|
= |end|
i |code|
# |code||[char|
$ [char
[ 0 ]
} |[|
N |[2|
i |nest|
$ [2
} |]|
N |[start|
d |nest|
j |[start|

$ check ]
} |]|
N |check #|
4 [1]
< |nest|
$ ]start
4 [0]
} |nest|
d |code|
= |end|
# |code||]char|
$ ]char
[ 0 ]
} |[|
N |]2|
d |nest|
$ ]2
} |]|
N |]start|
i |nest|
j |]start|

$ check #
} |#|
N |end|

i |code|
> |code|
5 [255]
N |nc|
# |const1||code|
$ nc
5 [221]
N |main loop|
# |const2||code|
j |main loop|

$ const1
$ const2


External resources