     1                                  ; ****************************************************************************
     2                                  ; init386.s (init9.s)
     3                                  ; ----------------------------------------------------------------------------
     4                                  ;
     5                                  ; Retro UNIX 386 v1 - /etc/init - process control initialization
     6                                  ;
     7                                  ; [ Last Modification: 07/04/2022 ]
     8                                  ;
     9                                  ; Derived from UNIX Operating System (v1.0 for PDP-11) 
    10                                  ; (Original) Source Code by Ken Thompson (1971-1972)
    11                                  ; <Bell Laboratories (17/3/1972)>
    12                                  ; <Preliminary Release of UNIX Implementation Document> (Section E.12)
    13                                  ; ****************************************************************************
    14                                  ; init4.s (17/11/2015) - Retro UNIX 386 v1 & v1.1
    15                                  ;;init5.s (24/01/2022) - Retro UNIX 386 v1.2
    16                                  ; init6.s (24/01/2022-02/03/2022) - Retro UNIX 386 v1 & v1.1
    17                                  ; init7.s (09/02/2022-19/03/2022) - Retro UNIX 386 v1.2 & v1.1 (& v1.0)
    18                                  ;;init8.s (14/02/2022-20/03/2022) - Retro UNIX 386 v1.2
    19                                  ; init9.s (20/03/2022) - Retro UNIX 386 v1 & v1.1 	
    20                                  
    21                                  ; (Retro UNIX 386 v1 - init386.s, NASM 2.11)
    22                                  ; Derived from INIT09.ASM (17/01/2014, Retro UNIX 8086 v1, MASM 6.11)  
    23                                  ; Derived from 'init.s' file of original UNIX v1
    24                                  ; INIT09.ASM, 17/01/2014 
    25                                  
    26                                  ; 20/03/2022 (init9.s) 
    27                                  ;     ((Same with 'init6.s' source code
    28                                  ;	except 'Serial Port -Terminal- login disabled' message.))
    29                                  ; 02/03/2022
    30                                  ; 25/02/2022
    31                                  ; 24/01/2022 (init6.s)
    32                                  ; 17/11/2015 (init4.s)
    33                                  ; 23/10/2015 (init3.s)
    34                                  ; 14/10/2015 (init2.s)
    35                                  ; 17/09/2015
    36                                  ; 03/09/2015
    37                                  ; 27/08/2015
    38                                  ; 13/08/2015
    39                                  ; 07/08/2015
    40                                  ; 30/06/2015
    41                                  ; 14/07/2013
    42                                  
    43                                  ; UNIX v1 system calls
    44                                  _rele 	equ 0
    45                                  _exit 	equ 1
    46                                  _fork 	equ 2
    47                                  _read 	equ 3
    48                                  _write	equ 4
    49                                  _open	equ 5
    50                                  _close 	equ 6
    51                                  _wait 	equ 7
    52                                  _creat 	equ 8
    53                                  _link 	equ 9
    54                                  _unlink	equ 10
    55                                  _exec	equ 11
    56                                  _chdir	equ 12
    57                                  _time 	equ 13
    58                                  _mkdir 	equ 14
    59                                  _chmod	equ 15
    60                                  _chown	equ 16
    61                                  _break	equ 17
    62                                  _stat	equ 18
    63                                  _seek	equ 19
    64                                  _tell 	equ 20
    65                                  _mount	equ 21
    66                                  _umount	equ 22
    67                                  _setuid	equ 23
    68                                  _getuid	equ 24
    69                                  _stime	equ 25
    70                                  _quit	equ 26	
    71                                  _intr	equ 27
    72                                  _fstat	equ 28
    73                                  _emt 	equ 29
    74                                  _mdate 	equ 30
    75                                  _stty 	equ 31
    76                                  _gtty	equ 32
    77                                  _ilgins	equ 33
    78                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    79                                  _msg    equ 35 ; Retro UNIX 386 v1 feature only !
    80                                  
    81                                  ;;;
    82                                  ESCKey equ 1Bh
    83                                  EnterKey equ 0Dh
    84                                  
    85                                  %macro sys 1-4
    86                                      ; 03/09/2015	
    87                                      ; 13/04/2015
    88                                      ; Retro UNIX 386 v1 system call.		
    89                                      %if %0 >= 2   
    90                                  	mov ebx, %2
    91                                  	%if %0 >= 3    
    92                                  	    mov ecx, %3
    93                                  	    %if %0 = 4
    94                                  	       mov edx, %4   
    95                                  	    %endif
    96                                  	%endif
    97                                      %endif
    98                                      mov eax, %1
    99                                      int 30h	   
   100                                  %endmacro
   101                                  
   102                                  ; Retro UNIX 386 v1 system call format:
   103                                  ; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
   104                                  
   105                                  [BITS 32] ; We need 32-bit intructions for protected mode
   106                                  
   107                                  [ORG 0] 
   108                                  
   109                                  START_CODE:
   110                                  	sys	_intr, 0  ; disable time-out function 
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000000 BB00000000          <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000005 B81B000000          <1>  mov eax, %1
    99 0000000A CD30                <1>  int 30h
   111                                  	sys	_quit, 0  ; disable quit (ctrl+brk) signal
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 0000000C BB00000000          <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000011 B81A000000          <1>  mov eax, %1
    99 00000016 CD30                <1>  int 30h
   112                                  
   113                                  	sys	_open, ctty, 0 ; open tty0
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000018 BB[3E030000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 0000001D B900000000          <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000022 B805000000          <1>  mov eax, %1
    99 00000027 CD30                <1>  int 30h
   114 00000029 7259                    	jc	short error
   115                                  
   116                                  	sys	_open, ctty, 1 ; for read and write
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 0000002B BB[3E030000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 00000030 B901000000          <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000035 B805000000          <1>  mov eax, %1
    99 0000003A CD30                <1>  int 30h
   117 0000003C 7246                    	jc	short error
   118                                  
   119                                  	sys	_write, 1, msg_te, sizeof_mte
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 0000003E BB01000000          <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 00000043 B9[C6030000]        <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94 00000048 BA55000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000004D B804000000          <1>  mov eax, %1
    99 00000052 CD30                <1>  int 30h
   120 00000054 722E                    	jc	short error
   121                                  i0:
   122                                  	sys	_read, 0, tchar, 1
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000056 BB00000000          <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 0000005B B9[3C030000]        <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94 00000060 BA01000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000065 B803000000          <1>  mov eax, %1
    99 0000006A CD30                <1>  int 30h
   123 0000006C 7216                    	jc	short error
   124                                  
   125                                  	;sys	_close, 0 ; close input file/tty
   126                                  	;jc	short error
   127                                  	;sys	_close, 1 ; close output file/tty
   128                                  	;jc	short error
   129                                  
   130 0000006E A0[3C030000]            	mov	al, [tchar]
   131                                  
   132                                  	; 25/02/2022
   133                                  	;cmp	al, EnterKey
   134                                  	;je	short multiuser
   135                                  
   136 00000073 3C1B                    	cmp	al, ESCKey
   137 00000075 7433                    	je	short singleuser
   138                                  
   139                                  	;jmp	short i0
   140                                  
   141                                  	; 25/02/2022
   142                                  	; (ALT+127 will ensure multiuser login without tty8 and tty9)
   143                                  	;cmp	al, 7Fh ; ALT+127
   144                                  	;je	short multiuser_x
   145                                  
   146                                  	; 25/02/2022
   147 00000077 3C0D                    	cmp	al, EnterKey
   148 00000079 747C                    	je	short multiuser
   149                                  
   150                                  	;jmp	short i0
   151                                  
   152                                  	; 07/04/2022
   153                                  	; 25/02/2022
   154 0000007B 3C7F                    	cmp	al, 7Fh ; ALT+127
   155 0000007D 75D7                    	jne	short i0
   156 0000007F E9AC000000              	jmp	multiuser_x
   157                                  
   158                                  error:
   159                                  	sys	_msg, error_msg, 255, 0Ch 
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000084 BB[3B040000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 00000089 B9FF000000          <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94 0000008E BA0C000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000093 B823000000          <1>  mov eax, %1
    99 00000098 CD30                <1>  int 30h
   160                                  		; error message with red color (max. 255 chars)
   161                                  exit:
   162                                  	sys	_exit
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90                              <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000009A B801000000          <1>  mov eax, %1
    99 0000009F CD30                <1>  int 30h
   163                                  
   164                                  haltsys:
   165                                  	;hlt
   166                                  	; 17/09/2015
   167 000000A1 90                      	nop
   168 000000A2 90                      	nop
   169 000000A3 90                      	nop
   170 000000A4 90                      	nop
   171 000000A5 90                      	nop
   172 000000A6 90                      	nop
   173 000000A7 90                      	nop
   174 000000A8 EBF7                    	jmp	short haltsys
   175                                  
   176                                  singleuser:
   177                                  i1:
   178                                  help:
   179                                  	sys	_close, 0 ; close input file/tty
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 000000AA BB00000000          <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000000AF B806000000          <1>  mov eax, %1
    99 000000B4 CD30                <1>  int 30h
   180                                  	;jc	short error
   181                                  	sys	_close, 1 ; close output file/tty
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 000000B6 BB01000000          <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000000BB B806000000          <1>  mov eax, %1
    99 000000C0 CD30                <1>  int 30h
   182                                  	;jc	short error
   183                                  	sys	_open, ctty, 0 ; open control tty
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 000000C2 BB[3E030000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 000000C7 B900000000          <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000000CC B805000000          <1>  mov eax, %1
    99 000000D1 CD30                <1>  int 30h
   184                                  	;jc	short error
   185                                  	sys	_open, ctty, 1 ; for read an write
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 000000D3 BB[3E030000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 000000D8 B901000000          <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000000DD B805000000          <1>  mov eax, %1
    99 000000E2 CD30                <1>  int 30h
   186                                  	;jc	short error
   187                                  	;
   188                                  	sys	_exec, shell, shellp
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 000000E4 BB[48030000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 000000E9 B9[8C030000]        <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000000EE B80B000000          <1>  mov eax, %1
    99 000000F3 CD30                <1>  int 30h
   189                                  	;
   190 000000F5 EBB3                     	jmp	short i1
   191                                  
   192                                  multiuser:
   193                                  	; 07/04/2022
   194 000000F7 BF[BC030000]            	mov	edi, itabs
   195 000000FC 31DB                    	xor	ebx, ebx
   196 000000FE B901090000              	mov	ecx, 0901h ; return tty8 serial port status
   197                                  	sys	_gtty
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90                              <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000103 B820000000          <1>  mov eax, %1
    99 00000108 CD30                <1>  int 30h
   198 0000010A 7206                    	jc	short multiuser_e ; device not responding !
   199 0000010C C60738                  	mov	byte [edi], '8'
   200 0000010F 83C704                  	add	edi, 4
   201                                  multiuser_e:
   202 00000112 29DB                    	sub	ebx, ebx
   203 00000114 B9010A0000              	mov	ecx, 0A01h ; return tty9 serial port status
   204                                  	sys	_gtty
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90                              <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000119 B820000000          <1>  mov eax, %1
    99 0000011E CD30                <1>  int 30h
   205 00000120 7205                    	jc	short multiuser_s ; device not responding !
   206 00000122 C60739                  	mov	byte [edi], '9'	
   207 00000125 EB1F                    	jmp	short multiuser_t
   208                                  
   209                                  multiuser_s:
   210 00000127 803D[BC030000]00        	cmp	byte [itabs], 0
   211 0000012E 7716                    	ja	short  multiuser_t
   212                                  
   213                                  	; 07/04/2022
   214                                  multiuser_x:	; 25/02/2022
   215                                  	;mov	byte [itabs], 0 ; (disable serial port login)
   216                                  	; 20/03/2022
   217                                  	sys	_write, 1, td_msg, td_msg_size
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000130 BB01000000          <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 00000135 B9[68040000]        <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94 0000013A BA3D000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000013F B804000000          <1>  mov eax, %1
    99 00000144 CD30                <1>  int 30h
   218                                  
   219                                  ;multi_user:
   220                                  multiuser_t:	; 07/04/2022
   221                                  	sys	_close, 0 ; close input file/tty
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000146 BB00000000          <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000014B B806000000          <1>  mov eax, %1
    99 00000150 CD30                <1>  int 30h
   222                                  	;jc	short error
   223                                  	sys	_close, 1 ; close output file/tty
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000152 BB01000000          <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000157 B806000000          <1>  mov eax, %1
    99 0000015C CD30                <1>  int 30h
   224                                  	;jc	short error
   225                                  	;
   226                                  	sys	_mount, fd1, usr ; root directory on mounted fd1
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 0000015E BB[58030000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 00000163 B9[52030000]        <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000168 B815000000          <1>  mov eax, %1
    99 0000016D CD30                <1>  int 30h
   227                                  				 ; disk is /usr
   228                                  
   229                                  	sys	_creat, utmp, 14 ; truncate /tmp/utmp
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 0000016F BB[62030000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 00000174 B90E000000          <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000179 B808000000          <1>  mov eax, %1
    99 0000017E CD30                <1>  int 30h
   230                                  	;jc	short error
   231                                  
   232                                  	sys	_close, eax	; close it
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000180 89C3                <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000182 B806000000          <1>  mov eax, %1
    99 00000187 CD30                <1>  int 30h
   233                                  
   234                                  	; 24/01/2022
   235 00000189 C605[AE040000]78        	mov	byte [zero+8], 'x'
   236                                  	;mov	byte [zero+8], 0 ; put identifier
   237                                  				 ; in output buffer
   238                                  
   239 00000190 E846010000              	call	wtmprec		; go to write acting info
   240                                  
   241                                  	; 23/10/2015
   242                                  	;
   243                                  	;; 10/12/2013
   244                                  	;; 'Enable Multi Tasking' (Time-Out)
   245                                  	;; system call (Retro UNIX 8086 v1 feature only !)
   246                                  	sys	_emt, 1
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000195 BB01000000          <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000019A B81D000000          <1>  mov eax, %1
    99 0000019F CD30                <1>  int 30h
   247                                  	;;
   248                                  
   249 000001A1 BE[9C030000]            	mov	esi, itab	; address of table to eSI
   250                                  
   251                                  ; create shell processes
   252                                  i2:
   253 000001A6 66AD                    	lodsw			; 'x', x=0, 1... to AX
   254 000001A8 6621C0                  	and	ax, ax
   255 000001AB 7412                    	jz	short pwait	; branch if table end
   256                                  	
   257 000001AD A2[7E030000]            	mov	[ttyx+8], al	; put symbol in ttyx
   258                                  	; 02/03/2022
   259                                  	;mov	ecx, eax ; 18/02/2022
   260 000001B2 89F7                    	mov	edi, esi	
   261 000001B4 E8A1000000              	call	dfork        ; go to make new init for this ttyx
   262                                  	;mov	eax, ecx ; 18/02/2022
   263 000001B9 66AB                    	stosw		     ; save child id in word offer
   264                                  	;		     ;	'0', '1',...etc.
   265 000001BB 89FE                    	mov	esi, edi
   266 000001BD EBE7                    	jmp	short i2     ; set up next child
   267                                  
   268                                  ; wait for process to die
   269                                  pwait:
   270                                  	;sys	_write, 1, beep, 1 ; 10/12/2013
   271                                  	;
   272                                  	sys	_wait       ; wait for user to terminate process
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90                              <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000001BF B807000000          <1>  mov eax, %1
    99 000001C4 CD30                <1>  int 30h
   273                                  	; 02/03/2022
   274 000001C6 7305                    	jnc	short i8    ; (eax = process ID of the child)
   275                                  	; ERROR ! there is not a child process to wait ! 
   276                                  	; (This may be only viable option for now!) - 02/03/2022 - 
   277 000001C8 E9B7FEFFFF              	jmp	error	; (print error message then terminate) 	
   278                                  i8:
   279 000001CD BE[9C030000]            	mov	esi, itab   ; initialize for search
   280                                  	; 02/03/2022 
   281                                  	;mov	dx, ax
   282                                  	; 24/01/2022
   283 000001D2 89C2                    	mov	edx, eax    ; (process ID of the child)
   284                                  
   285                                  ; search for process id
   286                                  i3:
   287 000001D4 66AD                    	lodsw		; bump eSI to child id location
   288 000001D6 6609C0                  	or	ax, ax
   289 000001D9 74E4                    	jz	short pwait ; ? something silly
   290                                  
   291 000001DB 66AD                    	lodsw
   292                                  	; 24/01/2022
   293 000001DD 39C2                    	cmp	edx, eax
   294                                  	;cmp	dx, ax      ; which process has terminated
   295 000001DF 75F3                    	jne	short i3    ; not this one
   296                                  
   297                                  ; take name out of utmp
   298                                  	;mov	ecx, 4
   299                                  	; 24/01/2022
   300 000001E1 31C9                    	xor 	ecx, ecx
   301 000001E3 B104                    	mov	cl, 4
   302                                  	;sub	esi, 4	; process is found, point x to 'x'
   303                                  			; for it
   304 000001E5 29CE                    	sub	esi, ecx  ; 4
   305                                  	;push	esi	; save address on stack
   306 000001E7 668B06                  	mov	ax, [esi] ; move 'x' to AX
   307                                  	; 24/01/2022
   308 000001EA 2C30                    	sub	al, '0'
   309                                  	;sub	ax, '0'   ; remove zone bits from character
   310                                  	;;shl	ax, 4	  ; generate proper offset for seek
   311 000001EC 66D3E0                  	shl	ax, cl	  ; 4
   312                                  	;movzx	edx, ax	
   313                                  	; 24/01/2022
   314 000001EF 89C2                    	mov	edx, eax
   315 000001F1 BF[A6040000]            	mov	edi, zero
   316 000001F6 31C0                    	xor	eax, eax  ; 0 ; clear	
   317                                  	;mov	ecx, 4	  ; output buffer
   318 000001F8 F3AB                    	rep	stosd
   319                                  	sys	_open, utmp, 1 ; open file for writing
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 000001FA BB[62030000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 000001FF B901000000          <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000204 B805000000          <1>  mov eax, %1
    99 00000209 CD30                <1>  int 30h
   320 0000020B 722E                    	jc	short i4  ; if can't open, create user anyway
   321                                  	;movzx	edi, ax	  ; save file desc
   322                                  	; 24/01/2022
   323 0000020D 89C7                    	mov	edi, eax  ; save file descriptor
   324                                  	sys	_seek, eax, edx, 0 
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 0000020F 89C3                <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 00000211 89D1                <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94 00000213 BA00000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000218 B813000000          <1>  mov eax, %1
    99 0000021D CD30                <1>  int 30h
   325                                  			  ; move to proper pointer position
   326                                  	sys	_write, edi, zero, 16 
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 0000021F 89FB                <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 00000221 B9[A6040000]        <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94 00000226 BA10000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000022B B804000000          <1>  mov eax, %1
    99 00000230 CD30                <1>  int 30h
   327                                  			  ; zero this position in
   328                                  	sys	_close, edi
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000232 89FB                <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000234 B806000000          <1>  mov eax, %1
    99 00000239 CD30                <1>  int 30h
   329                                  			  ; close file
   330                                  
   331                                  ; re-create user process
   332                                  i4:
   333                                  	;pop	esi	; restore 'x' to eSI
   334 0000023B 66AD                    	lodsw		; move it to AX
   335 0000023D 89F7                    	mov	edi, esi 
   336 0000023F A2[7E030000]            	mov	[ttyx+8], al ; get correct ttyx
   337 00000244 A2[AE040000]            	mov	[zero+8], al
   338                                  	 		; move identifier to output buffer
   339 00000249 E88D000000              	call	wtmprec	; go to write accting into
   340 0000024E E807000000              	call	dfork 	; fork
   341 00000253 66AB                    	stosw		; save id of child
   342                                  	; 02/03/2022 
   343                                  	;; 25/02/2022
   344                                  	;mov	edx, eax  ; save id of child
   345                                  	;	
   346 00000255 E965FFFFFF              	jmp	pwait	  ; go to wait for next process end
   347                                  
   348                                  dfork:
   349 0000025A BB[69020000]            	mov	ebx, i5 ; return address for new process
   350                                  	sys	_fork
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90                              <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000025F B802000000          <1>  mov eax, %1
    99 00000264 CD30                <1>  int 30h
   351 00000266 72F2                    	jc	short dfork ; try again
   352 00000268 C3                      	retn
   353                                  
   354                                  i5: ; to new copy of init
   355                                  	;sys	_quit, 0  ; disable quit (ctrl+brk) signal
   356                                  	;sys	_intr, 0  ; disable time-out function 
   357                                  	;sys	_chown, ttyx, 0
   358                                  	;sys	_chmod; ttyx, 15
   359                                  o0:	
   360 00000269 31DB                    	xor	ebx, ebx
   361                                  	;xor	ch, ch
   362                                  	;mov	cl, [ttyx+8]
   363 0000026B 0FB60D[7E030000]        	movzx	ecx, byte [ttyx+8]
   364 00000272 80E930                  	sub	cl, '0'
   365                                  	; 17/01/2014
   366                                  	; 07/12/2013
   367                                  	; set console tty for current process
   368                                  	;mov	dx, 0FF00h
   369                                  	;mov	dh, 0FFh ; do not set cursor position
   370                                  			 ; do not set serial port parameters	
   371                                  	;mov	edx, 0FF00h
   372                                  	; 18/02/2022
   373 00000275 29D2                    	sub	edx, edx
   374 00000277 FECE                    	dec	dh
   375                                  	; edx = 0FF00h	
   376                                  	sys	_stty
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90                              <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000279 B81F000000          <1>  mov eax, %1
    99 0000027E CD30                <1>  int 30h
   377 00000280 724D                    	jc	short terminate
   378                                  o1:
   379                                  	; 16/11/2015	
   380                                  	sys	_open, ttyx, 0 ; open this ttyx for reading
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000282 BB[76030000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 00000287 B900000000          <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000028C B805000000          <1>  mov eax, %1
    99 00000291 CD30                <1>  int 30h
   381                                  	;jnc	short o2
   382                                  	;call	wait_for_terminal
   383                                  	;jmp	short o1
   384                                  	; 17/11/2015
   385 00000293 723A                    	jc	short terminate	
   386                                  	; 18/02/2022
   387                                  	;jc	short help1
   388                                  o2:
   389                                  	; 16/11/2015
   390                                  	sys	_open, ttyx, 1 ; open this ttyx for writing
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000295 BB[76030000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 0000029A B901000000          <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000029F B805000000          <1>  mov eax, %1
    99 000002A4 CD30                <1>  int 30h
   391                                  	;jnc	short o3
   392                                  	;call	wait_for_terminal
   393                                  	;jmp	short o2
   394                                  	; 17/11/2015
   395 000002A6 7227                    	jc	short terminate	
   396                                  	; 18/02/2022
   397                                  	;jc	short help1		
   398                                  o3:	
   399                                  	sys	_exec, getty, gettyp ; getty types <login> and
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 000002A8 BB[80030000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 000002AD B9[94030000]        <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000002B2 B80B000000          <1>  mov eax, %1
    99 000002B7 CD30                <1>  int 30h
   400                                  			   	 ; executes login which logs user
   401                                  				 ; in and executes sh-
   402                                  	; 14/10/2015
   403                                  	sys	_msg, getty_error_msg, 255, 0Ch ; error msg with red color
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 000002B9 BB[51040000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 000002BE B9FF000000          <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94 000002C3 BA0C000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000002C8 B823000000          <1>  mov eax, %1
    99 000002CD CD30                <1>  int 30h
   404                                  
   405                                  terminate:
   406                                  	sys	_exit  ; HELP!
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90                              <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000002CF B801000000          <1>  mov eax, %1
    99 000002D4 CD30                <1>  int 30h
   407                                  ;help1:
   408 000002D6 E9CFFDFFFF              	jmp	help
   409                                  
   410                                  ;wait_for_terminal:
   411                                  ;	; 16/11/2015
   412                                  ;	; 24/10/2015
   413                                  ;	mov	al, [ttyx+8]
   414                                  ;	sub	al, '0'
   415                                  ;	cmp	al, 8
   416                                  ;	jb	short sysexit
   417                                  ;	sys	_sleep
   418                                  ;	retn
   419                                  ;sysexit:
   420                                  ;	pop	eax ;return address	
   421                                  ;	jmp	short terminate
   422                                  
   423                                  wtmprec:
   424                                  	; 18/02/2022
   425                                  	; 24/01/2022
   426                                  	; 23/10/2015 (wtmp_err)
   427                                  	;cmp	byte [wtmp_err], 0
   428                                  	;ja	short i6 	
   429                                  
   430                                  	sys	_time  ; get time
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90                              <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000002DB B80D000000          <1>  mov eax, %1
    99 000002E0 CD30                <1>  int 30h
   431 000002E2 A3[B0040000]            	mov	[zero+10], eax ; move to output buffer
   432                                  
   433 000002E7 803D[B6040000]00        	cmp	byte [wtmp_err], 0
   434 000002EE 7744                    	ja	short i6 ; 18/02/2022
   435                                  
   436                                  	sys	_open, wtmp, 1 ; open accounting file
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 000002F0 BB[6C030000]        <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 000002F5 B901000000          <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 000002FA B805000000          <1>  mov eax, %1
    99 000002FF CD30                <1>  int 30h
   437 00000301 7232                    	jc	short i7 ; 18/02/2022
   438                                  	; 24/01/2022
   439                                  	;jc	short i6
   440                                  
   441 00000303 89C6                    	mov	esi, eax ; save file descriptor
   442                                  
   443                                  	sys	_seek, eax, 0, 2 ; move pointer to end of file
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000305 89C3                <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 00000307 B900000000          <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94 0000030C BA02000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000311 B813000000          <1>  mov eax, %1
    99 00000316 CD30                <1>  int 30h
   444                                  	;;push	esi	 ; save file descriptor
   445                                  	;jc	short i7
   446                                  
   447                                  	sys	_write, esi, zero, 16 ; write accting info
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 00000318 89F3                <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92 0000031A B9[A6040000]        <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94 0000031F BA10000000          <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 00000324 B804000000          <1>  mov eax, %1
    99 00000329 CD30                <1>  int 30h
   448                                  	;;pop	ebx	 ; restore file descriptor
   449                                  	;jc	short i7
   450                                  
   451                                  	sys	_close, esi ; close file
    86                              <1> 
    87                              <1> 
    88                              <1> 
    89                              <1>  %if %0 >= 2
    90 0000032B 89F3                <1>  mov ebx, %2
    91                              <1>  %if %0 >= 3
    92                              <1>  mov ecx, %3
    93                              <1>  %if %0 = 4
    94                              <1>  mov edx, %4
    95                              <1>  %endif
    96                              <1>  %endif
    97                              <1>  %endif
    98 0000032D B806000000          <1>  mov eax, %1
    99 00000332 CD30                <1>  int 30h
   452                                  i6:
   453 00000334 C3                      	retn
   454                                  
   455                                  	; 18/02/2022
   456                                  	; 24/01/2022
   457                                  i7:
   458 00000335 FE05[B6040000]          	inc	byte [wtmp_err] ; 23/10/2015
   459 0000033B C3                      	retn
   460                                  
   461                                  	; 24/01/2022
   462                                  ;here:
   463                                  	;hlt	; General Protection Fault ! 17/09/2015
   464                                  	;;jmp	short here
   465                                  	;jmp	haltsys	
   466                                  
   467                                  align 2
   468 0000033C 00                      tchar:  db 0
   469 0000033D 00                      	db 0 ; 25/02/2022
   470                                  ;align 2
   471 0000033E 2F6465762F74747900      ctty:   db "/dev/tty", 0
   472 00000347 90                      align 2
   473 00000348 2F62696E2F736800        shell:  db "/bin/sh", 0
   474 00000350 2D00                    shellm: db "-", 0
   475                                  ;align 2
   476 00000352 2F75737200              usr:	db "/usr",0
   477 00000357 90                      align 2
   478 00000358 2F6465762F66643100      fd1:	db "/dev/fd1", 0
   479                                  ;
   480 00000361 90                      align 2
   481 00000362 2F746D702F75746D70-     utmp:   db "/tmp/utmp", 0
   481 0000036B 00                 
   482 0000036C 2F746D702F77746D70-     wtmp:   db "/tmp/wtmp", 0
   482 00000375 00                 
   483 00000376 2F6465762F74747978-     ttyx:   db "/dev/ttyx", 0
   483 0000037F 00                 
   484 00000380 2F6574632F67657474-     getty:  db "/etc/getty",0
   484 00000389 7900               
   485                                  
   486 0000038B 90                      align 2
   487                                  ; 27/08/2015 (dw -> dd)
   488 0000038C [50030000]              shellp: dd shellm
   489 00000390 00000000                	dd 0
   490 00000394 [80030000]              gettyp: dd getty
   491 00000398 00000000                	dd 0
   492                                  itab:
   493 0000039C 30000000                	db '0',0, 0,0
   494 000003A0 31000000                	db '1',0, 0,0
   495 000003A4 32000000                	db '2',0, 0,0
   496 000003A8 33000000                	db '3',0, 0,0
   497 000003AC 34000000                	db '4',0, 0,0
   498 000003B0 35000000                	db '5',0, 0,0
   499 000003B4 36000000                	db '6',0, 0,0
   500 000003B8 37000000                	db '7',0, 0,0
   501                                  ;itabs:		; 25/02/2022
   502                                  	; serial ports (COM1, COM2)
   503                                  	;db '8',0, 0,0
   504                                  	;db '9',0, 0,0
   505                                  	;dw 0
   506                                  itabs:
   507                                  	; 07/04/2022
   508 000003BC 00000000                	dd 0
   509 000003C0 00000000                	dd 0
   510 000003C4 0000                    	dw 0
   511                                  
   512                                  ;-----------------------------------------------------------------
   513                                  ;  messages
   514                                  ;-----------------------------------------------------------------
   515                                  
   516                                  msg_te:
   517 000003C6 0D0A                    	db 0Dh, 0Ah
   518 000003C8 5479706520454E5445-     	db 'Type ENTER to start in multi user mode', 0Dh, 0Ah
   518 000003D1 5220746F2073746172-
   518 000003DA 7420696E206D756C74-
   518 000003E3 692075736572206D6F-
   518 000003EC 64650D0A           
   519 000003F0 6F7220747970652045-     	db 'or type ESC to start in single user mode.'
   519 000003F9 534320746F20737461-
   519 00000402 727420696E2073696E-
   519 0000040B 676C65207573657220-
   519 00000414 6D6F64652E         
   520 00000419 0D0A                    	db 0Dh, 0Ah
   521                                  sizeof_mte equ $ - msg_te 
   522 0000041B 00                      	db 0
   523                                  copy_right_msg:
   524 0000041C 286329204572646F67-     	db  '(c) Erdogan TAN - 07/04/2022'
   524 00000425 616E2054414E202D20-
   524 0000042E 30372F30342F323032-
   524 00000437 32                 
   525 00000438 0D0A00                  	db  0Dh, 0Ah, 0
   526                                  error_msg:
   527 0000043B 0D0A07                  	db 0Dh, 0Ah, 07h
   528 0000043E 2F6574632F696E6974-     	db '/etc/init error ! '
   528 00000447 206572726F72202120 
   529 00000450 00                      	db 0
   530                                  
   531                                  getty_error_msg:
   532                                  	; 14/10/2015
   533 00000451 0D0A07                  	db 0Dh, 0Ah, 07h
   534 00000454 2F6574632F67657474-     	db '/etc/getty error ! '
   534 0000045D 79206572726F722021-
   534 00000466 20                 
   535 00000467 00                      	db 0
   536                                  
   537                                  	; 20/03/2022
   538 00000468 0D0A                    td_msg:	db 0Dh, 0Ah
   539 0000046A 53657269616C20506F-     	db 'Serial Port (Terminal) login feature has been disabled..'
   539 00000473 727420285465726D69-
   539 0000047C 6E616C29206C6F6769-
   539 00000485 6E2066656174757265-
   539 0000048E 20686173206265656E-
   539 00000497 2064697361626C6564-
   539 000004A0 2E2E               
   540 000004A2 200D0A00                	db 20h, 0Dh, 0Ah, 0
   541                                  
   542                                  td_msg_size equ ($ - td_msg) - 1
   543                                  
   544                                  ; 23/10/2015
   545                                  align 2
   546                                  zero:
   547 000004A6 00<rep 8h>              	times 8 db 0
   548 000004AE 00<rep 6h>              	times 6 db 0
   549 000004B4 00<rep 2h>              	times 2 db 0	 
   550                                  
   551                                  wtmp_err:  ; 18/02/2022	
   552 000004B6 00                      	db 0
   553                                  	
   554                                  ; 24/01/2022
   555                                  	
   556                                  ;-----------------------------------------------------------------
   557                                  ; Original UNIX v1 - /etc/init source code (init.s)
   558                                  ;		     in PDP-11 (unix) assembly language
   559                                  ;-----------------------------------------------------------------
   560                                  
   561                                  ;/ init -- process control initialization
   562                                  ;
   563                                  ;mount = 21.
   564                                  ;
   565                                  ;	sys	intr; 0 / turn off interrupts
   566                                  ;	sys 	quit; 0
   567                                  ;	cmp	csw,$73700 / single user?
   568                                  ;	bne	1f / no
   569                                  ;help:
   570                                  ;	clr	r0 / yes
   571                                  ;	sys	close / close current read
   572                                  ;	mov	$1,r0 / and write
   573                                  ;	sys	close / files
   574                                  ;	sys	open; ctty; 0 / open control tty
   575                                  ;	sys	open; ctty; 1 / for read and write
   576                                  ;	sys	exec; shell; shellp / execute shell
   577                                  ;	br	help / keep trying
   578                                  ;1:
   579                                  ;	mov	$'0,r1 / prepare to change
   580                                  ;1 :
   581                                  ;	movb	r1,tapx+8 / mode of dec tape drive x, where
   582                                  ;	sys	chmod; tapx; 17 / x=0 to 7, to read/write by owner or
   583                                  ;	inc	r1 / non-owner mode
   584                                  ;	cmp	r1,$'8 / finished?
   585                                  ;	blo	1b / no
   586                                  ;	sys	mount; rk0; usr / yes, root file on mounted rko5
   587                                  ;				/ disk ls /usr
   588                                  ;	sys	creat; utmp; 16 / truncate /tmp/utmp
   589                                  ;	sys	close / close it
   590                                  ;	movb	$'x,zero+8. / put identifier in output buffer
   591                                  ;	jsr	pc,wtmprec / go to write accting info
   592                                  ;	mov	$itab,r1 / address of table to r1
   593                                  ;
   594                                  ;/ create shell processes
   595                                  ;
   596                                  ;1:
   597                                  ;	mov	(r1)+,r0 / 'x, x=0, 1... to r0
   598                                  ;	beq	1f / branch if table end
   599                                  ;	movb	r0,ttyx+8 / put symbol in ttyx
   600                                  ;	jsr	pc,dfork / go to make new init for this ttyx
   601                                  ;	mov	r0,(r1)+ / save child id in word offer '0, '1,...etc.
   602                                  ;	br	1b / set up next child
   603                                  ;
   604                                  ;/ wait for process to die
   605                                  ;
   606                                  ;1:
   607                                  ;	sys	wait / wait for user to terminate process
   608                                  ;	mov	$itab,r1 / initialize for search
   609                                  ;
   610                                  ;/ search for process id
   611                                  ;
   612                                  ;2:
   613                                  ;	tst	(r1)+ / bump r1 to child id location
   614                                  ;	beq	1b / ? something silly
   615                                  ;	cmp	r0,(r1)+ / which process has terminated
   616                                  ;	bne	2b / not this one
   617                                  ;
   618                                  ;/ take name out of utmp
   619                                  ;
   620                                  ;	sub	$4, r1 / process is found, point x' to 'x
   621                                  ;		       / for it
   622                                  ;	mov	r1,-(sp) / save address on stack
   623                                  ;	mov	(r1),r1 / move 'x to r1
   624                                  ;	sub	$'0,r1 / remove zone bits from character
   625                                  ;	asl	r1 / generate proper
   626                                  ;	asl	r1 / offset
   627                                  ;	asl	r1 / for
   628                                  ;	asl	r1 / seek
   629                                  ;	mov	r1,0f / move it to offset loc for seek
   630                                  ;	mov	$zero,r1
   631                                  ;2:
   632                                  ;	clr	(r1)+ / ccear-
   633                                  ;	cmp	r1,$zero+16. / output buffer
   634                                  ;	blo	2b / area
   635                                  ;	sys	open; utmp; 1 / open file for writing
   636                                  ;	bes	2f / if can't open, create user anyway
   637                                  ;	mov	r0,r1 / save file desc
   638                                  ;	sys	seek; 0:..; 0 / move to proper pointer position
   639                                  ;	mov	r1,r0 / not required
   640                                  ;	sys	write; zero; 16. / zero this position in
   641                                  ;	mov	r1,r0 / restore file descriptor
   642                                  ;	sys	close / close file
   643                                  ;
   644                                  ;/ re-create user process
   645                                  ;
   646                                  ;2:
   647                                  ;	mov	(sp)+,r1 / restore 'x to r1
   648                                  ;	mov	(r1)+,r0 / move it to r0
   649                                  ;	movb	r0,ttyx+8 / get correct ttyx
   650                                  ;	movb	r0,zero+8 / move identifier to output buffer
   651                                  ;	jsr	pc,wtmprec / go to write accting into
   652                                  ;	jsr	pc,dfork / fork
   653                                  ;	mov	r0,(r1)+ / save id of child
   654                                  ;	br	1b / go to wait for next process end
   655                                  ;
   656                                  ;dfork:
   657                                  ;	mov	r1,r2
   658                                  ;	sub	$itab+2,r2 / left over
   659                                  ;	asl	r2 / from previous
   660                                  ;	asl	r2 / version of code
   661                                  ;	mov	r2,offset
   662                                  ;	sys	fork
   663                                  ;		br 1f / to new copy of init
   664                                  ;	bes	dfork / try again
   665                                  ;	rts	pc / return
   666                                  ;1 :
   667                                  ;	sys	quit; 0 / new init turns off
   668                                  ;	sys	intr; 0 / interrupts
   669                                  ;	sys	chown; ttyx; 0 / change owner to super user
   670                                  ;	sys	chmod; ttyx; 15 / changemode to read/write owner,
   671                                  ;				/ write non-owner
   672                                  ;	sys	open; ttyx; 0 / open this ttyx for reading
   673                                  ;			      / and wait until someone calls
   674                                  ;	bes	help1 / branch if trouble
   675                                  ;	sys	open; ttyx; 1 / open this ttyx for writing after
   676                                  ;			      / user call
   677                                  ;	bes	help1 / branch if trouble
   678                                  ;	sys	exec; getty; gettyp / getty types <login> and
   679                                  ;				    / executes login which logs user
   680                                  ;				    / in and executes sh-
   681                                  ;	sys	exit / HELP!
   682                                  ;
   683                                  ;help1:
   684                                  ;	jmp	help / trouble
   685                                  ;
   686                                  ;wtmprec:
   687                                  ;	sys	time / get time
   688                                  ;	mov	ac,zero+10. / more to output
   689                                  ;	mov	mq,zero+12. / buffer
   690                                  ;	sys	open; wtmp; 1 / open accounting file
   691                                  ;	bes	2f
   692                                  ;	mov	r0,r2 / save file descriptor
   693                                  ;	sys	seek; 0; 2 / move pointer to end of file
   694                                  ;	mov	r2,r0 / not required
   695                                  ;	sys	write; zero; 16. / write accting info
   696                                  ;	mov	r2,r0 / restore file descriptor
   697                                  ;	sys	close / close file
   698                                  ;2:
   699                                  ;	rts	pc
   700                                  ;
   701                                  ;ctty:	</dev/tty\0>
   702                                  ;shell:	</bin/sh\0>
   703                                  ;shellm: <-\0>
   704                                  ;tapx:	</dev/tapx\0>
   705                                  ;rk0:	</dev/rk0\0>
   706                                  ;utmp:	</tmp/utmp\0>
   707                                  ;wtmp:	</tmp/wtmp\0>
   708                                  ;ttyx:	</dev/ttyx\0>
   709                                  ;getty:	</etc/getty\0>
   710                                  ;usr:	</usr\0>
   711                                  ;	.even
   712                                  ;
   713                                  ;shellp: shellm
   714                                  ;	0
   715                                  ;gettyp: getty
   716                                  ;	0
   717                                  ;itab:
   718                                  ;	'0; ..
   719                                  ;	'1; ..
   720                                  ;	'2; ..
   721                                  ;	'3; ..
   722                                  ;	'4; ..
   723                                  ;	'5; ..
   724                                  ;	'6; ..
   725                                  ;	'7; ..
   726                                  ;	0
   727                                  ;
   728                                  ;offset: .=.+2
   729                                  ;zero:	 .=.+8; .=.+6; .=.+2
   730                                  
   731                                  ; 23/01/2022
   732                                  
   733                                  ;-----------------------------------------------------------------
   734                                  ; Original UNIX v2 - /etc/init source code (init.s)
   735                                  ;		     in PDP-11 (unix) assembly language
   736                                  ;-----------------------------------------------------------------
   737                                  
   738                                  ; / init -- process control initialization
   739                                  ;
   740                                  ;	sys	intr; 0
   741                                  ;	sys	quit; 0
   742                                  ;	sys	38. / get console switches
   743                                  ;	cmp	r0,$173030
   744                                  ;	bne	1f
   745                                  ;help:
   746                                  ;	clr	r0
   747                                  ;	sys	close
   748                                  ;	mov	$1,r0
   749                                  ;	sys	close
   750                                  ;	sys	open; ctty; 0
   751                                  ;	sys	open; ctty; 1
   752                                  ;	sys	exec; shell; shellp
   753                                  ;	br	help
   754                                  ;1:
   755                                  ;	sys	mount; rk1; usr
   756                                  ;	sys	mount; rk2; ssys
   757                                  ;	sys	mount; rk3; crp
   758                                  ;	mov	$'0,r1
   759                                  ;1:
   760                                  ;	movb	r1,tapx+8
   761                                  ;	sys	chmod; tapx; 17
   762                                  ;	inc	r1
   763                                  ;	cmp	r1,$'8
   764                                  ;	blo	1b
   765                                  ;	sys	creat; utmp; 16
   766                                  ;	sys	close
   767                                  ;	sys	unlink; dpdlock
   768                                  ;	sys	fork
   769                                  ;		br daemon
   770                                  ;	sys	fork
   771                                  ;		br dirass
   772                                  ;	sys	fork
   773                                  ;		br dds
   774                                  ;	movb	$'x,zero+8.
   775                                  ;	jsr	pc,wtmprec
   776                                  ;	mov	$itab,r1
   777                                  ;	br	1f
   778                                  ;
   779                                  ;daemon:
   780                                  ;	sys	exec; etcdpd; etcdpdp
   781                                  ;	sys	exit
   782                                  ;
   783                                  ;dirass:
   784                                  ;	sys	chdir; usrmel
   785                                  ;	sys	exec; melda; meldap
   786                                  ;	sys	exit
   787                                  ;
   788                                  ;dds:
   789                                  ;	sys	exec; usrdd; usrddp
   790                                  ;	sys	exit
   791                                  ;
   792                                  ;/ create shell processes
   793                                  ;
   794                                  ;1:
   795                                  ;	mov	(r1)+,r0
   796                                  ;	beq	pwait
   797                                  ;	movb	r0,ttyx+8
   798                                  ;	jsr	pc,dfork
   799                                  ;	mov	r0,(r1)+
   800                                  ;	br	1b
   801                                  ;
   802                                  ;/ wait for process to die
   803                                  ;
   804                                  ;pwait:
   805                                  ;	sys	wait
   806                                  ;	mov	$itab,r1
   807                                  ;
   808                                  ;/ search for process id
   809                                  ;
   810                                  ;2:
   811                                  ;	tst	(r1)+
   812                                  ;	beq	pwait
   813                                  ;	cmp	r0,(r1)+
   814                                  ;	bne	2b
   815                                  ;
   816                                  ;/ take name out of utmp
   817                                  ;
   818                                  ;	sub	$4,r1
   819                                  ;	mov	r1,-(sp)
   820                                  ;	mov	(r1),r1
   821                                  ;	sub	$'0,r1
   822                                  ;	cmp	r1,$'a-'0
   823                                  ;	blo	2f
   824                                  ;	sub	$'a-'0-10.,r1	/ map a-z into 10. on
   825                                  ;2:
   826                                  ;	asl	r1
   827                                  ;	asl	r1
   828                                  ;	asl	r1
   829                                  ;	asl	r1
   830                                  ;	mov	r1,0f
   831                                  ;	mov	$zero,r1
   832                                  ;2:
   833                                  ;	clr	(r1)+
   834                                  ;	cmp	r1,$zero+16.
   835                                  ;	blo	2b
   836                                  ;	sys	open; utmp; 1
   837                                  ;	bes	2f
   838                                  ;	mov	r0,r1
   839                                  ;	sys	seek; 0:..; 0
   840                                  ;	mov	r1,r0
   841                                  ;	sys	write; zero; 16.
   842                                  ;	mov	r1,r0
   843                                  ;	sys	close
   844                                  ;
   845                                  ;/ re-create user process
   846                                  ;
   847                                  ;2:
   848                                  ;	mov	(sp)+,r1
   849                                  ;	mov	(r1)+,r0
   850                                  ;	movb	r0,ttyx+8
   851                                  ;	movb	r0,zero+8.
   852                                  ;	jsr	pc,wtmprec
   853                                  ;	jsr	pc,dfork
   854                                  ;	mov	r0,(r1)+
   855                                  ;	br	pwait
   856                                  ;
   857                                  ;dfork:
   858                                  ;	sys	fork
   859                                  ;		br 1f
   860                                  ;	bes	dfork
   861                                  ;	rts	pc
   862                                  ;1:
   863                                  ;	sys	quit; 0
   864                                  ;	sys	intr; 0
   865                                  ;	sys	chown; ttyx; 0
   866                                  ;	sys	chmod; ttyx; 15
   867                                  ;	sys	open; ttyx; 0
   868                                  ;	bes	help1
   869                                  ;	sys	open; ttyx; 1
   870                                  ;	bes	help1
   871                                  ;	sys	exec; getty; gettyp
   872                                  ;	sys	exit			/ HELP!
   873                                  ;
   874                                  ;help1:
   875                                  ;	jmp	help
   876                                  ;
   877                                  ;wtmprec:
   878                                  ;	mov	r1,-(sp)
   879                                  ;	sys	time
   880                                  ;	mov	r0,zero+10.
   881                                  ;	mov	r1,zero+12.
   882                                  ;	sys	open; wtmp; 1
   883                                  ;	bes	2f
   884                                  ;	mov	r0,r2
   885                                  ;	sys	seek; 0; 2
   886                                  ;	mov	r2,r0
   887                                  ;	sys	write; zero; 16.
   888                                  ;	mov	r2,r0
   889                                  ;	sys	close
   890                                  ;2:
   891                                  ;	mov	(sp)+,r1
   892                                  ;	rts	pc
   893                                  ;
   894                                  ;etcdpdp:
   895                                  ;	etcdpd; 0
   896                                  ;meldap:
   897                                  ;	melda; 0
   898                                  ;usrddp:
   899                                  ;	usrdd; 0
   900                                  ;usrdd:	</usr/demo/dds\0>
   901                                  ;melda:	</usr/mel/da\0>
   902                                  ;usrmel:</usr/mel\0>
   903                                  ;rk1:	</dev/rk1\0>
   904                                  ;rk2:	</dev/rk2\0>
   905                                  ;rk3:	</dev/rk3\0>
   906                                  ;usr:	</usr\0>
   907                                  ;ssys:	</sys\0>
   908                                  ;crp:	</crp\0>
   909                                  ;ctty:	</dev/tty\0>
   910                                  ;shell:	</bin/sh\0>
   911                                  ;shellm:<-\0>
   912                                  ;dpdlock:
   913                                  ;	</usr/dpd/lock\0>
   914                                  ;etcdpd:
   915                                  ;	</etc/dpd\0>
   916                                  ;tapx:	</dev/tapx\0>
   917                                  ;utmp:	</tmp/utmp\0>
   918                                  ;wtmp:	</tmp/wtmp\0>
   919                                  ;ttyx:	</dev/ttyx\0>
   920                                  ;getty:	</etc/getty\0>
   921                                  ;	.even
   922                                  ;
   923                                  ;shellp:shellm
   924                                  ;	0
   925                                  ;gettyp:getty
   926                                  ;	0
   927                                  ;itab:
   928                                  ;	'0; ..
   929                                  ;	'1; ..
   930                                  ;	'2; ..
   931                                  ;	'3; ..
   932                                  ;	'4; ..
   933                                  ;	'5; ..
   934                                  ;	'6; ..
   935                                  ;	'7; ..
   936                                  ;	'8; ..
   937                                  ;	'a; ..
   938                                  ;	'b; ..
   939                                  ;	 0
   940                                  ;
   941                                  ;	.bss
   942                                  ;offset:.=.+2
   943                                  ;zero:	.=.+8.; .=.+6; .=.+2.
