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

Table of Contents

Source

  • 向字符缓存池写入一个字符
  • 传入2个参数
  • 返回值
    • 若成功写入,返回0
    • 否则返回一个非零值

0967 _putc:

0968     mov 2(sp),r0

0969     mov 4(sp),r1

0970     mov PS,-(sp)

0971     mov r2,-(sp)

0972     mov r3,-(sp)

0973     bis $340,PS

0974     bic $100,PS           / spl 5

0975     mov 4(r1),r2           / last ptr

0976     bne 1f

  • 判断clist.c-cl是否为空指针,若不是,跳到983行执行,否则继续
0977     mov _cfreelist,r2
0978     beq 9f
0979     mov (r2),_cfreelist
0980     clr (r2)
  • 新分配缓存的c_next 置为 NULL
  • 同时 R2指向新分配缓存块的第一个字符存放的位置
0981     mov r2,2(r1)+           / first ptr

0982     br 2f

0983 1:

0984     bit $7,r2

0985     bne 2f

  • 判断寄存器R2的值最后三位是否为000
0986     mov _cfreelist,r3

0987     beq 9f

0988     mov (r3),_cfreelist

  • 986 - 998行类似于977 -979行,此处不再赘述
0989     mov r3,-10(r2)

0990     mov r3,r2

  • 队尾缓存块的c_next → R3,R3 → R2
  • 于是当前R2中存放的是新分配缓存块的首地址
0991     clr (r2)+
  • 新分配缓存的c_next 置为 NULL
  • 同时 R2指向新分配缓存块的第一个字符存放的位置
0992 2:

0993     movb r0,(r2)+

0994     mov r2,4(r1)

0995     inc (r1) / count

0996     clr r0
  • 0 → R0,用于返回0,表明字符写入缓存池成功
0997     mov (sp)+,r3

0998     mov (sp)+,r2

0999     mov (sp)+,PS

1000     rts pc

  • 以下为缓存分配失败后的处理
1001 9:

1002     mov pc,r0

  • 0 → R0,用于返回一个非0值,表明字符写入缓存池失败
1003     mov (sp)+,r3

1004     mov (sp)+,r2

1005     mov (sp)+,PS

1006     rts pc

Extend

附图

Ref

Caller

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