os development

hello,
i started os development and i also get a tutorial on http://www.brokenthorn.com/Resources/OSDevIndex.html
but the code is very difficult can you explain it
project Kernel.sln
the code is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
====================================================
	entry.cpp
		-This is the kernel entry point. This is called
		from the boot loader
====================================================
*/

extern void _cdecl main ();
extern void _cdecl InitializeConstructors();
extern void _cdecl Exit ();

void _cdecl kernel_entry () {

#ifdef ARCH_X86
	_asm {
		cli						// clear interrupts--Do not enable them yet
		mov ax, 10h				// offset 0x10 in gdt for data selector, remember?
		mov ds, ax
		mov es, ax
		mov fs, ax
		mov gs, ax
		mov ss, ax				// Set up base stack
		mov esp, 0x90000
		mov ebp, esp			// store current stack pointer
		push ebp
	}
#endif

	//! Execute global constructors
	InitializeConstructors();

	//!	Call kernel entry point
	main ();

	//! Cleanup all dynamic dtors
	Exit ();

#ifdef ARCH_X86
	_asm cli
#endif
	for (;;);
}

and the DebugDisplay.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//============================================================================
//    INTERFACE DATA DECLARATIONS
//============================================================================
//============================================================================
//    INTERFACE FUNCTION PROTOTYPES
//============================================================================

extern void DebugClrScr (const unsigned short c);
extern void DebugPuts (char* str);
extern int DebugPrintf (const char* str, ...);
extern unsigned DebugSetColor (const unsigned c);
extern void DebugGotoXY (unsigned x, unsigned y);

//============================================================================
//    INTERFACE OBJECT CLASS DEFINITIONS
//============================================================================
//============================================================================
//    INTERFACE TRAILING HEADERS
//============================================================================
//****************************************************************************
//**
//**    END DebugDisplay.h
//**
//****************************************************************************
#endif 

and DebugDisplay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//****************************************************************************
//**
//**    [FILE NAME]
//**    - [FILE DESCRIPTION]
//**
//****************************************************************************
//============================================================================
//    IMPLEMENTATION HEADERS
//============================================================================

#include <stdarg.h>
#include <string.h>
#include "DebugDisplay.h"

//============================================================================
//    IMPLEMENTATION PRIVATE DEFINITIONS / ENUMERATIONS / SIMPLE TYPEDEFS
//============================================================================
//============================================================================
//    IMPLEMENTATION PRIVATE CLASS PROTOTYPES / EXTERNAL CLASS REFERENCES
//============================================================================
//============================================================================
//    IMPLEMENTATION PRIVATE STRUCTURES / UTILITY CLASSES
//============================================================================
//============================================================================
//    IMPLEMENTATION REQUIRED EXTERNAL REFERENCES (AVOID)
//============================================================================
//============================================================================
//    IMPLEMENTATION PRIVATE DATA
//============================================================================

// Note: Some systems may require 0xB0000 Instead of 0xB8000
// We dont care for portability here, so pick either one
#define VID_MEMORY	0xB8000

// these vectors together act as a corner of a bounding rect
// This allows GotoXY() to reposition all the text that follows it
static unsigned int _xPos=0, _yPos=0;
static unsigned _startX=0, _startY=0;

// current color
static unsigned _color=0;

//============================================================================
//    INTERFACE DATA
//============================================================================
//============================================================================
//    IMPLEMENTATION PRIVATE FUNCTION PROTOTYPES
//============================================================================
//============================================================================
//    IMPLEMENTATION PRIVATE FUNCTIONS
//============================================================================

#ifdef _MSC_VER
#pragma warning (disable:4244)
#endif

void DebugPutc (unsigned char c) {

	if (c==0)
		return;

	if (c == '\n'||c=='\r') {	/* start new line */
		_yPos+=2;
		_xPos=_startX;
		return;
	}

	if (_xPos > 79) {			/* start new line */
		_yPos+=2;
		_xPos=_startX;
		return;
	}

	/* draw the character */
	unsigned char* p = (unsigned char*)VID_MEMORY + (_xPos++)*2 + _yPos * 80;
	*p++ = c;
	*p =_color;
}

