#include "stdio.h"

// Global variable
	char text[2048];	// 2048 should be enough

	

int make_boring_routine(int symbols_per_line, int CNT)
{
	symbols_per_line++;
	if ( symbols_per_line !=24 && text[CNT+1]!=0 )
	{
	  printf(",");
	}
	return symbols_per_line;
}
	
void print_data( int line_begin, int x_position, int color, char text[2048])
{
	printf("\nTady to mas\n----------------------------------\n");
	printf("\n\n	LDI data,%d\n	CALL select_line_both\n", line_begin );
	printf("	STS write_text_from_DB_line_SRAM,data\n");	// save info about line number
	// to "write_text_from_DB_line_SRAM"
	printf("\n	LDI data,%d\n", x_position);	// save x position

	if ( color == 0 )
	{
	  printf("\n	CLT\n");	// font color - black
	}
	else
	{
	  printf("\n	SET\n");	// font color - white
	}
	
	printf("\n.DB ");	// begin of database
	
	int CNT = 0;
	int symbols_per_line = 0;	// max symbols on one line in assebler
	while (CNT <=2048)
	{
	    if ( text[CNT]>=65 && text[CNT]<=90 )
	    {	// ASCII A-Z
	      printf("B%c", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	      
	    }
	    if ( text[CNT]>=97 && text[CNT]<=122)
	    {	// ASCII a-z
	      printf("n%c", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]>=48 && text[CNT]<=57 )
	    {	// ASCII 0-9
	      printf("n%c", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]==95 )
	    {	// space
	      printf("sp", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]==33 )
	    {	// !
	      printf("em", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]==46 )
	    {	// .
	      printf("po", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]==63 )
	    {	// ?
	      printf("qu", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]==181 )
	    {	// u (micro)
	      printf("mi", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]==34 )
	    {	// "
	      printf("dq", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]==58 )
	    {	// :
	      printf("co", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]==44 )
	    {	// ,
	      printf("li", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]==124 )
	    {	// |
	      printf("pipe", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]==40 )
	    {	// (
	      printf("op", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]==41 )
	    {	// )
	      printf("cp", text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }
	    if ( text[CNT]==45 )
	    {
	      printf("mns",text[CNT]);
	      symbols_per_line = make_boring_routine(symbols_per_line, CNT);
	    }



	    if ( symbols_per_line == 24 )
	    {
	      printf("\n.DB ");	// add a new line
	      symbols_per_line = 0;
	    }


	    CNT++;
	}
	if ( symbols_per_line%2 == 0 )
	{	// If # symbols will be odd -> add zero byte manually
	  printf(",etx,0\n");
	}	// etx - End of text 
	else
	{
	  printf(",etx\n");
	}
	
}

void clear_text_matrix(void)
{
	int CNT;
	while (CNT<=2048)
	{
	  text[CNT] = 0;
	  CNT++;
	}
}

void main(void)
{
	int line_begin;	// number of first line
	int x_position;
	int color;

	
	clear_text_matrix();
  
	printf("\nMej na pameti, ze misto mezery musis psat _ !!!\n");
	printf("\nZadej cislo radku (0~7)\n");
  
	scanf("%d", &line_begin);	// Save user input to "line_begin"
					// number
	
	printf("\nZadej Xovou souradnici (0~127)\n");
	scanf("%d", &x_position);	// data
	
	printf("\nBarva pisma - 0 cerne, 1 bile\n");
	scanf("%d", &color);	// T-bit
	
	printf("\nZadej samotny text\n");
	scanf("%s", &text);
	
	
	print_data( line_begin, x_position, color, text );
}
