UNIX 6th code reading - prf.c(english translation)

this entry is the english translation of http://d.hatena.ne.jp/takahirox/20101123/1290485182

introduction

today, i'll make the notes of prf.c. it deals with print messages, panic and so on.

this entry follows http://d.hatena.ne.jp/takahirox/20110423/1303541336

printf

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

the kernel accesses from x2 to xc with shifting pointer from x1(2346, 2361). so it doesn't care the number of arguments.

the following figure shows the address of arguments. the kernel can access next argument with increasing the pointer.


2361 : adx++;
  • 2348 - 2352 : handling fmt one character at a time
    • 2348 : break the loop if '%'
    • 2349 - 2350 : return if '\0'(the end of strings). here is the only point to leave printf( )
    • 2351 : if not them call putchar( ) and then display a character
  • 2353 : fmt points the next to '%' at this point. c is assigned to the next character to '%'
    • 2354 - 2355 : calling println( ) and display a number if '%d', '%l' or '%c'
    • 2356 - 2360 : display xn if '%s'

printn

http://www.asahi-net.or.jp/~ax2s-kmtn/ref/bdh.html#binary

printn( ) is implemented as the logic "repeat division with cardinal number and then translated value results in remainder" that is explained at the above url. ldiv( ) and lrem( ) is implemented with assembly language(why?).

putchar

putchar( ) use device registers, so you can understand it more with seeing chapter 24.

2393 : while((KL->xsr&0200) == 0 )
2394 :         ;

busy waiting until the 7bit of transmitter status register is set as 1 means a terminal is ready to output.(0200(octal) means 10000000(binary))

as you know, busy waiting is heavy for cpu. but it's not serious issue because it is guaranteed that the bit is set in a very short time(?).

  • 2398 : KL->xsr is set as all 0. 6bit means the permission of interrupt. so an interrupt doesn't occur until restoring KL->xsr at 2406 even 7bit is set as 1.
    • this is for case that 7bit is set as 1 by 2406 and then next putchar( ) begins?
  • 2399 : output character is assigned to transmitter data buffer register. this begins the output(display) transmission?
  • 2402 - 2403 : to gain time for handling newline
  • 2405 : waits until output transmission has done?

panic

  • 2422 - 2423 : stop the process with calling idle( ). system clock can work even idle( ) is called.

other notes

  • according to code, perhaps when a code like "printf( '  ', a, b, c )" is executed it just ignore a, b and c, error doesn't occur.
  • i need to read PDP11 Peripherals Handbook and so on to understand the inside of putchar( ).

conclusion

i'll make the notes of chapter 6 next time. unix will begin to work at last!