EFOS Playground
Write a small program, run it on EFVM, the virtual processor of EFOS, and watch registers, memory and output change one instruction at a time. Write EFASM assembly directly, or write efclang, the EFOS Easy & Flexible language, and see the assembly it becomes.
How to use this playground about a minute to read
Get going
- Pick a sample from the menu, or type your own program in the editor.
- Press Run to execute it. Text the program prints appears in Console output.
- Press Step to run one instruction at a time. The yellow line in the editor is the instruction about to run. Step back undoes one step.
- Click a row in Code to set a breakpoint (a red dot). Run stops there.
- Press Reset to start the program again from the top.
What you are looking at
- Registers R0 to R7 are the CPU's eight 32-bit variables. R7 is the stack pointer. A register that just changed is highlighted.
- ZF / NF are flags set by
CMP: zero and negative. Branches likeBEQandBLTread them. - Memory shows the program's bytes. Strings you define with
DZstart at address 0. Use Data and Stack to jump around. - Your edits are saved in this browser only. Share copies a link that holds your program inside the link itself, so anyone who opens it sees the same code. Nothing is stored on any server.
Things to try
- New: press efclang above, open fizzbuzz and Run. Then open Show the assembly this program became and Step through it.
- Open countdown, change
#5to#9, and Run. - Step through it and watch R1 fall while the flags change at each
CMP. - Open hello, then edit the text in the quotes.
- In fib, change
#12to print more numbers. - Write your own: add the numbers 1 to 10 and print the sum.
- Make a mistake on purpose. The assembler names the line and column.
Write in efclang
- Pick a sample from the menu: hello is the shortest, fizzbuzz and primes use loops and functions.
- Press Run. The page turns your program into machine code and runs it. What it prints appears in Console output.
- Edit the program and press Run again. A mistake is reported with its line and column.
- Open Show the assembly this program became under the editor to read the machine code the compiler wrote.
- Step runs one machine instruction at a time and highlights the line of your program it belongs to.
Things to try
- In fizzbuzz, change
20to40and Run. - In primes, change
100to300. - In fib, Step into a call and watch R7 (the stack pointer) fall in the Machine panel.
- Replace the program with this one and Run:
fn main() { var s = 0; for (var i = 1; i <= 10; i += 1) { s += i; } print(s); return 0; } - Pick mandelbrot (picture) or circles (picture) and Run: a Screen panel appears below the console. In lines (picture), change
colour(i)and watch the fan change. - Pick paint (interactive) or snake (interactive), press Run, then click the picture and use the mouse or the arrow keys.
- Draw your own:
fn main() { screen(32, 32); drawline(0, 0, 31, 31, 0xFF0000); return 0; } - Make a mistake on purpose, such as using a name you never declared.
efclang reference the code you write when you choose "efclang"
| You write | What it means |
|---|---|
| fn main() { ... return 0; } | Every program starts in main. What it returns becomes the exit code. |
| var x = 5; | x = x + 1; | x += 1; | Variables hold whole numbers (32-bit, can be negative). Declare them with var, inside a function or at the top. |
| const N = 10; | A name for a fixed number, worked out when the program is compiled. |
| var table[16]; table[i] = 3; | An array of 16 numbers, counted from 0. Declared at the top level only. Stay within the size: nothing checks it. |
| if a < b { ... } else { ... } | Choose. No parentheses needed. Compare with == != < <= > >=; combine with && || !. |
| while cond { ... } | for (var i = 0; i < 10; i += 1) { ... } | Repeat. break; leaves a loop, continue; skips to the next round. |
| fn add(a, b) { return a + b; } | Your own functions, with any number of inputs. They can call themselves. |
| + - * / % & | ^ ~ << >> | Arithmetic and bit operations. / and % round toward zero. Numbers can be written 255, 0xFF or 'A'. |
| print(x) | write(x) | putc(65) | puts("hi\n") | Print a number and newline; a number alone; one character; a piece of text. |
| exit(3); | End the program at once with that exit code. |
| screen(64, 48); | Open a drawing screen 64 dots wide and 48 high. Dots are counted from the top-left corner, (0, 0). Everything else needs this first. |
| cls(0x101820); | Fill the whole screen with one colour. Colours are written 0xRRGGBB: red, green, blue, two hex digits each (0xFF0000 is red). |
| pixel(x, y, colour) | drawline(x1, y1, x2, y2, colour) | Colour one dot; draw a straight line between two dots. |
| drawrect(x, y, w, h, colour) | fillrect(x, y, w, h, colour) | Draw the outline of a box, or a solid box. Parts outside the screen are cut off, not an error. |
| present(); | closescreen(); | Show the finished picture and wait for the next screen refresh (about 60 times a second, so a loop that calls present() once per round runs at a steady speed); close the screen. Each drawing command returns 0 when it worked and a negative number when it did not (for example, no screen yet). |
| while nextinput() { ... input(0) ... } | Read the keyboard and mouse. nextinput() takes the next event (1 if there was one, 0 if not); input(n) then reads its parts: 0 kind (1 key, 2 mouse), 1 action (1 press, 2 release, 3 move, 4 wheel), 2 key or button code, 3 character or wheel step, 4 and 5 mouse x and y, 6 shift/ctrl/alt bits. Letters and digits give their capital letter or digit as the code, Enter 13, Esc 27, arrows 257 to 260. |
| inputcount() | inputinfo(i, n) | How many events are waiting; peek at part n of the ith waiting event without taking it. |
The CPU can only add, subtract and compare, so the compiler writes extra routines for *, /, % and shifts. Open Show the assembly this program became under the editor to see everything it generated, and use Step to follow it. Limits: the whole program must fit in 2047 instructions and 2 KB of data. Screens up to about 4 million dots work in this page.
Assembly quick reference instructions and system calls
| Instruction | What it does |
|---|---|
| MOV Rd, #n | MOV Rd, Ra | Put a number, or another register's value, into Rd. A label such as message gives its address. |
| ADD / SUB Rd, Ra, Rb|#n | Rd = Ra plus or minus a register or a number. |
| AND / OR / XOR Rd, Ra, Rb | Bitwise operations on whole registers. |
| CMP Ra, Rb|#n | Compare. Sets the flags; changes no register. |
| JMP / BEQ / BNE / BLT / BLE / BGT / BGE label | Jump to a label: always, or if equal, not equal, less, less or equal, greater, greater or equal (after a CMP). |
| CALL label | RET | Call a routine and return from it (uses the stack). |
| LD / LDB Rd, [Ra] or [Ra+n] | Load a 32-bit word, or one byte, from memory. |
| ST [Rd] or [Rd+n], Rb | Store a register into memory. |
| OUT Rd | Print a register as a number on its own line. |
| SYS #n | Ask the system to do something (see the calls below). |
| HALT | Stop the program. |
| label: | Names this spot. Comments start with ;. Numbers start with # (#10 or #0xFF). |
| DZ name, "text" | DB name, #n, ... | Define text (ending in zero) or raw bytes in memory. Escapes: \n \t \" \\ \xNN. |
| System call | Inputs | What it does |
|---|---|---|
| SYS #34 | R0 = number | Print R0 as a number (no newline). |
| SYS #35 | Print a newline. | |
| SYS #2 | R0 = character code | Print one character. |
| SYS #4 | R0 = 1, R1 = address | Print the zero-ended string at that address. |
| SYS #1 | R0 = 1, R1 = address, R2 = length | Print that many bytes. |
| SYS #33 / #37 / #38 | R0 = size, or address | Allocate, resize and free memory. |
| SYS #5 / #6 | R0 = to, R1 = value or from, R2 = length | Fill or copy memory. |
| SYS #0 | R0 = exit code | End the program. |
Program
Show the assembly this program became
Console output
Screen
Keys and mouse over the picture go to the program (click it first). Click the editor to type there again.