Writing to console

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
HLA91
Posts: 9
Joined: Mon Feb 18, 2008 12:23 pm
Location: UK

Writing to console

Post by HLA91 »

HI guys
I am totally new to OS dev and I found an article on the web about making a simple OShttp://www.rohitab.com/discuss/lofivers ... 24959.html
I downloaded the attachment, compiled the OS and ran the ISO on my virtual pc and it went fine. I modified the kernel.c (code below) so that it printed an extra line saying goodbye, but it doesn't show up.
The code for kernel.c is below and kernel.h and screen.c are attached.

Code: Select all

#include "kernel.h"

int strlen(string str);
void dec2bin(dword decimal, int prefix);
void drawdesktop(byte ch, char fore, char back);
void drawwindow(string title, int xpos, int ypos, int width, int height, char fore, char back);


void kernel(void) // kernel entry point
{
	drawdesktop(177, COLOR_WHITE, COLOR_BLUE);
	drawwindow(" Simple OS Example - by Napalm ", 4, 4, 72, 16, COLOR_LTWHITE, COLOR_BLUE);
	putsnocr("Welcome to this example! ");
        /* I put another putsnocr line here but it didnt show up only "Welcome to this example" showed  */
	hidecursor();
	
	dword value = 0xDEADBEEF;
	while(1){
		// display value in decimal
		gotoxy(6, 9);
		dword decimal = value;
		char base[10];
		int i = 0;
		while(decimal > 0){
			base[i++] = '0' + (decimal % 10);
			decimal /= 10;
		}
		while(i-- > 0) putch(base[i]);
		
		// display value in hex	
		gotoxy(6, 10);
		putsnocr("0x");
		for(int i = 28; i > -1; i -= 4){
			byte nibble = ((value >> i) & 0x0F);
			if(nibble < 10) putch('0' + nibble);
			else putch('A' + (nibble - 10));
		}
		
		// display value in binary
		gotoxy(6, 11);
		dec2bin(value, 32);
		value++;
	}
}

int strlen(string str) // simple string length
{
	string end = str;
	while(*end++);
	return (end - str);
}

void drawdesktop(byte ch, char fore, char back)
{
	// clear screen with foreground and background
	// and also with desired character
	settextcolor(fore, back);
	cls(ch);
}

// decimal to binary
void dec2bin(dword decimal, int prefix)
{
	dword j = 0;
	for(int i = prefix; i >= 0; --i, j = ((decimal & (1 << i)) >> i))
		if(prefix || j) prefix = putch('0' + j);
}

void drawwindow(string title, int xpos, int ypos, int width, int height, char fore, char back)
{
	int xextent = (xpos + width - 1), yextent = (ypos + height - 1);
	// draw window
	for(int y = ypos; y <= yextent; y++){
		settextcolor(fore, back);
		gotoxy(xpos, y);
		if(y == ypos){ // draw top caption bar
			settextcolor(COLOR_LTYELLOW, COLOR_RED);
			for(int x = xpos; x <= xextent; x++){
				if(x == xpos) putch(213);
				else if(x == xextent) putch(254);
				else putch(205);
			}
			gotoxy(xpos + ((width - strlen(title)) / 2), y);
			putsnocr(title);
		}else if(y < yextent){ // draw middle lines
			for(int x = xpos; x <= xextent; x++){
				if(x == xpos || x == xextent) putch(179);
				else putch(' ');
			}
		}else{ // draw bottom edge
			for(int x = xpos; x <= xextent; x++){
				if(x == xpos) putch(192);
				else if(x == xextent) putch(217);
				else putch(196);
			}
		}
	}
	// draw vertical shadow
	for(int y = ypos; y <= yextent; y++){
		gotoxy(xextent + 1, y + 1);
		settextcolor(COLOR_WHITE, COLOR_BLACK);
		putch(177);
	}
	// draw horizontal shadow
	for(int x = xpos; x <= xextent; x++){
		gotoxy(x + 1, yextent + 1);
		settextcolor(COLOR_WHITE, COLOR_BLACK);
		putch(177);
	}
	// position cursor inside window
	gotoxy(xpos + 2, ypos + 2);
	settextcolor(fore, back);
}
Can someone please tell me what is going wrong?

Thanks for your time

HLA91
Attachments
SCREEN.C
(1.91 KiB) Downloaded 31 times
KERNEL.H
(1.07 KiB) Downloaded 27 times
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

Post by xyjamepa »

May be it's a stupid question ,but did you recompile your kernel?
and did you use the new kernel?
I mean after recompilation...
The man who follows the crowd will usually get no further than the crowd.
The man who walks alone is likely to find himself in places
no one has ever been before.
HLA91
Posts: 9
Joined: Mon Feb 18, 2008 12:23 pm
Location: UK

Post by HLA91 »

Yes I did and even I thought about that so I deleted all the files replaced them with the originals modified the kernel.c and then re-compiled but only the welcome on the first line showed.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Writing to console

Post by jal »

HLA91 wrote:Can someone please tell me what is going wrong?
I haven't got a clue. However, have you played with various putsnocrs? Changed the text of the first one, instead of adding another one? Removing the first one, see if the 'second' one is displayed? Etc.? It seems you've only added the line, saw it didn't work, and posted here.


JAL
HLA91
Posts: 9
Joined: Mon Feb 18, 2008 12:23 pm
Location: UK

Post by HLA91 »

I have changed the text of the first one and it worked fine but when I tried to add another line it didnt work.
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

Post by xyjamepa »

I suggest you take a look at this
and then try to write your own code.
The man who follows the crowd will usually get no further than the crowd.
The man who walks alone is likely to find himself in places
no one has ever been before.
HLA91
Posts: 9
Joined: Mon Feb 18, 2008 12:23 pm
Location: UK

Post by HLA91 »

ahh ok thanks ill read through it and hopefully make my own, thanks for the link I should have searched a bit better.
User avatar
codemastersnake
Member
Member
Posts: 148
Joined: Sun Nov 07, 2004 12:00 am
Contact:

Post by codemastersnake »

HLA91 wrote:ahh ok thanks ill read through it and hopefully make my own, thanks for the link I should have searched a bit better.
First of all the source code is lil' complex as compared there are lot more simpler kernels available on the Internet.

Plus it's good if you write your own kernel from the scratch. It's always helpful and you'll will learn a lot. More over by writing your own kernel you'll be able to find out bugs in your code more easily and won't get in to mess like you have been right now.

Best of luck with your OS.
Post Reply