Character on stack programming language(cos)

Hello world is "hello world" Z

The Z is a command that terminates the program execution.

COS Language

Character on stack programming language has 39 commands:


1. stacks: \$%@RDP
2. arithmetic: +-*/
3. input & output: .:,;WF"
4. flow control: <>L_![]
5. decisions: =?|
6. code: ()#Z' 0 to 9 a to z
7. memory: {}AM
8. miscellenious: BT

Parameters of cos: p means integer
c means source text character
a means integer array

Any ascii character including whitespace that is not part of cos
language is ignored by interpreter. Its presence generates no error. Small
letter ascii characters are used as parameters for some commands e.g variables.


Char Name Syntax Result on data stack

\ swap 2 3\ 3 2
$ duplicate 2$ 2 2
% drop 2 3% 2
@ rotate 2 3 4@ 3 4 2
R rstack 4R (4 is moved to return stack)
D dstack D 4 (4 is back on the data stack)
P pick 6 7 8 9 2P 6 7 8 9 7

Note: 0P is the same as $. And 1P is same as over in forth language

Cos has 5 stacks: source, data, return, variable, & array stack.
Return stack used to temporarily store values from data stack and
to store return addresses of function calls.

All stacks are integer stacks and are made from main stack. Therefore they
can be manipulated with same commands. E.g it's possible to fetch from the
array cells using P


Char Name Syntax Result on data stack

+ plus 2 3+ 5
- minus 'da'- 3 (ascii 100 - ascii 97=3)

  • times 7$* 49 (7 was duplicated 7*7=49)

/ divide 9 9 2#3/ 33 (99/3=33)

All arithmetic is integer. Therefore 5 2 / will give 2 not 2.5

To input negative number write zero,the number,&subtract e.g 09- (-9)

Cos uses a signed integer as basic data type. It is -2147483648 to 2147483647

Char Name Syntax Result

. printnumber 4. (the 4 will be printed on sreen)

printchar 7: (a beep will be heard)"1W

" printstring "Hello world!" (even whitespace characters are printed)
, inputnumber ,(only one number figure put on data stack. To get more
use more commas e.g ,,2# creates 2 digit number e.g 45)

inputchar (Inputs ascii char on datastack. Does not input string)

W window 0W (clears screen)
1W (prints newline)
pp2W (puts cursor at xy location on screen
e.g 9 8 2W will put cursor on
horizontal 9 and vertical 8)
F file 0F (Input all contents of file data stack)
ap1F (write chars into file e.g 'ek'21F
will write 2 chars into workfile)
ap2F (appends file. Similar syntax to 1F)
3F (puts size of file on datastack)
ap4F Assign name to workfile. Default is the
loaded sourcefile
Flow control

Char Name Syntax Result
< goleft c< (instruction pointer skips all letters
leftward till it finds the preceding letter
e.g 0b1+$.b< will print numbers infinitely)

> goright(skipright) same as skipleft
_ mark _c(optional) it marks a block in code L will jump to

L goto pL or cL 0L means goto beginning of source
finds nth or corresponding character
parameter underscore

! call function p! or c! Similar to goto but 0! impossible.

[ function [c(optional) Code within square brackets is function

] return ] Marks end of function. Control returned to
code that called it. In cos impossible to
nest functions but a function can call another
function or itself


Char Name Syntax Result

= equal pp= (puts 0 on stack if TOS is equal to NOS
1 if NOS is less than TOS
2 if NOS is greater than TOS)

? question(if) pp? (execute next command if NOS is equal
to TOS else execution skipped untill
| is found)

| endif | Execution always continues from here

Note: cos has no break or exit commands so to achieve break manually jump
the command after | by using brackets or skip or goto

Cos does not support decision nesting. Use gotos to achieve and, or etc logical

Char Name Syntax Result

( comment (comment) Code between the brackets not executed
It is possible to execute that code if
a flow command put instruction pointer
inside the brackets

  1. Number ap# Creates a number from a given array of

numbers e.g 9 8 7 3# will form 987
' Literal 'literal(s)' All asciis inside the single quotation
marks is put on data stack as literal
ascii characters. Can be one character
or many e.g 'a'(97) or 'b1c' (98 49 99)
Z End Z Stops code execution

Note: In cos 4g6k- the g and k will be ignored since they are not part of the
language. The execution will result in -2 as answer. Numbers should be separated
e.g 100 90 - will give 10 as an answer

Cos recognises 0 to 9 and it puts these chars on data stack. A number is always
entered as an array of numeral characters. To enter thirty six either write 36
or 6 6* or '$'($ is 36 ascii) or 3 6 2# or 6$*.

Char Name Syntax Result

{ assign pp{ or pc{ TOS integer is moved from data stack to an
address in the memory array
} fetch p} or c} Integer is copied from address in array memory
to be TOS data stack

A array app0A Assigns numbered integer into arraymemory cells
ppp1A Assign 1 integer into a memory array
pp2A Copies integers from array to data stack.
pp3A Copies 1 integer from array cells to data stack

M memory 0M machine call the workfile memory address
1M print the memory image of cos interpreter

Note: The character parameter is any char from a to z. There are 27 address
memory cells from 0 to 27 (0,a to z = 27 cells) From 28 that's arrays

Memory management is done by manipulating data stack. It contains program code, variables, arrays, primary stack, and return stack.

{, } can be used to self-modify code. Thus cos is turing complete.

The default address
variable stack: 0 to 27
array stack: 28 - programesize+45000
primary stack: 45027-programsize+30000
return stack: 75027-programsize+1000
source stack: 76027-programsize

stack(0) 0 1399428
stack(76027) 76027 1703536
origin: 76028 1703540
rp: 76029 1703544
sp: 76030 1703548
ip: 76031 1703552
tc: 76032 1703556
sc: 76033 1703560
fc: 76034 1703564
ansn: 76035 1703568
an: 76036 1703572
ab: 76037 1703576
ac: 76038 1703580
workfile: 76041 1703592


Char Name Syntax Result

B boolean pp0B NOS and TOS
pp1B NOS or TOS
p2B not TOS

shift pp3B NOS shiftleft TOS
pp4B NOS shiftright TOS

T time 0T Puts time on data stack
p1T Random number from 0 to p
p2T Pauses execution for a p

p3T p is number of microsends to
pause. Afterward a key in
keyboard buffer is fetched
if no key then zero is stacked

This category currently contains no pages or media.