NetRun Executable Examples

Note that these examples are truly not the best way to learn a new language, but at least they're a place to start!

Compiled Languages

C

int i;
printf("Hello, world!\n");
for (i=0;i<10;i++) {
printf("i=%d\n", i);
}

(Try this in NetRun now!)

C++

std::cout<<"Hello, world!\n";
for (int i=0; i<10; i++) {
std::cout<<"i="<<i<<"\n";
}

(Try this in NetRun now!)
  The "C" example also works as C++.

C++11

std::cout<<"Hello, world!\n";
std::vector<int> v;
for (int j=0;j<10;j++) v.push_back(j);
for (int i : v) {
std::cout<<"i="<<i<<"\n";
}

(Try this in NetRun now!)
  The "C++" or "C" examples also work.

CBMC

Bounded model checker converts your C (with very limited C++) into a SAT instance.
This then looks for values of uninitialized variables that break your assert statements.

int main() {
	// Uninitialized variables get filled with our "guess"
	unsigned int x;
	unsigned int y;
	
	// Avoid exploiting overflow bugs by adding assume statements 
	unsigned int product=385499;
	unsigned int limit=20000; // basically sqrt(product)
	__CPROVER_assume(x>1 && x<=limit && y>1 && y<product);
	
	// Use assert failures to extract the answer
	int z=x*y;
	assert(z!=product);
}

(Try this in NetRun now!)


Fortran 77

       function foo()
INTEGER foo

do i=1,10
CALL print_int(i);
end do
foo = i + 3;

end function

(Try this in NetRun now!)

NASM Assembly Syntax, x64

push r12  ; Save old value, so we can use it
mov r12,0 ; r12=0
startloop:
mov rdi, r12 ; rdi=r12
extern print_int
call print_int ; prints rdi
inc r12
cmp r12, 10 ; compares r12 against 10
jl startloop

pop r12 ; cleanup
ret

(Try this in NetRun now!)

GNU Assembler Syntax, x64

push %r12 # Save old value, so we can use it
mov $0,%r12 # r12=0
startloop:
mov %r12, %rdi # rdi=r12
call print_int # prints rdi
inc %r12
cmp $10,%r12 # compares r12 against $10 (!)
jl startloop

pop %r12 # cleanup
ret

(Try this in NetRun now!)

Scripting Languages

JavaScript

var start = (new Date).getTime();
var i=0;
while (i<10) {
print("i="+i);
i++;
}
var elapsed = (new Date).getTime() - start;
print("Elapsed="+elapsed+" milliseconds");

(Try this in NetRun now!)

Python

from time import time;
start=time();
for i in range(0,10):
print "i=",i;
elapsed=time()-start;
print "Elapsed=",elapsed," seconds";

(Try this in NetRun now!)

Python3

from time import time;
start=time();
for i in range(10):
print("i=",i);
elapsed=time()-start;
print("Elapsed=",elapsed," seconds");

(Try this in NetRun now!)

Ruby

start=Time.now
for i in (0..9)
print("i=",i,"\n")
end
elapsed=Time.now-start
print("Elapsed=",elapsed," seconds");

(Try this in NetRun now!)

Perl

use Time::HiRes qw( gettimeofday );
my $start=gettimeofday();
my $i=0;
while ($i < 10) {
print "i=$i\n";
$i=$i+1;
}
my $elapsed=gettimeofday()-$start;
print "Elapsed=$elapsed seconds\n";

(Try this in NetRun now!)

PHP

Free form text here!
<?php
$start=microtime(true);
for ($i=0;$i<10;$i++) {
print "i=$i\n";
}
$elapsed=microtime(true)-$start;
print "Elapsed=$elapsed seconds";
?>

(Try this in NetRun now!)

Postscript

0 1 9 {
	/i exch def     % name our loop counter as i
	i =             % print loop counter
} for

(Try this in NetRun now!)

Bash

time (
i=0
while [ $i -lt 10 ]
do
echo "i=$i"
i=`expr $i + 1`
done
)

(Try this in NetRun now!)

Graphics Card Programming

GLSL

vec2 c=vec2(3.0,2.0)*(texcoords-0.5)+vec2(0.0,0.0); /* constant c, varies onscreen*/
vec2 z=c;
/* Mandelbrot iteration: keep iterating until z gets big */
for (int i=0;i<15;i++) {
/* break if length of z is >= 4.0 */
if (z.r*z.r+z.g*z.g>=4.0) break;
/* z = z^2 + c; (where z and c are complex numbers) */
z=vec2(
z.r*z.r-z.g*z.g,
2.0*z.r*z.g
)+c;
}
gl_FragColor=fract(vec4(z.r,z.g,0.25*length(z),0));

(Try this in NetRun now!)

  This one is a little weird because I don't give you control over the vertex shader, or the geometry being rendered.  Instead, this is a GLSL pixel shader that runs on a unit box, with the bottom left corner as the origin.  Your location in the box is passed in as "texcoords" (the vertex shader makes a "varying vec2 texcoords;").  See the GLSL quick reference.

GLFP: OpenGL Fragment Program Assembly

TEMP sum;
MOV sum, 0.0;
TEX out, in,texture[1],2D;
ADD sum, sum,out;
ADD in, in,{0.01,0.00,0,0};
TEX out, in,texture[1],2D;
ADD sum, sum,out;
MUL out, sum,0.5;

