Thread: Simple OS writing to screen help

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Simple OS writing to screen help

    HI guys

    I found this article on how to create your own simple OS that just prints to screen, I know its pretty lame but I am trying to get it to write one more line to the screen but it just wont do it. Here is the kernel.c source, there are 2 more files, kernel.h and screen.c
    I have (hopefully) attatched the other 2 files. I placed a comment about 15 lines down which explains what I did.
    Code:
    #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 anyone please explain whats wrong?

    Cheers

    HLA91

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I wouldn't call that your own operating system . . . it's just an ncurses program as far as I can tell. An operating system is a lot more complicated.

    Anyway . . . check out the name of your function.
    Code:
    putsnocr("Welcome to this example! ");
    It's not printing a newline (CR). That would mean that at the very best, your message would appear right after this one -- and I suspect that the code might be overwriting that part of the screen. You'll probably want to move the cursor before printing a new string. I haven't really used ncurses, but I think move() would do the trick, or a function like mvputsnocr(). Something like that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple doubt which striked me while reading OS
    By chottachatri in forum C Programming
    Replies: 3
    Last Post: 10-30-2008, 07:14 PM
  2. SDL + Keys
    By Livijn in forum Game Programming
    Replies: 24
    Last Post: 05-11-2007, 04:31 PM
  3. Laptop LCD Screen
    By mkylman in forum Tech Board
    Replies: 8
    Last Post: 11-11-2006, 08:27 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. FYI - a simple way to clear the screen
    By johnc in forum C Programming
    Replies: 13
    Last Post: 07-17-2002, 04:53 PM