code:printf - ikarishinjieva/unixV6-code-analyze-chs GitHub Wiki

Source

  • 类似于C中的printf函数,按指定格式输出内容
  • 共传入两个参数:fmt 和 x1 (其他参数未调用)
  • fmt : 格式串,格式保留字包括 %s %l %d(=%l) %o
  • &x1 : 数据指针

2329

2330 /*

2331  * Scaled down version of C library printf.

2332  * Only %s %l %d (==%l) %o are recognized.

2333  * Used to print diagnostic information

2334  * directly on console tty.

2335  * Since it is not interrupt driven,

2336  * all system activities are pretty much

2337  * suspended.

2338  * Printf should not be used for chit-chat.

2339  */

2340 printf(fmt,x1,x2,x3,x4,x5,x6,x7,x8,x9,xa,xb,xc)

2341 char fmt[];

2342 {

2343     register char *s;

2344     register *adx, c;

2345

2346     adx = &x1;

以下开始扫描格式串,并逐一转换输出
2347 loop:

2348     while((c = *fmt++) != ’%’) {

2349          if(c == ’\0’)

2350          return

2351     putchar(c);

2352 }

  • 若当前扫描的字符并非保留字,则直接输出
  • 若格式串读到末尾(\0),则函数返回
  • 当读到格式保留字时,往下执行
2353     c = *fmt++;

2354     if(c == ’d’ || c == ’l’ || c == ’o’)

2355          printn(*adx, c==’o’? 8: 10);

  • 转换并输出数字格式
  • 2355判断输出八进制或十进制数
2356     if(c == ’s’) {

2357          s = *adx;

2358          while(c = *s++)

2359           putchar(c);

2360     }

  • 输出字符串
2361     adx++;

2362     goto loop;

  • 返回2347,进行下一次扫描
2363 }

2364 /* -------------------------*/

Caller

⚠️ **GitHub.com Fallback** ⚠️