Source code

 
 
  C Language manual 

// Prime number generation and Hello World in C // Roelh 20200311 - 20200817 // Sieve of Eratosthenes algorithm // see: https://en.wikipedia.org/wiki/Byte_Sieve // first come standard functions void putchar( int c) // A character written to address 0xF000 appears on screen. { *((char*) 0xf000) = c; // This line is for in simulator. int count, timer; c = c << 2; c = c | 0xF803; // two stopbits for (count=0; count<16; count++) { asm " movp 0x0020,A2" ; //set A2 to output register asm " mov D0,(A2+24)" ; //set MOSI low if (c & 0x8000) asm " mov D0,(A2+26)" ; //set MOSI high c = c << 1; for (timer=0; timer<750; timer++) {} // delay asm " movp 0x0020,A2" ; //set A2 to output register asm " mov D0,(A2+18)" ; //set CLK high for (timer=0; timer<750; timer++) {} // delay asm " movp 0x0020,A2" ; //set A2 to output register asm " mov D0,(A2+16)" ; //set CLK low } asm " movp 1,A2" ; //set page of A2 back to 1 }; int putdigit(int n, int div ) { int ch; ch='0'; while ( n >= div) { ch++; n -= div ; }; putchar(ch); return n; // return remaining number } void putnumber(int n) { n= putdigit(n,10000); n= putdigit(n,1000); n= putdigit(n,100); n= putdigit(n,10); n= putdigit(n,1); } void putstring(char *s) { while ( *s != 0) { putchar (*s); s++; } }; // Now comes the sieve algorithm // In the simulator it takes some time before the // first primes appear. This is because // the array must be initialized. You can follow this in the // simulator RAM (at the low right side of the screen), // that counts up to 120 (hex 0x78) before primes start. int flags[240]; char hello[] = "Hello World ! "; int main() { int i, prime, k, count, iter, size; size=120; // set size low because simulator is slow. // size 120 gives primes up to 241 putchar('*'); putstring(hello); // this is also a Hello World program for (iter = 1;iter <= 10;iter ++) { count=0 ; for (i = 0;i <= size;i++) flags [i] = 1; for (i = 0;i <= size;i ++) { if (flags[i]) { prime = i + i + 3; putnumber(prime); putchar(' '); k = i + prime; while (k <= size) { flags[k] = 0; k += prime; } count = count + 1; } } } }
  												 
                				
  		
                				
Compiler/Assembler/Simulator updated 20200818. Have a look at the Kobold K2 project page

This simulator/assembler is derived from the work of Marco Schweighauser, see his Blog

Assembly listing     

  												 
  						 

Assembly language manual     

Simulation

Press ASSEMBLE and then RUN

PC
0000
WP
0000
A2
0000
A3
0000
D0
0000
D1
0000
D2
0000
D3
0000
PCN
0000
CY
0
IR
0000
EA
0000
(EA)
0000
VAL
0000
ACT
0000
REG
0000
0
RAM contents (page 1)
0100 0000 0000 0000 0000
0108 0000 0000 0000 0000
0110 0000 0000 0000 0000
0118 0000 0000 0000 0000
0120 0000 0000 0000 0000
0128 0000 0000 0000 0000
0130 0000 0000 0000 0000
0138 0000 0000 0000 0000
0140 0000 0000 0000 0000
0148 0000 0000 0000 0000
0150 0000 0000 0000 0000
0158 0000 0000 0000 0000
Experimental C Compiler version 20200818
2021/09/21 19:58 CET. End of compilation.
No parser errors.
0

