* added compilers lcc and bcc (linux86)
[mascara-docs.git] / i86 / mtx / mtx / io.html
blobbecdb2ac17ff0b6e07abfd2afaa4c7e9eeedc561
1 <html><body><pre>
2 460 Notes on I/O Functions
4 When MTX starts, there is no OS support except BIOS. Therefore, all terminal
5 I/O must depend on getc()/putc() of BIOS. You have already implemented gets()
6 and prints(). Here, we outline how to implement a simple printf() function for
7 formatted printing of numbers in %c %s, %u, %d, %x, %l format. First, we
8 implement a printu() for printing unsigned (short) integers.
10 typedef unsigned char u8;
11 typedef unsigned short u16;
12 typedef unsigned long u32;
14 char *ctable = "0123456789ABCDEF";
15 u16 BASE = 10;
17 int rpu(u16 x)
19 char c;
20 if (x){
21 c = ctable[x % BASE];
22 rpu(x / BASE);
24 putc(c);
27 int printu(u16 x)
29 if (x==0)
30 putc('0');
31 else
32 rpu(x);
33 putc(' ');
36 where rpu(x) recursively generates the digits of x % 10 and prints them on the
37 return path. For example, if x=123, the digits are generated in the order of
38 '3', 2', '1' but printed as '1', '2', '3' as they should. With printu(),
39 implementing a printd(), which prints signed integers, becomes trivial.
41 By setting BASE = 16, we can print in hex. By changing the parameter type to
42 u32, we can print long values, e.g. LBA sector and inode numbers. Assume that we
43 have prints(), printu(), printd(), printx(), printl() and printX(), where
44 printl() and printX() print 32-bit values in decimal and hex, respectively. It
45 is easy to implement a simple printf(char *fmt, ...) for formatted printing,
46 where fmt is a format string containing conversion symbols %c, %s, %u, %d, %x,
47 %l, %X.
49 Note that in 16-bit mode all parameters are passed as u16. For LONG values, you
50 must cast them by (long)item or (u32)item in function calls to force the BCC
51 compiler to pass them as 32-bit items.
53 int printf(char *fmt)
55 char *cp = fmt; // cp points at the fmt string
56 u16 *ip = (int *)&fmt + 1; // ip points at first item to be printed on stack
57 u32 *up;
58 while (*cp){
59 if (*cp != '%'){
60 putc(*cp);
61 if (*cp=='\n')
62 putc('\r');
63 cp++;
64 continue;
66 cp++;
67 switch(*cp){
68 case 'c' : putc (*ip); break;
69 case 's' : prints(*ip); break;
70 case 'u' : printu(*ip); break;
71 case 'd' : printi(*ip); break;
72 case 'x' : printx(*ip); break;
73 case 'l' : printl(*(u32 *)ip++); break;
74 case 'X' : printX(*(u32 *)ip++); break;
76 cp++; ip++;
79 This basic printf() function does not support field width or precision but it
80 is adequate for the simple printing task of MTX.