char tbuf[32];
char bchars[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

void itoa(unsigned i,unsigned base,char* buf) {
   int pos = 0;
   int opos = 0;
   int top = 0;

   if (i == 0 || base > 16) {
      buf[0] = '0';
      buf[1] = '\0';
      return;
   }

   while (i != 0) {
      tbuf[pos] = bchars[i % base];
      pos++;
      i /= base;
   }
   top=pos--;
   for (opos=0; opos<top; pos--,opos++) {
      buf[opos] = tbuf[pos];
   }
   buf[opos] = 0;
}

void itoa_s(int i,unsigned base,char* buf) {
   if (base > 16) return;
   if (i < 0) {
      *buf++ = '-';
      i *= -1;
   }
   itoa(i,base,buf);
}

//============================================================================
//    INTERFACE FUNCTIONS
//============================================================================

unsigned DebugSetColor (const unsigned c) {

	unsigned t=_color;
	_color=c;
	return t;
}

void DebugGotoXY (unsigned x, unsigned y) {

	// reposition starting vectors for next text to follow
	// multiply by 2 do to the video modes 2byte per character layout
	_xPos = x*2;
	_yPos = y*2;
	_startX=_xPos;
	_startY=_yPos;
}

void DebugClrScr (const unsigned short c) {

	unsigned char* p = (unsigned char*)VID_MEMORY;

	for (int i=0; i<160*30; i+=2) {

		p[i] = ' ';  /* Need to watch out for MSVC++ optomization memset() call */
		p[i+1] = c;
	}

	// go to start of previous set vector
	_xPos=_startX;_yPos=_startY;
}

void DebugPuts (char* str) {

	if (!str)
		return;

	for (size_t i=0; i<strlen (str); i++)
		DebugPutc (str[i]);
}

int DebugPrintf (const char* str, ...) {

	if(!str)
		return 0;

	va_list		args;
	va_start (args, str);

	for (size_t i=0; i<strlen(str);i++) {

		switch (str[i]) {

			case '%':

				switch (str[i+1]) {

					/*** characters ***/
					case 'c': {
						char c = va_arg (args, char);
						DebugPutc (c);
						i++;		// go to next character
						break;
					}

					/*** address of ***/
					case 's': {
						int c = (int&) va_arg (args, char);
						char str[32]={0};
						itoa_s (c, 16, str);
						DebugPuts (str);
						i++;		// go to next character
						break;
					}

					/*** integers ***/
					case 'd':
					case 'i': {
						int c = va_arg (args, int);
						char str[32]={0};
						itoa_s (c, 10, str);
						DebugPuts (str);
						i++;		// go to next character
						break;
					}

					/*** display in hex ***/
					case 'X':
					case 'x': {
						int c = va_arg (args, int);
						char str[32]={0};
						itoa_s (c,16,str);
						DebugPuts (str);
						i++;		// go to next character
						break;
					}

					default:
						va_end (args);
						return 1;
				}

				break;

			default:
				DebugPutc (str[i]);
				break;
		}

	}

	va_end (args);
}

//============================================================================
//    INTERFACE CLASS BODIES
//============================================================================
//****************************************************************************
//**
//**    END[DebugDisplay.cpp]
//**
//**************************************************************************** 

and main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
======================================
	main.cpp
		-kernel startup code
======================================
*/

#include "DebugDisplay.h"

void _cdecl main () {

	int i=0x12;
	DebugClrScr (0xfd);

	DebugGotoXY (2,4);
	DebugSetColor (0x38);
	DebugPrintf ("+--------------------------------------------------------------------+\n");
	DebugPrintf ("|-------------Welcome to Usman Nazir Operating System----------------|\n");
	DebugPrintf ("+--------------------------------------------------------------------+\n\n");
	DebugSetColor (0x12);

	DebugSetColor (0x12);
	DebugPrintf ("\ni as integer ........................");
	DebugPrintf ("\ni in hex ............................");

	DebugGotoXY (25,8);
	DebugSetColor (0x1F);
	DebugPrintf ("\n[%i]",i);
	DebugPrintf ("\n[0x%x]",i);

	DebugGotoXY (2,12);
	DebugSetColor (0x1F);
	DebugPrintf ("\n\nI am preparing to load... Hold on, please... :)");

	DebugGotoXY (2,16);
	DebugSetColor (0x2f);
	DebugPrintf ("Hi my name is Usman Nazir this is my first kernel\n");
	DebugPrintf ("developed in Microsoft Visual C++");

	DebugGotoXY (0,20);
	DebugSetColor (0x38);
	DebugPrintf (".......");
	DebugSetColor (0x1f);
	

	DebugGotoXY (4,19);
	DebugSetColor (0x1f);
	DebugPrintf ("..........................................................................");
	
}

project Lib.sln
cstd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//! MSVC++ C++ runtime

// Very MSVC++ dependent. Will try to support different compiliers later.
#ifndef _MSC_VER
#error "MOS2 Kernel C++ Runtime requires Microsoft Visual C++ 2005 or later."
#endif

//! Function pointer typedef for less typing
typedef void (__cdecl *_PVFV)(void);

//! __xc_a points to beginning of initializer table
#pragma data_seg(".CRT$XCA")
_PVFV __xc_a[] = { 0 };

//! __xc_z points to end of initializer table
#pragma data_seg(".CRT$XCZ")
_PVFV __xc_z[] = { 0 };

//! Select the default data segment again (.data) for the rest of the unit
#pragma data_seg()

//! Now, move the CRT data into .data section so we can read/write to it
#pragma comment(linker, "/merge:.CRT=.data")

/*
===========================
	Globals
===========================
*/

//! function pointer table to global deinitializer table
static _PVFV * pf_atexitlist = 0;

//! Maximum entries allowed in table. Change as needed
static unsigned max_atexitlist_entries = 32;

//! Current amount of entries in table
static unsigned cur_atexitlist_entries = 0;

/*
===========================
	CRT Routines
===========================
*/

//! initialize all global initializers (ctors, statics, globals, etc..)
void __cdecl _initterm ( _PVFV * pfbegin, _PVFV * pfend ) {

	//! Go through each initializer
    while ( pfbegin < pfend )
    {
	  //! Execute the global initializer
      if ( *pfbegin != 0 )
            (**pfbegin) ();

	    //! Go to next initializer inside the initializer table
        ++pfbegin;
    }
}

//! initialize the de-initializer function table
void __cdecl _atexit_init(void) {

    max_atexitlist_entries = 32;

	// Warning: Normally, the STDC will dynamically allocate this. Because we have no memory manager, just choose
	// a base address that you will never use for now
	pf_atexitlist = (_PVFV *)0x5000;
}

//! adds a new function entry that is to be called at shutdown
int __cdecl atexit(_PVFV fn) {

	//! Insure we have enough free space
	if (cur_atexitlist_entries>=max_atexitlist_entries)
		return 1;
	else {

		//! Add the exit routine
		*(pf_atexitlist++) = fn;
		cur_atexitlist_entries++;
	}
	return 0;
}

//! shutdown the C++ runtime; calls all global de-initializers
void _cdecl Exit () {

	//! Go through the list, and execute all global exit routines
	while (cur_atexitlist_entries--) {

			//! execute function
			(*(--pf_atexitlist)) ();
	}
}

//! execute all constructors and other dynamic initializers
void _cdecl InitializeConstructors() {

   _atexit_init();
   _initterm(__xc_a, __xc_z); 
}

// Disable C4100 ('Unused Parameter') warning for now, 'til we could get these written
#pragma warning (disable:4100)

//! purecall function handler
int __cdecl ::_purecall() { for (;;); return 0; }

//! global new and delete operators
void* __cdecl ::operator new (unsigned int size) { return 0; }
void* __cdecl operator new[] (unsigned int size) { return 0; }
void __cdecl ::operator delete (void * p) {}
void __cdecl operator delete[] (void * p) { }

extern "C" float __declspec(naked) _CIcos() {
#ifdef ARCH_X86
   _asm fcos
#endif
};

extern "C" float __declspec(naked) _CIsin() {
#ifdef ARCH_X86
   _asm fsin
#endif
};

extern "C" float __declspec(naked) _CIsqrt() {
#ifdef ARCH_X86
   _asm fsqrt
#endif
};

//! called by MSVC++ to convert a float to a long
extern "C" long __declspec (naked) _ftol2_sse() {
	int a;
#ifdef ARCH_X86
	_asm {
		fistp [a]
		mov	ebx, a
	}
#endif
}

//! required by MSVC++ runtime for floating point operations (Must be 1)
extern "C" int _fltused = 1;

//! enable warning
#pragma? 


and string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//****************************************************************************
//**
//**    [string.cpp]
//**    - [Standard C string routine implimentation]
//**
//****************************************************************************
//============================================================================
//    IMPLEMENTATION HEADERS
//============================================================================

#include <string.h>

//============================================================================
//    IMPLEMENTATION PRIVATE DEFINITIONS / ENUMERATIONS / SIMPLE TYPEDEFS
//============================================================================
//============================================================================
//    IMPLEMENTATION PRIVATE CLASS PROTOTYPES / EXTERNAL CLASS REFERENCES
//============================================================================
//============================================================================
//    IMPLEMENTATION PRIVATE STRUCTURES / UTILITY CLASSES
//============================================================================
//============================================================================
//    IMPLEMENTATION REQUIRED EXTERNAL REFERENCES (AVOID)
//============================================================================
//============================================================================
//    IMPLEMENTATION PRIVATE DATA
//============================================================================
//============================================================================
//    INTERFACE DATA
//============================================================================
//============================================================================
//    IMPLEMENTATION PRIVATE FUNCTION PROTOTYPES
//============================================================================
//============================================================================
//    IMPLEMENTATION PRIVATE FUNCTIONS
//============================================================================
//============================================================================
//    INTERFACE FUNCTIONS
//============================================================================

size_t strlen ( const char* str ) {

	size_t	len=0;
	while (str[len++]); /* careful! MSVC++ optomization might embed call to strlen()*/
	return len;
}

//============================================================================
//    INTERFACE CLASS BODIES
//============================================================================
//****************************************************************************
//**
//**    END[String.cpp]
//**
//**************************************************************************** 

this is all code please explain it ?
Wait. If YOU wrote it why do you need an explanation???
Do you really expect someone to sit down and write you a thesis on how all that works?

Why not ask some specific questions about which parts you find hard to understand?
closed account (Lv0f92yv)
Yes, specific questions would be good. OS development in its entirety is generally very difficult (so I've read, I certainly don't know much about it), so asking people to read through hundreds of lines of code and explain all of it won't be very helpful.

Perhaps if you take smaller sections and ask questions on those, some of those here with more experience will be more willing to help.
There is no point dabbling with OS development before you are an experienced programmer.
Topic archived. No new replies allowed.