code:getc - ikarishinjieva/unixV6-code-analyze-chs GitHub Wiki
- 从字符缓存池中读入一个字符
- 传入1个参数:
- 其值为struct clist的地址
- 返回值:
- 若I/O字符缓存队列为空,返回-1
- 否则返回读取到的字符
0923 / Character list get/put
0924
0925 /* ------------------------- */
0926 .globl _getc, _putc
0927 /* ------------------------- */
0928 .globl _cfreelist
0929
0930 _getc:
0931 mov 2(sp),r1
0932 mov PS,-(sp)
0933 mov r2,-(sp)
0934 bis $340,PS
0935 bic $100,PS / spl 5
0936 mov 2(r1),r2 / first ptr
- 处理机优先级置为5,关中断
0937 beq 9f / empty
0938 movb (r2)+,r0 / character
- 若I/O字符缓存队列的队首指针为空,表明缓存队列为空,跳转至960行
0939 bic $!377,r0
- R2在将I/O字符缓存队列队首字符读出至R0的同时,自己指向了下一个字符
0940 mov r2,2(r1)
- 清R0符号位
0941 dec (r1)+ / count
0942 bne 1f
0943 clr (r1)+
0944 clr (r1)+ / last block
0945 br 2f
0946 1:
0947 bit $7,r2
0948 bne 3f
- 判断寄存器R2的值最后三位是否为000
- 若为000,表明当前的缓存已被输出完,可以释放,原因请参考缓存池的改造
0949 mov -10(r2),(r1) / next block
0950 add $2,(r1)
- 本句作用为将下一缓存的头指针赋给了clist.c-cf
- 注:此处10为八进制数
0951 2:
- 将clist.c-cf指向I/O字符缓存队列中的第一个字符(加2意味着跳过了c-next)
0952 dec r2
0953 bic $7,r2
0954 mov _cfreelist,(r2)
0955 mov r2,_cfreelist
0956 3:
- 952 - 955
- 为将用完的缓存链入自由字符缓存队列队首的操作
0957 mov (sp)+,r2
0958 mov (sp)+,PS
0959 rts pc
0960 9:
0961 clr 4(r1)
0962 mov $-1,r0
0963 mov (sp)+,r2
0964 mov (sp)+,PS
0965 rts pc
0966