Exercise 1.8
我们丢弃了一小部分代码---即当我们在printf中指定输出"%o"格式的字符串,即八进制格式的代码。尝试去完成这部分程序。
解答:
在这个练习中我们首先要阅读以下三个源文件的代码,弄清楚他们三者之间的关系:
三个文件分别为 \kern\printf.c,\kern\console.c, \lib\printfmt.c
首先大致浏览三个源文件,其中粗略的观察到3点:
1.\kern\printf.c中的cprintf,vcprintf子程序调用了\lib\printfmt.c中的vprintfmt子程序。
2.\kern\printf.c中的putch子程序中调用了cputchar,这个程序是定义在\kern\console.c中的。
3.\lib\printfmt.c中的某些程序也依赖于cputchar子程序
所以得出结论,\kern\printf.c,\lib\printfmt.c两个文件的功能依赖于\kern\console.c的功能。所以我们就先探究一下\kern\console.c。
1.\kern\console.c
这个文件中定义了如何把一个字符显示到console上,即我们的显示屏之上,里面包括很多对IO端口的操作。
其中我们最感兴趣的自然就是cputchar子程序了。下面是这个程序的代码
// `High'-level console I/O. Used by readline and cprintf. void cputchar(int c) { cons_putc(c); } // output a character to the console static void cons_putc(int c) { serial_putc(c); lpt_putc(c); cga_putc(c); }