1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">
12 <meta content=
"text/html; charset=ISO-8859-1" http-equiv=
"content-type">
21 <title>Devbook
</title>
30 <div style=
"text-align: center;">
31 <h1><span style=
"font-weight: bold; color: rgb(195, 21, 26);">ZeX/OS
</span></h1>
36 <span style=
"font-weight: bold;">Developer's book for v0.4
.1<br>
41 writed by ZeXx86
<br>
52 <div style=
"text-align: left;"><span style=
"font-weight: bold;"></span><br>
57 <span style=
"font-weight: bold;"></span></div>
62 <span style=
"font-weight: bold;"><br>
77 2. Assembler tutorial
<br>
85 5. Filesystem driver tutorial
<br>
96 <hr style=
"width: 100%; height: 2px;"><br>
106 <div style=
"text-align: left;">
107 <span style=
"font-weight: bold;">1.
System calls
<br>
116 In
<a href=
"http://en.wikipedia.org/wiki/Computing" title=
"Computing">computing
</a>, a
<b>system call
</b> is the mechanism used by an application program to request service from the
<a href=
"http://en.wikipedia.org/wiki/Operating_system" title=
"Operating system">operating system
</a>.
<br>
121 ZeX/OS uses this implementation on
<a href=
"http://en.wikipedia.org/wiki/X86" title=
"X86">x86
</a> where the system call number is placed in the EAX
<a href=
"http://en.wikipedia.org/wiki/Processor_register" title=
"Processor register">register
</a> before interrupt
0x80 is executed.
<br>
126 <table style=
"text-align: left; width: 883px; height: 247px;" border=
"1" cellpadding=
"2" cellspacing=
"2">
141 <td style=
"text-align: center; background-color: rgb(51, 204, 255); font-weight: bold; font-style: italic;" valign=
"undefined">Name
</td>
146 <td style=
"background-color: rgb(51, 204, 255); text-align: center; font-weight: bold; font-style: italic;" valign=
"undefined">Description
</td>
151 <td style=
"text-align: center; font-weight: bold; font-style: italic; background-color: rgb(51, 255, 255);" valign=
"undefined">%eax
</td>
156 <td style=
"text-align: center; font-weight: bold; font-style: italic; background-color: rgb(51, 255, 255);" valign=
"undefined">%ebx
</td>
161 <td style=
"text-align: center; font-weight: bold; font-style: italic; background-color: rgb(51, 255, 255);" valign=
"undefined">%ecx
</td>
166 <td style=
"text-align: center; font-weight: bold; font-style: italic; background-color: rgb(51, 255, 255);" valign=
"undefined">%edx
</td>
171 <td style=
"font-weight: bold; font-style: italic; text-align: center; background-color: rgb(51, 255, 255);" valign=
"undefined">$
0x9000</td>
186 <td style=
"text-align: center;" valign=
"undefined">sys_exit
</td>
191 <td style=
"text-align: center;" valign=
"undefined">Exit app at call point
</td>
196 <td style=
"text-align: center;" valign=
"undefined">1</td>
201 <td style=
"text-align: center;" valign=
"undefined">int
</td>
206 <td style=
"text-align: center;" valign=
"undefined">-
</td>
211 <td style=
"text-align: center;" valign=
"undefined">-
</td>
216 <td style=
"text-align: center;" valign=
"undefined">-
</td>
231 <td style=
"text-align: center;" valign=
"undefined">sys_getch
</td>
236 <td style=
"text-align: center;" valign=
"undefined">Get a pressed character
</td>
241 <td style=
"text-align: center;" valign=
"undefined">2</td>
246 <td style=
"text-align: center;" valign=
"undefined">-
</td>
251 <td style=
"text-align: center;" valign=
"undefined">-
</td>
256 <td style=
"text-align: center;" valign=
"undefined">-
</td>
261 <td style=
"text-align: center;" valign=
"undefined">unsigned char
</td>
276 <td style=
"text-align: center;" valign=
"undefined">sys_sleep
</td>
281 <td style=
"text-align: center;" valign=
"undefined">Wait until time (in sec) pass
</td>
286 <td style=
"text-align: center;" valign=
"undefined">3</td>
291 <td style=
"text-align: center;" valign=
"undefined">unsigned int
</td>
296 <td style=
"text-align: center;" valign=
"undefined">-
</td>
301 <td style=
"text-align: center;" valign=
"undefined">-
</td>
306 <td style=
"text-align: center;" valign=
"undefined">-
</td>
321 <td style=
"text-align: center;" valign=
"undefined">sys_putch
</td>
326 <td style=
"text-align: center;" valign=
"undefined">Print a character on the screen
</td>
331 <td style=
"text-align: center;" valign=
"undefined">4</td>
336 <td style=
"text-align: center;" valign=
"undefined">char
</td>
341 <td style=
"text-align: center;" valign=
"undefined">-
</td>
346 <td style=
"text-align: center;" valign=
"undefined">-
</td>
351 <td style=
"text-align: center;" valign=
"undefined">-
</td>
366 <td style=
"text-align: center;" valign=
"undefined">sys_color
</td>
371 <td style=
"text-align: center;" valign=
"undefined">Set color to next text
</td>
376 <td style=
"text-align: center;" valign=
"undefined">5</td>
381 <td style=
"text-align: center;" valign=
"undefined">unsigned int
</td>
386 <td style=
"text-align: center;" valign=
"undefined">unsigned int
</td>
391 <td style=
"text-align: center;" valign=
"undefined">-
</td>
396 <td style=
"text-align: center;" valign=
"undefined">-
</td>
411 <td style=
"text-align: center;" valign=
"undefined">sys_cls
</td>
416 <td style=
"text-align: center;" valign=
"undefined">Clear a screen
</td>
421 <td style=
"text-align: center;" valign=
"undefined">6</td>
426 <td style=
"text-align: center;" valign=
"undefined">-
</td>
431 <td style=
"text-align: center;" valign=
"undefined">-
</td>
436 <td style=
"text-align: center;" valign=
"undefined">-
</td>
441 <td style=
"text-align: center;" valign=
"undefined">-
</td>
456 <td style=
"text-align: center;" valign=
"undefined">sys_getkey
</td>
461 <td style=
"text-align: center;" valign=
"undefined">Get a keycode
</td>
466 <td style=
"text-align: center;" valign=
"undefined">7</td>
471 <td style=
"text-align: center;" valign=
"undefined">-
</td>
476 <td style=
"text-align: center;" valign=
"undefined">-
</td>
481 <td style=
"text-align: center;" valign=
"undefined">-
</td>
486 <td style=
"text-align: center;" valign=
"undefined">unsigned char
</td>
501 <td style=
"text-align: center;" valign=
"undefined">sys_gotoxy
</td>
506 <td style=
"text-align: center;" valign=
"undefined">Move a cursor to another place
</td>
511 <td style=
"text-align: center;" valign=
"undefined">8</td>
516 <td style=
"text-align: center;" valign=
"undefined">unsigned int
</td>
521 <td style=
"text-align: center;" valign=
"undefined">unsigned int
</td>
526 <td style=
"text-align: center;" valign=
"undefined">-
</td>
531 <td style=
"text-align: center;" valign=
"undefined">-
</td>
556 <div style=
"text-align: left;"><br>
560 <span style=
"font-weight: bold;">2.
Assembler tutorial
<br>
568 <span style=
"font-weight: normal;">For people interested in assembler programming for ZeX/OS is small example of source code for
<span style=
"font-style: italic;">nasm
</span> </span><span style=
"font-weight: normal;">compiler.
</span><span style=
"font-weight: bold;"></span><br>
572 <span style=
"font-weight: bold;"><br>
576 <span style=
"font-weight: bold;"></span></span><span style=
"font-style: italic;">>File start.asm
<<br>
581 <table style=
"text-align: left; background-color: rgb(234, 234, 234); width: 521px; height: 544px;" border=
"0" cellpadding=
"2" cellspacing=
"2">
593 <td>[BITS
32]
594 ; first, we need tell to compiler, that we
595 want work in
32bit mode
<br>
599 global _start
; entry point
<br>
603 _start:
; define of _start function
<br>
607 jmp main
; jump to main function
<br>
619 cls:
620 ;
621 clear screen function
<br>
625 mov eax,
6 ; system call number
6<br>
629 int
0x80
630 ; call interrupt for system call
635 ret
636
637 ; end of cls function
<br>
645 putch:
646 ; print
647 chatacter on the screen
<br>
651 mov ebx, '.'
; we want print character '.'
<br>
655 mov eax,
4 ; this is system call number
6<br>
659 int
0x80
660 ; system call interrupt
<br>
664 ret
665
666 ; end of putch function
<br>
678 main:
; main function
<br>
682 call cls
683 ; this clear a screen
<br>
691 mov cl,
10 ; set number
10 to register cl
<br>
699 dec cl
700 ; decrease one number from
705 call putch
; print charecter '.' on the screen
<br>
709 jnz nav
710 ; will be jump to nav until
719 ret
720
721 ; here ends our program
<br>
745 For compile, you need
<span style=
"font-style: italic;">nasm
</span> compiler and
<span style=
"font-style: italic;">ld
</span> for link object.
<br>
749 nasm -f elf -ostart.o start.asm
<br>
753 ld -m elf_i386 -s -omyapp start.o
<br>
757 <span style=
"font-style: italic;"></span><span style=
"font-weight: bold;"><span style=
"font-weight: bold;"></span></span><br>
765 <span style=
"font-weight: bold;">3.
C tutorial
<br>
773 <span style=
"font-weight: bold;"><span style=
"font-weight: bold;"></span></span></span><span style=
"font-weight: normal;">For people interested in C programming language for ZeX/OS is small example of source code for
</span><span style=
"font-style: italic; font-weight: normal;">gcc
</span><span style=
"font-weight: normal;"> compiler.
<br>
777 </span><span style=
"font-weight: bold;"><span style=
"font-weight: bold;"></span><br>
781 </span><span style=
"font-style: italic;">>File main.c
<</span><span style=
"font-style: italic;"><br>
786 <table style=
"text-align: left; background-color: rgb(234, 234, 234); height: 237px; width: 381px;" border=
"0" cellpadding=
"2" cellspacing=
"2">
798 <td>#include
<stdio.h
><br>
814 puts (
"Hello world !");
<br>
818 putch ('\n');
<br>
826 int a =
8;
<br>
830 int b =
5;
<br>
834 int result = a + b;
<br>
842 printf (
"a + b = result\n%d + %d = %d", a, b, result);
<br>
850 return
1;
<br>
882 <span style=
"font-weight: bold;">4.
Glibc
<br>
888 <span style=
"font-weight: bold;"></span></span>The GNU C Library,
889 commonly known as glibc, is the C standard library released by the GNU
890 Project. Originally written by the Free Software Foundation (FSF) for
891 the GNU operating system, the library's development has been overseen
892 by a committee since
2001, with Ulrich Drepper as the lead contributor
899 This version of glibc is completly written by
<span style=
"font-style: italic;">ZeXx86
</span> for light compatibility with GNU programs written in C programming languague on ZeX/OS.
<br>
905 <span style=
"text-decoration: underline;">Include layout
<br>
911 </span>#include
<stdio.h
><span style=
"font-style: italic;"></span><br>
914 <table style=
"text-align: left; background-color: rgb(234, 234, 234); width: 608px; height: 164px;" border=
"0" cellpadding=
"2" cellspacing=
"2">
926 <div style=
"text-align: left;">void putch (char c); //
Print a character on the screen
</div>
929 void puts (unsigned char *text); // Print a string
<br>
932 void printf (const char *fmt, ...); // Same as puts but with arguments
<br>
935 void cls (); // Clear a screen
<br>
938 unsigned char getch (); // Get a pressed character
<br>
941 unsigned char getkey (); // Get a keycode
<br>
944 void gotoxy (int x, int y); // Move a cursor to another place
<br>
947 void setcolor (int t, int f); // Set color to next text
<br>
949 int sprintf(char *buffer, const char *fmt, ...); // Copy string fmt with args to buffer
968 #include
<stdlib.h
><span style=
"font-style: italic;"></span><br>
971 <table style=
"text-align: left; background-color: rgb(234, 234, 234); width: 608px; height: 88px;" border=
"0" cellpadding=
"2" cellspacing=
"2">
980 <td>void *malloc (size_t size); // Allocate block of memory
<br>
983 void *realloc (void *blk, size_t size); // Allocate next block of memory
<br>
986 void free (void *blk); // Free a block of allocated memory
<br>
989 void exit (); //
Exit app at call point
1008 #include
<unistd.h
><span style=
"font-style: italic;"></span><br>
1011 <table style=
"text-align: left; background-color: rgb(234, 234, 234); width: 607px; height: 31px;" border=
"0" cellpadding=
"2" cellspacing=
"2">
1020 <td>unsigned int sleep (unsigned int) // Wait until time (in sec) pass
1039 #include
<string.h
><br>
1042 <table style=
"text-align: left; background-color: rgb(234, 234, 234); width: 756px; height: 164px;" border=
"0" cellpadding=
"2" cellspacing=
"2">
1051 <td>void *memcpy (void *dst_ptr, const void *src_ptr, size_t count); // Copy block of memory
<br>
1054 void *memsetw (void *dst, int val, size_t count); // Put to memory block of
some character
<br>
1057 void *memset (void *dst, int val, size_t count); // Put to memory block of
some character
<br>
1060 size_t strlen (const char *str); // Get length of string
<br>
1063 char *strcpy (char *to, const char *from); // Copy string
<br>
1065 char *strncpy (char *s1, const char *s2, size_t n); // Copy string with declared length
<br>
1068 char *strcat (char *s, const char *add); // Add string to another string
<br>
1071 int strcmp (const char *cs, const char *ct); // Compare two strings
<br>
1074 int strncmp (const char *cs, const char *ct, size_t count); // Compare two strings with defined length
<br>
1076 char *strdup (const char *s); // Alloc new block of memory for string s
<br>
1098 #include
<x86.h
><span style=
"font-style: italic;"></span><br>
1101 <table style=
"text-align: left; background-color: rgb(234, 234, 234); width: 403px; height: 126px;" border=
"0" cellpadding=
"2" cellspacing=
"2">
1110 <td>unsigned inportb (unsigned short port); // Get byte from port
<br>
1113 void outportb (unsigned port, unsigned val); // Set byte on port
<br>
1116 unsigned disable (void); // Disable interrupts
<br>
1119 void enable (void); // Enable interrupts
<br>
1122 void enable_irq (unsigned short irq_num); // Enable irq
<br>
1125 void disable_irq (unsigned short irq_num); // Disable irq
1141 <span style=
"font-weight: bold;">5.
</span><span style=
"font-weight: bold;">Filesystem driver tutorial
<br>
1143 </span>ZeX/OS is developed for no one filesystem, so it supports fs handlers, disk handlers, etc..
<br>
1144 As you should see, in this short tutorial, you will learn how it works
<br>
1147 Base function to read/write data over disk
<span style=
"font-style: italic;"></span><br>
1150 <table style=
"text-align: left; background-color: rgb(234, 234, 234); width: 593px; height: 126px;" border=
"0" cellpadding=
"2" cellspacing=
"2">
1159 <td>/* partition_t is defined in partition.h, addr is
28bit
1160 (
32) address of block, easy addr =
1 is block
1, etc .. buffer is
1161 classical char * for getting data after calling */
<br>
1162 int lba28_drive_read (partition_t *p, unsigned int addr, unsigned char *buffer);
<br>
1164 /* same, but for writing data from buffer */
<br>
1165 int lba28_drive_write (partition_t *p, unsigned char addr, unsigned char *buffer);
<br>
1167 /* when you want device metod, use this */
<br>
1168 dev_t *dev = dev_findbypartition (p);
<br>
1170 if (!dev) // check for existing device
<br>
1171 return
0;
<br>
1173 if (!dev-
>handler (DEV_ACT_READ, p, block, more, n))
<br>
1174 return
0; // error ?
<br>
1176 /* handler is defined in drive.c and function shows like this */
<br>
1177 bool ide_acthandler (unsigned act, partition_t *p, char *block, char *more, int n)
<br>
1194 myfs.c
<span style=
"font-style: italic;"></span><br>
1197 <table style=
"text-align: left; background-color: rgb(234, 234, 234); width: 687px; height: 1057px;" border=
"0" cellpadding=
"2" cellspacing=
"2">
1206 <td>#include
<partition.h
><br>
1207 #include
<system.h
><br>
1208 #include
<string.h
><br>
1209 #include
<dev.h
><br>
1210 #include
<fs.h
><br>
1213 bool myfs_read_dir (partition_t *p, char *block, int n)
<br>
1215 /* its your space for create
1216 function, which read directory and put it to dir[] structure,
<br>
1217 dir is defined in system.h include */
<br>
1218 return
1;
<br>
1222 ...
<br>
1225 /** MyFS handler for handling disk operations
<br>
1226 *
act - action; arg - argument; block - chunk of data for
1227 input/output; n - number of block; l - lenght of
output
<br>
1229 bool myfs_handler (unsigned act, unsigned arg, char *block, unsigned n, unsigned long l)
<br>
1231 switch (act) {
<br>
1232 case FS_ACT_INIT:
<br>
1233 {
<br>
1234 /* something, what
you need to initialize, e.g. memory stacks */
<br>
1235 return
1;
<br>
1236 }
<br>
1237 break;
<br>
1238 case FS_ACT_READ:
<br>
1239 {
<br>
1240 switch (arg) {
<br>
1241 case FS_ARG_FILE:
<br>
1242 {
<br>
1243
1244 myfs_read_file (curr_part, n); //
1245 this have to read file at n block on partition_t *curr_part
<br>
1246 /* ... some stuff for get new data on char *block */
<br>
1247 return
1;
<br>
1248 }
<br>
1249 break;
<br>
1250 case FS_ARG_DIR:
<br>
1251 {
<br>
1252
1253 myfs_read_dir (curr_part, n); //
1254 this is your new fs function too
<br>
1255 /* same as in file operation */
<br>
1256 return
1;
<br>
1257 }
<br>
1258 break;
<br>
1259 }
<br>
1261 return
0;
<br>
1262 }
<br>
1263 break;
<br>
1264 case FS_ACT_WRITE:
<br>
1265 {
<br>
1266 /* Yeah, you
1267 want to probably create FS with write support, you can do it here */
<br>
1268 return
1;
<br>
1269 }
<br>
1270 }
<br>
1272 return
0;
<br>
1290 <span style=
"font-weight: bold;"> </span><span style=
"font-weight: bold;"></span></div>