(Try this in NetRun now!)

  As with GLSL, I pass in your texture coordinates, here as "in".  I also take the pixel color as "out".  See my GLFP cheat sheet.

CUDA

#include <cuda.h>
#include <iostream>
#include <fstream>
#include "lib/inc.c"

#define check(cudacall) { int err=cudacall; if (err!=cudaSuccess) std::cout<<"CUDA ERROR "<<err<<" at line "<<__LINE__<<"'s "<<#cudacall<<"\n";}

/* GPU Code! */
__global__ void fill_in_array(float *arr,int wid) {
int bs=16;
int tx=threadIdx.x%bs, ty=threadIdx.x/bs;
int blocksPerRow=wid/bs;
int bx=blockIdx.x%blocksPerRow, by=blockIdx.x/blocksPerRow;
int x=bx*bs+tx, y=by*bs+ty;
float cr=x*0.002, ci=y*0.002;
float zr=0.0, zi=0.0;
int count;
enum {max_count=256};
for (count=0;count<max_count;count++) {
// z= z*z+c
float nzr=zr*zr-zi*zi + cr;
float nzi=2.0*zr*zi + ci;
if ((nzr*nzr+nzi*nzi)>4.0f) break;
zr=nzr; zi=nzi;
}
arr[y*wid+x]=count;
}

/* Run on CPU */
int main(int argc,char *argv[]) {
int wid=512,ht=512;
float *arr=0; /* LIVES ON THE GPU!!!! */
double start=time_in_seconds();
check(cudaMalloc((void **)&arr, wid*ht*sizeof(float)));
std::cout<<"Allocated GPU memory at address "<<arr<<"\n";

fill_in_array<<<wid*ht/(16*16),16*16>>>(arr,wid);

std::cout<<"Ran GPU code!\n";
float harr[wid*ht];
check(cudaMemcpy(harr,arr,wid*ht*sizeof(float),cudaMemcpyDeviceToHost));
double elapsed=time_in_seconds()-start;
std::cout<<"Copied back array="<<harr[0]<<": rate "<<elapsed*1.0e9/(wid*ht)<<"ns/pixel, time "<<elapsed<<" seconds\n";
std::ofstream of("out.ppm",std::ios_base::binary);
of<<"P5\n"; // greyscale, binary
of<<wid<<" "<<ht<<"\n"; // image size
of<<"255\n"; // byte image
for (int i=0;i<wid*ht;i++) {
char c=(char)harr[i];
of<<c;
}
return 0;
}

(Try this in NetRun now!)

You have to write the C++ side main function yourself for CUDA or EPGPU--I don't provide one.

EPGPU

#include "epgpu.h"
#include "epgpu.cpp"
#include "lib/inc.c"
#include <iostream>
#include <fstream>

GPU_FILLKERNEL_2D(char,
mandelbrot,(),
float scale=2.0/1000;
float cr=i*scale-1.0; float ci=j*scale;
float zr=cr; float zi=ci;
int count;
enum {max_count=255};
for (count=0;count<max_count;count++) {
if ((zr*zr+zi*zi)>4.0) break;
float tzr=zr*zr-zi*zi+cr;
zi=2.0f*zr*zi+ci;
zr=tzr;
}
result=count;
)

int main() {
std::cout<<"Starting up\n"; std::cout.flush();
int w=1000,h=1000;
gpu_array2d<char> arr(w,h);
std::cout<<"Array created\n"; std::cout.flush();
arr=mandelbrot();
std::cout<<"Rendered\n"; std::cout.flush();
double start=time_in_seconds();
arr=mandelbrot();

char arrCPU[w*h];
arr.read(arrCPU);

std::ofstream of("out.ppm");
of<<"P5\n"<<w<<" "<<h<<" 255\n"; of.write(arrCPU,w*h);
double elapsed=time_in_seconds()-start;
std::cout<<"Total render time="<<elapsed<<"\n";
return 0;
}

(Try this in NetRun now!)

This is my own OpenCL wrapper language; see the official EPGPU page.

Other Languages

VHDL

use std.textio.all;

entity foo is
end foo;

architecture arch of foo is
begin
process
variable L : line;
begin
write(L, string'("a big old hello world!"));
writeline(output, L);
wait for 50 ns;
write(L, string'("and another one"));
writeline(output, L);
wait;
end process;
end arch;

(Try this in NetRun now!)

SPICE

Simple Capacitor-Pump Circuit for SPICE

Vcc 1 0 PULSE(0v 5v 2ms 1us 1us 1ms 2ms)
Ra 1 2 1K
Cb 2 0 0.3uF

.tran 0.2ms 10ms
.print tran v(2)

(Try this in NetRun now!)

This is a weird one because you get a gnuplot as output, not text.  This example is an RC filter applied to a square wave.

Prolog

cat(tom).
mouse(jerry).
carnivore(X) :- cat(X).
prey(X) :- mouse(X).
eats(X,Y) :- carnivore(X),prey(Y).

(Try this in NetRun now!)

  The query goes into the "Input" box, for example "eats(tom,X)." to figure out what tom can eat.


O. Lawlor, lawlor@alaska.edu
CS, UAF