Page 1 of 1

the tab character (\t) is not printed correctly

Posted: Sun Jul 19, 2020 6:04 pm
by ITchimp
I am writing the ram disk for my simple OS... when I print the tab character \t, it looks like a dot, (pls see the attachment jpg file)... what is wrong, I thought the rendering is supposed to be a vertical jump (blank) on the screen... what is wrong here?

Re: the tab character (\t) is not printed correctly

Posted: Sun Jul 19, 2020 6:39 pm
by Octocontrabass
How do you print a newline character?

Why is printing a newline character different from printing letters and numbers and such?

Re: the tab character (\t) is not printed correctly

Posted: Sun Jul 19, 2020 8:58 pm
by ITchimp
the code for print a single char is below. it just shift the cursor to the next line....

Code: Select all

void print_char(char character, int col, int row, char attribute_byte){
	char* video_memory = (char *) VIDEO_ADDRESS;
	int offset;
	if (attribute_byte <0)
		attribute_byte = WHITE_ON_BLUE;
	if (col<0 || row<0){
		offset = get_cursor();
	}else{
		offset = get_screen_offset(col, row);
	}
	if (character =='\n'){
		int rows = (offset>>1) /(MAX_COLS) ;
		offset = get_screen_offset (79, rows);
	}else{
		video_memory[offset] = character;
		video_memory[offset+1] = attribute_byte;
	}
	offset +=2;
	offset = handle_scrolling(offset);
	set_cursor(offset);

}

Re: the tab character (\t) is not printed correctly

Posted: Sun Jul 19, 2020 9:16 pm
by Octocontrabass
So... why do you need to shift the cursor to the next line? Other characters display fine when you write them directly to video memory. What makes '\n' so special?

Re: the tab character (\t) is not printed correctly

Posted: Mon Jul 20, 2020 9:54 am
by Schol-R-LEA
To put it another way, what do tab and newline have in common? What class or category of characters do they both belong to?

As a bonus question, what other ASCII characters - or even categories of characters - have to be handled specially? Are there any which shouldn't be printed at all? Why are these characters part of the ASCII set in the first place, and do they still have a role today?

Also, why do different OSes which use ASCII nonetheless have different newline characters (or pairs of characters, for Windows)? Why was the original standard for newlines a Carriage Return followed by Line Feed, and why did Unix drop one of them?

This may sound like an overly pedantic history lesson, but it is directly relevant to why tab and newline differ from other ASCII characters in this regard, and understanding that history may make a bunch of things about why you do certain things certain ways clearer.

Finally, just for fun, what about if you decide to support a different character encoding scheme, such as Latin-1 (which is mostly ASCII but adds a list of extended characters), UTF-8 (which shares a lot of character code points with ASCII, but is decidedly different in many ways), UTF-16 (which is very definitely not the same as either ASCII or UTF-8) or even EBCDIC (which you'll probably never need anyway, but just for argument's sake, as it bears almost no resemblance to ASCII)?

Re: the tab character (\t) is not printed correctly

Posted: Mon Jul 20, 2020 12:28 pm
by thewrongchristian
ITchimp wrote:I am writing the ram disk for my simple OS... when I print the tab character \t, it looks like a dot, (pls see the attachment jpg file)... what is wrong, I thought the rendering is supposed to be a vertical jump (blank) on the screen... what is wrong here?
Tab is a horizontal jump, not vertical.

If you have tabstop set to 8, say, then printing a tab should jump the cursor to the next modulo-8 column (assuming 0 based column counting.)

So, if you're in the first column (column 0) and print a tab, you want to jump to column 8. Similarly, if you're in any of columns 1..7, a tab will jump you to column 8.

If you want the tab to erase the intervening characters, you need to print a space in each of the columns you're jumping over until you reach your desired column.

As it is at the moment, you're just putting the character 9 in the character buffer, and the display is just rendering whatever the font defines for glyph 9 to the screen. The character buffer is dumb, and won't interpret control characters, which is what tab is.