Intermediate JSON code:
Link: Intermediate format
{ "types": { "int": [ "basic",2] , "void": [ "basic",0] , "char": [ "basic",1] , "float": [ "basic",4] , "bool": [ "basic",1] , "array-of-chars": [ "array",0,"char"] , "-none-": [ "basic",2] , "_tt": [ "pt",2,"char"] , "_tt1": [ "pt",2,"char"] , "_tt2": [ "array",480,"int"] , "_tt3": [ "array",0,"char"] } , "globals": { "flags": "_tt2","hello": "_tt3"} , "functions": [{"function": "putchar","par": { "c": "int"} , "return": "void","mod": [ ],"body": [ { "var": { "count": "int","timer": "int"} } , [ "",[ "char","*",61440] ,"=","c"] , [ "","c","=",[ "int","c","<<",2] ] , [ "","c","=",[ "int","c","|",63491] ] , { "for": [ [ "","count","=",0] ,[ "bool","count","<",16] ,[ "int", "count","=",[ "int","count","+",1] ] ] , "do_": [ { "asm": " movp 0x0020,A2"} , { "asm": " mov D0,(A2+24)"} , { "if": [ "bool","c","&",32768] , "then": [ { "asm": " mov D0,(A2+26)"} ] } , [ "","c","=",[ "int","c","<<",1] ] , { "for": [ [ "","timer","=",0] ,[ "bool","timer","<",750] ,[ "int", "timer","=",[ "int","timer","+",1] ] ] , "do_": [ ] } , { "asm": " movp 0x0020,A2"} , { "asm": " mov D0,(A2+18)"} , { "for": [ [ "","timer","=",0] ,[ "bool","timer","<",750] ,[ "int", "timer","=",[ "int","timer","+",1] ] ] , "do_": [ ] } , { "asm": " movp 0x0020,A2"} , { "asm": " mov D0,(A2+16)"} ] } , { "asm": " movp 1,A2"} ] } , {"function": "putdigit","par": { "n": "int","div": "int"} , "return": "int","mod": [ ],"body": [ { "var": { "ch": "int"} } , [ "","ch","=",48] , { "while": [ "bool","n",">=","div"] , "do_": [ [ "int","ch","=",[ "int","ch","+",1] ] , [ "int","n","=",[ "int","n","-","div"] ] ] } , [ "void","putchar",[ "ch"] ] , { "return": "n"} ] } , {"function": "putnumber","par": { "n": "int"} , "return": "void","mod": [ ],"body": [ { "var": { } } , [ "","n","=",[ "int","putdigit",[ "n",10000] ] ] , [ "","n","=",[ "int","putdigit",[ "n",1000] ] ] , [ "","n","=",[ "int","putdigit",[ "n",100] ] ] , [ "","n","=",[ "int","putdigit",[ "n",10] ] ] , [ "","n","=",[ "int","putdigit",[ "n",1] ] ] ] } , {"function": "putstring","par": { "s": "_tt1"} , "return": "void","mod": [ ],"body": [ { "var": { } } , { "while": [ "bool",[ "char","*","s"] ,"!=",0] , "do_": [ [ "void","putchar",[ [ "char","*","s"] ] ] , [ "_tt1","s","=",[ "_tt1","s","+",1] ] ] } ] } , {"function": "main","par": { } , "return": "int","mod": [ ],"body": [ { "var": { "i": "int","prime": "int","k": "int","count": "int", "iter": "int","size": "int"} } , [ "","size","=",120] , [ "void","putchar",[ 42] ] , [ "void","putstring",[ "hello"] ] , { "for": [ [ "","iter","=",1] ,[ "bool","iter","<=",10] ,[ "int", "iter","=",[ "int","iter","+",1] ] ] , "do_": [ [ "","count","=",0] , { "for": [ [ "","i","=",0] ,[ "bool","i","<=","size"] ,[ "int", "i","=",[ "int","i","+",1] ] ] , "do_": [ [ "",[ "int","flags",":","i"] ,"=",1] ] } , { "for": [ [ "","i","=",0] ,[ "bool","i","<=","size"] ,[ "int", "i","=",[ "int","i","+",1] ] ] , "do_": [ { "if": [ "int","flags",":","i"] , "then": [ [ "","prime","=",[ "int",[ "int","i","+", "i"] ,"+",3] ] , [ "void","putnumber",[ "prime"] ] , [ "void","putchar",[ 32] ] , [ "","k","=",[ "int","i","+","prime"] ] , { "while": [ "bool","k","<=","size"] , "do_": [ [ "",[ "int","flags",":","k"] ,"=",0] , [ "int","k","=",[ "int","k","+","prime"] ] ] } , [ "","count","=",[ "int","count","+",1] ] ] } ] } ] } ] } ] , "initlist": { "hello": "Hello World ! "} }

End of JSON code.

Types, globals and functions in the old internal format:
Link: (old)AST manual
list of types in the old way: