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 puthexdigit(int n, int level) { char c; c = ( n & 0x000F ) + '0'; if (level !=1) puthexdigit( n >> 4, level-1); if (c > '9') c = c + 7; putchar(c); } 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++; } }; void printf (char* str, ... ) { int * arp; char c; int arg; arp = (int*)&str; while (1) { c = *str++; if (c != '%') { if (c==0) break; putchar(c); continue; } else { arp++; arg = *arp; switch(*str++) { case 0 : str--; break; case 'c': putchar(arg); // char break; case 'i': putnumber(arg); // integer break; case 's': putstring((char*)arg); break; case 'x': puthexdigit(arg,4); // hex break; default: arp--; putchar(c); break; } } } } // 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 ! "; void primes() { int i, prime, k, count, iter, size; size=120; // set size low because simulator is slow. // size 120 gives primes up to 241 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; } } } } int main() { putstring(hello); // this is also a Hello World program putchar('\n'); //primes(); printf("Test printf %s, char %c, hex %x ",hello,'+',0x12F5); }
  												 
                				
  		
                				
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
2024/04/25 14:50 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": [ "pt",2,"char"] , "_tt3": [ "pt",2,"int"] , "_tt": [ "pt",2,"int"] , "_tt": [ "pt",2,"char"] , "_tt4": [ "array",480,"int"] , "_tt5": [ "array",0,"char"] } , "globals": { "flags": "_tt4","hello": "_tt5", "_G1": "array-of-chars"} , "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": "puthexdigit","par": { "n": "int","level": "int"} , "return": "void","mod": [ ],"body": [ { "var": { "c": "char"} } , [ "","c","=",[ "char",[ "char","n","&",15] ,"+",48] ] , { "if": [ "bool","level","!=",1] , "then": [ [ "void","puthexdigit",[ [ "int","n",">>",4] ,[ "int", "level","-",1] ] ] ] } , { "if": [ "bool","c",">",57] , "then": [ [ "","c","=",[ "char","c","+",7] ] ] } , [ "void","putchar",[ "c"] ] ] } , {"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": "printf","par": { "str": "_tt2","...": "int"} , "return": "void","mod": [ ],"body": [ { "var": { "arp": "_tt3","c": "char","arg": "int"} } , [ "","arp","=",[ "_tt","&","str"] ] , { "while": 1, "do_": [ [ "","c","=",[ "char","*",[ "_tt2","str","++"] ] ] , { "if": [ "bool","c","!=",37] , "then": [ { "if": [ "bool","c","==",0] , "then": [ { "break": "0"} ] } , [ "void","putchar",[ "c"] ] , { "continue": "0"} ] , "else": [ [ "_tt3","arp","=",[ "_tt3","arp","+", 1] ] , [ "","arg","=",[ "int","*","arp"] ] , { "switch": [ "char","*",[ "_tt2","str","++"] ] , "block": [ { "case": 0} , [ "_tt2","str","=",[ "_tt2","str","-",1] ] , { "break": "0"} , { "case": 99} , [ "void","putchar",[ "arg"] ] , { "break": "0"} , { "case": 105} , [ "void","putnumber",[ "arg"] ] , { "break": "0"} , { "case": 115} , [ "void","putstring",[ "arg"] ] , { "break": "0"} , { "case": 120} , [ "void","puthexdigit",[ "arg",4] ] , { "break": "0"} , { "default": "0"} , [ "_tt3","arp","=",[ "_tt3","arp","-",1] ] , [ "void","putchar",[ "c"] ] , { "break": "0"} ] } ] } ] } ] } , {"function": "primes","par": { } , "return": "void","mod": [ ],"body": [ { "var": { "i": "int","prime": "int","k": "int","count": "int", "iter": "int","size": "int"} } , [ "","size","=",120] , { "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] ] ] } ] } ] } ] } , {"function": "main","par": { } , "return": "int","mod": [ ],"body": [ { "var": { } } , [ "void","putstring",[ "hello"] ] , [ "void","putchar",[ 10] ] , [ "void","printf",[ "_G1","hello",43,4853] ] ] } ] , "initlist": { "hello": "Hello World ! ", "_G1": "Test printf %s, char %c, hex %x "} }

End of JSON code.

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