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