﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-创新无极限-文章分类-c语言</title><link>http://www.cppblog.com/sgq116300/category/5548.html</link><description>Sun Guoqing的Blog</description><language>zh-cn</language><lastBuildDate>Tue, 20 May 2008 04:07:16 GMT</lastBuildDate><pubDate>Tue, 20 May 2008 04:07:16 GMT</pubDate><ttl>60</ttl><item><title>内核printf源代码分析  </title><link>http://www.cppblog.com/sgq116300/articles/36573.html</link><dc:creator>sunGuoqin</dc:creator><author>sunGuoqin</author><pubDate>Wed, 14 Nov 2007 04:13:00 GMT</pubDate><guid>http://www.cppblog.com/sgq116300/articles/36573.html</guid><wfw:comment>http://www.cppblog.com/sgq116300/comments/36573.html</wfw:comment><comments>http://www.cppblog.com/sgq116300/articles/36573.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/sgq116300/comments/commentRss/36573.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/sgq116300/services/trackbacks/36573.html</trackback:ping><description><![CDATA[<span id="ArticleContent1_ArticleContent1_lblContent">
<p>打开Source Insight来阅读EduOS的源代码,我们在stdio.c里找到了printf的实现代码.首先看看对printf的定义:<br>[code]<br>int printf (const char *cntrl_string, ...)<br>[/code]<br>第一个参数cntrl_string是控制字符串,也就是平常我们写入%d,%f的地方.紧接着后面是一个变长参数.</p>
<p>看看函数头部的定义:</p>
<p>&nbsp; [code]int pos = 0, cnt_printed_chars = 0, i; <br>&nbsp; unsigned char* chptr;<br>&nbsp; va_list ap;[/code]<br>马上晕!除了ap我们可以马上判断出来是用来读取变长参数的,i用于循环变量.其他变量都不知道是怎么回事.不要着急,我们边看代码边分析.代码的第一行必然是</p>
<p>[code]va_start (ap, cntrl_string);[/code]<br>用来初始化变长参数.</p>
<p>接下来是一个while循环</p>
<p>[code]while (cntrl_string[pos]) {<br>...<br>}[/code]</p>
<p>结束条件是cntrl_string[pos]为NULL,显然这个循环是用来遍历整个控制字符串的.自然pos就是当前遍历到的位置了.进入循环首先闯入视线的是</p>
<p>[code] if (cntrl_string[pos] == '%') {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pos++;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...<br>&nbsp;} [/code]</p>
<p>开门见山,上来就当前字符是否办断是否%.一猜就知道如果成立pos++马上取出下一个字符在d,f,l等等之间进行判断.往下一看,果真不出所料:</p>
<p>[code]switch (cntrl_string[pos]) {<br>&nbsp;&nbsp;&nbsp; case 'c':<br>...<br>&nbsp;&nbsp;&nbsp; case 's':<br>...<br>&nbsp;&nbsp;&nbsp; case 'i':<br>...<br>&nbsp;&nbsp;&nbsp; case 'd':<br>...<br>&nbsp;&nbsp;&nbsp; case 'u':<br>...[/code]</p>
<p>用上switch-case了. 快速浏览一下下面的代码.</p>
<p>首先看看case 'c'的部分</p>
<p>[code]case 'c':<br>&nbsp;putchar (va_arg (ap, unsigned char));<br>&nbsp;cnt_printed_chars++;<br>&nbsp;break;[/code]</p>
<p>%c表示仅仅输出一个字符.因此先通过va_arg进行参数的类型转换,之后用putchar[1]输出到屏幕上去.之后是<br>cnt_printed_chars++,通过这句我们就可以判断出cnt_printed_chars使用来表示,已经被printf输出的字符个数的.</p>
<p>再来看看 case 's':<br>[code]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 's':<br>&nbsp;chptr = va_arg (ap, unsigned char*);<br>&nbsp;i = 0;<br>&nbsp;while (chptr [i]) {<br>&nbsp;&nbsp; cnt_printed_chars++;<br>&nbsp;&nbsp; putchar (chptr [i++]);<br>&nbsp;}<br>&nbsp;break;[/code]和case 'c',同出一辙.cnt_printed_chars++放在了循环内,也证明了刚才提到的他的作用.另外我们也看到了cnptr是用来在处理字符串时的位置指针.到此为止,我们清楚的所有变量的用途,前途变得更加光明了.</p>
<p>接下来:<br>[code]// PartI<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 'i':<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 'd':<br>&nbsp;cnt_printed_chars += printInt (va_arg (ap, int));<br>&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 'u':<br>&nbsp;cnt_printed_chars += printUnsignedInt (va_arg (ap, unsigned int));<br>&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 'x':<br>&nbsp;cnt_printed_chars += printHexa (va_arg (ap, unsigned int), 'x');<br>&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 'X':<br>&nbsp;cnt_printed_chars += printHexa (va_arg (ap, unsigned int), 'X');<br>&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 'o':<br>&nbsp;cnt_printed_chars += printOctal (va_arg (ap, unsigned int));<br>&nbsp;break;<br>// Part II<br>&nbsp;case 'p':<br>&nbsp;putchar ('0');<br>&nbsp;putchar ('x');<br>&nbsp;cnt_printed_chars += 2; /* of '0x' */<br>&nbsp;cnt_printed_chars += printHexa (va_arg (ap, unsigned int), 'x');<br>&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case '#':<br>&nbsp;pos++;<br>&nbsp;switch (cntrl_string[pos]) {<br>&nbsp;case 'x':<br>&nbsp;&nbsp; putchar ('0');<br>&nbsp;&nbsp; putchar ('x');<br>&nbsp;&nbsp; cnt_printed_chars += 2; /* of '0x' */<br>&nbsp;&nbsp; cnt_printed_chars += printHexa (va_arg (ap, unsigned int), 'x');<br>&nbsp;&nbsp; break;<br>&nbsp;case 'X':<br>&nbsp;&nbsp; putchar ('0');<br>&nbsp;&nbsp; putchar ('X');<br>&nbsp;&nbsp; cnt_printed_chars += 2; /* of '0X' */<br>&nbsp;&nbsp; cnt_printed_chars += printHexa (va_arg (ap, unsigned int), 'X');<br>&nbsp;&nbsp; break;<br>&nbsp;case 'o':<br>&nbsp;&nbsp; putchar ('0');<br>&nbsp;&nbsp; cnt_printed_chars++;<br>&nbsp;&nbsp; cnt_printed_chars += printOctal (va_arg (ap, unsigned int));<br>&nbsp;&nbsp; break;[/code]<br>注意观察一下,PartII的代码其实就是比PartI的代码多一个样式.在16进制数或八进制前加入0x或是o,等等.因此这里就只分析一下PartI咯.</p>
<p>其实仔细看看PartI的个条case,也就是把参数分发到了更具体的函数用于显示,然后以返回值的形式返回输出个数.对于这些函数就不具体分析了.我们先来看看一些善后处理:</p>
<p>先看case的default处理.<br>[code]default:<br>&nbsp;putchar ((unsigned char) cntrl_string[pos]);<br>&nbsp;cnt_printed_chars++;[/code]就是直接输出cntrl_string里%号后面的未知字符.应该是一种容错设计处理.</p>
<p>再看看if (cntrl_string[pos] == '%')的else部分<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>[code]else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putchar ((unsigned char) cntrl_string[pos]);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cnt_printed_chars++;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pos++;<br>&nbsp;}[/code]<br>如果不是%开头的,那么直接输出这个字符.</p>
<p>最后函数返回前<br>&nbsp; [code]va_end (ap);<br>&nbsp; return cnt_printed_chars;[/code]va_end处理变长参数的善后工作.并返回输出的字符个数.</p>
<p>在最后我们有必要谈谈putChar函数以及基本输出的基础函数printChar,先来看看putChar</p>
<p>[code]int putchar (int c) {<br>&nbsp; switch ((unsigned char) c) {<br>&nbsp; case '\n' :<br>&nbsp;&nbsp;&nbsp; newLine ();<br>&nbsp;&nbsp;&nbsp; break;<br>&nbsp; case '\r' :<br>&nbsp;&nbsp;&nbsp; carriageReturn ();<br>&nbsp;&nbsp;&nbsp; break;<br>&nbsp; case '\f' :<br>&nbsp;&nbsp;&nbsp; clearScreen ();<br>&nbsp;&nbsp;&nbsp; break;<br>&nbsp; case '\t' :<br>&nbsp;&nbsp;&nbsp; printChar (32); printChar (32); /* 32 = space */<br>&nbsp;&nbsp;&nbsp; printChar (32); printChar (32);<br>&nbsp;&nbsp;&nbsp; printChar (32); printChar (32); <br>&nbsp;&nbsp;&nbsp; printChar (32); printChar (32);<br>&nbsp;&nbsp;&nbsp; break;<br>&nbsp; case '\b':<br>&nbsp;&nbsp;&nbsp; backspace ();<br>&nbsp;&nbsp;&nbsp; break;<br>&nbsp; case '\a':<br>&nbsp;&nbsp;&nbsp; beep ();<br>&nbsp;&nbsp;&nbsp; break;<br>&nbsp; default :<br>&nbsp;&nbsp;&nbsp; printChar ((unsigned char) c);<br>&nbsp; }<br>&nbsp; return c;<br>}[/code]<br>通
览一下,也是switch-case为主体的.主要是用来应对一些特殊字符,如\n,\r,....这里需要提一下,关于\t的理解.有些人认为\t就是
8个space,有些人则认为,屏幕分为10大列(每个大列8个小列总共80列).一个\t就跳到下一个大列输出.也就是说不管你现在实在屏幕的第
1,2,3,4,5,6,7位置输出字符,只要一个\t都在第8个位置开始输出.
VS.NET中就是用的这种理解.因此如果按照这个理解的话,\t的实现可以这样</p>
<p>[code]int currentX = ((currentX % 10) + 1) * 8;[/code]</p>
<p>然后在currentX位置输出.</p>
<p>接下来看printChar也就是输出部分最低层的操作咯</p>
<p>[code]void printChar (const byte ch) {<br>&nbsp; *(word *)(VIDEO + y * 160 + x * 2) = ch | (fill_color &lt;&lt; 8); <br>&nbsp; x++;<br>&nbsp; if (x &gt;= WIDTH)<br>&nbsp;&nbsp;&nbsp; newLine ();<br>&nbsp; setVideoCursor (y, x);<br>}[/code]
这里VIDEO表示显存地址也就是0xB8000.通过 y * 160 + x
屏幕(x,y)坐标在显存中的位置.这里需要知道,一个字符显示需要两个字节,一个是ASCII码,第二个是字符属性代码也就是颜色代码.因此才必须
y * 80 * 2 + x = y * 160 + x.那么ch | (fill_color &lt;&lt;
8)也自然就是写入字符及属性代码用的了.每写一个字符光标位置加1,如果大于屏幕宽度WIDTH就换行.最后通过setVideoCursor设置新的
光标位置.完成了整个printChar过程.</p>
</span><img src ="http://www.cppblog.com/sgq116300/aggbug/36573.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/sgq116300/" target="_blank">sunGuoqin</a> 2007-11-14 12:13 <a href="http://www.cppblog.com/sgq116300/articles/36573.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>