Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: M-Code esoteric programming language  (Read 711 times)

madk

  • Bay Watcher
    • View Profile
    • pineapplemachine
M-Code esoteric programming language
« on: July 18, 2010, 04:09:06 pm »

Hello, world!
Code: [Select]
>[64]j[8]
$8
}[32].i[9]}[9]?[8];_
$32
Hello,[32]world!
$64
[45]
Code: [Select]
// Program execution starts at 0. //

>[64] // Store 45 in register A from memory address 64 to frequently be used as a constant. //
j[8] // Jump to memory address 8 //


$8 // This tells the parser that the following code is intended to start at memory address 8. //
}[32] // Push "H" into register B from address 32. //
. // Add "H" from B to the end of the memory string. //
i[9] // Increment the 32 in }[32] by one, so next loop it'll push "e", then "l", (...) //
}[9] // Push that same number into B to get ready for a comparison. //
?[8] // Jump back to $8 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. //

$32 // Set the memory address to 32. //
// Whitespace is not recognized by the parser, [32] is the ASCII code for a space. //
Hello,[32]world!

$64 // Set the address to 64. //
[45] // This is for indicating where the "!" in "Hello, world!" is located. //

Fibonacci sequence:
Code: [Select]
>[64]1;s>[65]1;sj[16]
$16
d[66]>[66]b=[96]>[64]}[65]+3;s([68]#[65][64]#[68][65]j[16]
$64
[0][1][15]
$96
_
Code: [Select]
// Program execution starts at 0. //

>[64] 1;s // Store 0 in register A and print it to the console. //
>[65] 1;s // Store 1 in A and print it. //
j[16] // Jump to memory address 16. //

$16 // Set address to 16. This is our main loop start. //

d[66] // Decrement the value at memory address 66 (15) by one. //
>[66] // Push that value into register A. //
b // Set register B to equal 0. //
=[96] // If A = B (A = 0) jump to memory address 96, where a program termination command is located. //

>[64] // Push the first value into A. //
}[65] // Push the second into B. //
+ // Add the first and second values (A+B) and put the result in register C. //
3;s // Print the resulting sum to the console. //
([68] // Pop the result into memory address 68. //
#[65][64] // Shift the second number into where the first was. //
#[68][65] // Shift the new resulting number into where the second was. //

j[16] // Jump back to the loop's start at address 16. //

$64 // Set address to 64, these memory addresses are used as variables in calculation. //
[0][1] // First value of Fibonacci sequence starts as 0, second starts as 1. //
[15] // Number of iterations (n-1) of the sequence to go through. //

$96 // Set address to 96. //
_ // Program termination command. //

Command reference:
Code: [Select]
Push, pop for the A, B, C registers
> <
} {
) (
arguments: [memory address]

Handle the string in memory by printing it, getting it from input, and swapping it with a second memory string
; \ ~

Clear the string in memory
s

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

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

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
h g
u t
e d

Jump unconditionally
j
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
N
arguments: [jump to address]

Swap A and B registers
x

Swap B and C registers
y

Swap C and A registers
z

Set a register to 0
a b c

Perform a logical NOT operation on registers A, B, C
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
L R
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: A
Logical XOR: X

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

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

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

Read the entire contents from a file and store it in program memory starting at a given address
P
arguments: [begin writing to memory address]

Kill program
_

Comment
// (...) //

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

Nonfunctional BF interpreter :P
Code: [Select]
// Program code //

">>>++.<<<.#
// Please use a "#" to indicate EOF. //

// BrainF*ck interptreter //


j[20]


$4
 // Constants //
[62] // 04: > //
[60] // 05: < //
[91] // 06: [ //
[93] // 07: ] //
[43] // 08: + //
[45] // 09: - //
[46] // 10: . //
[48] // 11: , //
[35] // 12: # (EOF) //
 // Variables //
[0] // 13:    Current in-code position //
[96] // 14:    Current tape position //
[0][0][0][0] // 15-18: Last 4 [ positions //
[0] // 19:    Current [] nest //

// memory address 20 //


$20

^[13] // Push the command into A //

$22
}[04] // Push ">" into B //
N[30]
i[14]
j[144]

$30
}[05] // Push "<" into B //
N[38]
d[14]
j[144]

$38
}[08] // Push "+" into B //
N[49]
#[14][46]
i[ 0 ]
j[144]

$49
}[09] // Push "-" into B //
N[60]
#[14][57]
d[ 0 ]
j[144]

$60
}[10] // Push "." into B //
N[74]
#[14][68]
)[ 0 ]
~,;s~
j[144]

$76
}[11] // Push "," into B //
N[88]
~\~
#[14][87]
'[ 0 ]

$88
}[06] // Push "[" into B //
N[108]
i[19]
S[18][17]
S[17][16]
S[16][15]
#[13][15]
j[144]

$108
}[07] // Push "]" into B //
N[139]
}[0]
#[14][118]
>[ 0 ]
N[134]
d[19]
#[16][15]
#[17][16]
#[18][17]
j[144]
#[15][13]
j[144]

$139
}[12] // Push "#" (EOF) into B //
N[144]
_




$144
i[13] // increment position in code //

// keep tape address within bounds //
>[13]
}[170]
N[157]
#[172][13]
j[20]
$157
}[171]
N[20]
#[173][13]
j[20]
$166

$170
[0][190][191][255]

// Tape spans from $191 - $255 //


Download

reference.txt for a command reference, readme.txt for instructions on how to use it.

I must have feedback!
« Last Edit: July 18, 2010, 04:16:51 pm by madk »
Logged

madk

  • Bay Watcher
    • View Profile
    • pineapplemachine
Re: M-Code esoteric programming language
« Reply #1 on: July 19, 2010, 02:55:00 pm »

"Hello, world!"

Code: [Select]
">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>>>++++++++[<++++>-]<.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+.#
"Please wait, this may take a while :P
~;s~j[16]><+-.,[0]#[223]^[1]}[6]N[26]i[15]j[172]}[7]N"d[15]j[172]}[8]
N-#[15]*i[0]j[172]}[9]N8#[15]5d[0]j[172]}[10]NH#[15]@)[0]~s,;~j[172]}[11]
NX~\'[1]~#[15]U([0]j[172]}[12]N[132]#[15]`}[0]4[0]N[172]4[1]<[194]4[0]}[194]
=[172]i[17]#[17]u^[0]}[12]N[0]4[0]}[194]d[17]=[172]#[17][152]^[0]}[12]N[159]
d[194]}[13]N[140]i[194]j[140]}[14]N[172]_i[17]>[17]5[255]N[183]#
[192][17]5[221]N[16]#[193][17]j[16][222][254]

BrainF*ck interpreter GET.