initial commit
[pfinal.git] / Routix / tareas / punto / old / routix.c
blobe9b12630c1ec2da582b421b54618dcd9c41700eb
1 /* routix.c */
2 #include "stdarg.h"
3 #include "routix.h"
4 #include "syscalls.h"
5 void sprintn ( unsigned int num, int base);
6 void sputchar (char car);
7 char getascii (char c);
9 #define _syscall0(numero,retorno) __asm__ __volatile__ ("int $0x50" : "=a" (retorno) : "a" (numero))
10 #define _syscall1(numero,retorno,param1) __asm__ __volatile__ ("int $0x50" : "=a" (retorno) : "a" (numero), "b" (param1))
11 #define _syscall2(numero,retorno,param1,param2) __asm__ __volatile__ ("int $0x50" : "=a" (retorno) : "a" (numero), "b" (param1), "c" (param2))
14 int sleep(int segundos)
16 int retorno;
18 _syscall1( SYS_TIMER | SYS_SLEEP, retorno, segundos);
20 return retorno;
23 int exec (char *tarea)
25 __asm__ __volatile__ ("movl %0, %%eax ; movl %1, %%ebx" : : "g" (SYS_PROCESS | SYS_EXEC) , "g" (tarea) : "eax" , "ebx");
26 __asm__ __volatile__ ("int $0x50");
28 int retorno;
29 __asm__ __volatile__ ("movl %%eax, %0" : "=g" (retorno) );
31 return retorno;
35 void gets (char *str)
38 __asm__ __volatile__ ("movl %0, %%eax ; movl %1, %%ebx" : : "g" (SYS_CONSOLE | SYS_GETS) , "g" (str) : "eax" , "ebx" );
39 __asm__ __volatile__ ("int $0x50");
43 int fork(void)
45 __asm__ __volatile__ ("movl %0, %%eax" : : "g" (SYS_PROCESS | SYS_FORK) : "eax");
46 __asm__ __volatile__ ("int $0x50");
50 void voido (void)
52 __asm__ __volatile__ ("movl %0, %%eax" : : "g" (SYS_PROCESS | SYS_VOID) : "eax");
53 __asm__ __volatile__ ("int $0x50");
57 // Funcion de libraria "putchar"
58 int putchar (char car)
60 char aux[2];
61 aux[1]='\0';
62 aux[0]=car;
63 puts(aux);
66 // llamada al sistema (similar a write pero hacia stdout)
67 void puts(char *str)
70 __asm__ __volatile__ ("movl %0, %%eax ; mov %1, %%ebx ; mov %2, %%ecx" : : \
71 "g" (SYS_CONSOLE | SYS_PRINT), "g" (str), "g" (strlen(str)) : "eax", "ebx", "ecx" );
73 __asm__ __volatile__ ("int $0x50");
76 int clrscr(void)
78 __asm__ __volatile__ ("movl %0, %%eax" : : "g" (SYS_CONSOLE | SYS_CLRSCR));
79 __asm__ __volatile__ ("int $0x50");
82 #define MAX_STRING 100
84 word sposicion=0;
85 char string_stdio[MAX_STRING];
87 void sputchar (char car)
89 string_stdio[sposicion] = car;
90 string_stdio[++sposicion] = '\0';
93 void printf ( char *stri, ...)
95 string_stdio[0]='\0';
96 char *p=stri;
97 char *d;
98 char nibble[8];
99 char car;
101 unsigned int i,flag;
103 va_list argumentos;
105 va_start(argumentos, stri );
107 for ( p=stri; *p ; p++ ) {
108 if ( *p != '%' ) {
109 sputchar(*p);
110 continue;
113 switch (*++p) {
115 case 'c':
116 car=va_arg(argumentos, int);
117 sputchar( (char) car);
118 break;
120 case 'x':
121 i = va_arg(argumentos, unsigned int );
122 sprintn(i,16);
123 break;
125 case 'd':
126 i = va_arg(argumentos, unsigned int);
127 sprintn(i,10);
128 break;
130 case 'o':
131 i = va_arg(argumentos, unsigned int);
132 sprintn(i,8);
133 break;
135 case 's':
136 d = va_arg(argumentos, char *);
137 strcat(string_stdio, d);
138 sposicion = sposicion + strlen(d);
139 break;
141 default:
142 sputchar(*p);
143 break;
148 va_end(argumentos);
149 puts(string_stdio);
154 void sprintn( unsigned int num, int base)
156 unsigned int div;
157 if ( (div=num/base) )
158 sprintn(div,base);
159 sputchar( getascii(num%base) );
165 //****************************************************************************************************
166 // printf ( char *string, ...) y funciones requeridas por ella
167 //****************************************************************************************************
169 char getascii_ ( char c );
170 void printn_ ( unsigned int num, int base);
172 void printf ( char *string, ...)
175 char *p=string;
176 char *d;
177 char nibble[8];
178 char car;
180 unsigned int i,flag;
182 va_list argumentos;
184 va_start(argumentos, string );
186 for ( p=string; *p ; p++ ) {
187 if ( *p != '%' ) {
188 putchar(*p);
189 continue;
192 switch (*++p) {
194 case 'c':
195 car=va_arg(argumentos, int);
196 putchar( (char) car);
197 break;
199 case 'x':
200 i = va_arg(argumentos, unsigned int );
201 printn_(i,16);
202 break;
204 case 'd':
205 i = va_arg(argumentos, unsigned int);
206 printn_(i,10);
207 break;
209 case 'o':
210 i = va_arg(argumentos, unsigned int);
211 printn_(i,8);
212 break;
214 case 's':
215 d = va_arg(argumentos, char *);
216 puts(d);
217 break;
219 default:
220 putchar(*p);
221 break;
226 va_end(argumentos);
229 void printn_ ( unsigned int num, int base)
231 unsigned int div;
232 if ( (div=num/base) ) printn_(div,base);
233 putchar( getascii_(num%base) );
236 char getascii ( char c )
238 char valor = '0' + c;
240 if ( valor > '9' ) valor += 7;
241 return valor;