00001
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
00058 int putchar (char car)
00059 {
00060 char aux[2];
00061 aux[1]='\0';
00062 aux[0]=car;
00063 puts(aux);
00064 }
00065
00066
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
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
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