routix.c

Go to the documentation of this file.
00001 /* routix.c */
00002 #include "stdarg.h"
00003 #include "routix.h"
00004 #include "syscalls.h"
00005 void sprintn ( unsigned int num, int base);
00006 void sputchar (char car);
00007 char getascii (char c);
00008 
00009 #define _syscall0(numero,retorno) __asm__ __volatile__ ("int $0x50" : "=a" (retorno) : "a" (numero))
00010 #define _syscall1(numero,retorno,param1) __asm__ __volatile__ ("int $0x50" : "=a" (retorno) : "a" (numero), "b" (param1))
00011 #define _syscall2(numero,retorno,param1,param2) __asm__ __volatile__ ("int $0x50" : "=a" (retorno) : "a" (numero), "b" (param1), "c" (param2))
00012 
00013 
00014 int sleep(int segundos)
00015 {
00016   int retorno;
00017 
00018  _syscall1( SYS_TIMER | SYS_SLEEP, retorno, segundos);
00019   
00020  return retorno;
00021 }
00022 
00023 int exec (char *tarea)
00024 {
00025     __asm__ __volatile__ ("movl %0, %%eax ; movl %1, %%ebx" : : "g" (SYS_PROCESS | SYS_EXEC) , "g" (tarea) : "eax" , "ebx");
00026     __asm__ __volatile__ ("int $0x50");
00027 
00028      int retorno;
00029     __asm__ __volatile__ ("movl %%eax, %0" : "=g" (retorno)  );
00030  
00031     return retorno;
00032 }       
00033 
00034 
00035 void gets (char *str)
00036 {
00037 
00038     __asm__ __volatile__ ("movl %0, %%eax ; movl %1, %%ebx" : : "g" (SYS_CONSOLE | SYS_GETS) , "g" (str) : "eax" , "ebx" );
00039     __asm__ __volatile__ ("int $0x50");
00040 
00041 }       
00042 
00043 int fork(void)
00044 {
00045     __asm__ __volatile__ ("movl %0, %%eax" : : "g" (SYS_PROCESS | SYS_FORK) : "eax");
00046     __asm__ __volatile__ ("int $0x50");
00047 }       
00048 
00049 
00050 void voido (void)
00051 {
00052     __asm__ __volatile__ ("movl %0, %%eax" : : "g" (SYS_PROCESS | SYS_VOID) : "eax");
00053     __asm__ __volatile__ ("int $0x50");
00054     
00055 }       
00056 
00057 // Funcion de libraria "putchar"
00058 int putchar (char car)
00059 {
00060     char aux[2];
00061     aux[1]='\0';
00062     aux[0]=car;
00063     puts(aux);
00064 }       
00065 
00066 // llamada al sistema (similar a write pero hacia stdout)
00067 void puts(char *str)
00068 {
00069 
00070     __asm__ __volatile__ ("movl %0, %%eax ; mov %1, %%ebx ; mov %2, %%ecx" : : \
00071                     "g" (SYS_CONSOLE | SYS_PRINT), "g" (str), "g" (strlen(str)) : "eax", "ebx", "ecx" );
00072 
00073     __asm__ __volatile__ ("int $0x50");
00074 }       
00075 
00076 int clrscr(void)
00077 {
00078     __asm__ __volatile__ ("movl %0, %%eax" : : "g" (SYS_CONSOLE | SYS_CLRSCR));
00079     __asm__ __volatile__ ("int $0x50");
00080 }       
00081 
00082 #define MAX_STRING  100
00083 
00084 word sposicion=0;
00085 char string_stdio[MAX_STRING];
00086 
00087 void sputchar (char car)
00088 {
00089     string_stdio[sposicion] = car;
00090     string_stdio[++sposicion] = '\0';
00091 }       
00092 
00093 void printf ( char *stri, ...)
00094 {
00095     string_stdio[0]='\0';    
00096     char *p=stri;
00097     char *d;
00098     char nibble[8];
00099     char car;
00100 
00101     unsigned int i,flag;
00102         
00103     va_list argumentos;
00104 
00105     va_start(argumentos, stri );
00106 
00107     for ( p=stri; *p ; p++ ) {
00108         if ( *p != '%' ) {
00109             sputchar(*p);
00110             continue;
00111         }
00112   
00113         switch (*++p) {
00114 
00115         case 'c':
00116              car=va_arg(argumentos, int);     
00117              sputchar( (char) car);
00118              break;
00119         
00120         case 'x':
00121              i = va_arg(argumentos, unsigned int );
00122              sprintn(i,16);
00123              break;
00124 
00125         case 'd':
00126              i = va_arg(argumentos, unsigned int);
00127              sprintn(i,10);
00128              break;
00129 
00130         case 'o':
00131              i = va_arg(argumentos, unsigned int);
00132              sprintn(i,8);
00133              break; 
00134 
00135         case 's':
00136              d = va_arg(argumentos, char *);
00137              strcat(string_stdio, d);
00138              sposicion = sposicion + strlen(d);
00139              break;
00140         
00141         default:
00142              sputchar(*p);
00143              break;
00144         }
00145         
00146     }
00147   
00148     va_end(argumentos);
00149     puts(string_stdio);
00150 }
00151 
00152 
00153 
00154 void sprintn( unsigned int num, int base)
00155 {
00156     unsigned int div;
00157     if ( (div=num/base) ) 
00158         sprintn(div,base);
00159     sputchar( getascii(num%base) );
00160 }
00161 
00162 
00163 
00164 
00165 //****************************************************************************************************
00166 // printf ( char *string, ...)  y funciones requeridas por ella
00167 //****************************************************************************************************
00168 /*
00169 char getascii_ ( char c );
00170 void printn_ ( unsigned int num, int base);
00171 
00172 void printf ( char *string, ...)
00173 {
00174 
00175  char *p=string;
00176  char *d;
00177  char nibble[8];
00178  char car;
00179 
00180  unsigned int i,flag;
00181         
00182  va_list argumentos;
00183 
00184  va_start(argumentos, string );
00185 
00186  for ( p=string; *p ; p++ ) {
00187   if ( *p != '%' ) {
00188    putchar(*p);
00189    continue;
00190   }
00191   
00192   switch (*++p) {
00193 
00194    case 'c':
00195              car=va_arg(argumentos, int);     
00196              putchar( (char) car);
00197              break;
00198 
00199    case 'x':
00200              i = va_arg(argumentos, unsigned int );
00201              printn_(i,16);
00202              break;
00203 
00204    case 'd':
00205              i = va_arg(argumentos, unsigned int);
00206              printn_(i,10);
00207              break;
00208 
00209    case 'o':
00210              i = va_arg(argumentos, unsigned int);
00211              printn_(i,8);
00212              break; 
00213 
00214    case 's':
00215              d = va_arg(argumentos, char *);
00216              puts(d);
00217              break;
00218 
00219    default:
00220              putchar(*p);
00221              break;
00222   }
00223         
00224  }
00225   
00226  va_end(argumentos);
00227 }
00228 
00229 void printn_ ( unsigned int num, int base)
00230 {
00231  unsigned int div;
00232  if ( (div=num/base) ) printn_(div,base);
00233  putchar( getascii_(num%base) );
00234 }
00235 */
00236 char getascii ( char c )
00237 {
00238  char valor = '0' + c;
00239 
00240  if ( valor > '9' ) valor += 7;
00241  return valor;
00242 }
00243 
00244 

Generated on Sun May 30 18:38:35 2004 for Routix OS by doxygen 1.3.6