     1                                  ; ****************************************************************************
     2                                  ; init386.s (INIT4.ASM, init4.s)
     3                                  ; ----------------------------------------------------------------------------
     4                                  ;
     5                                  ; Retro UNIX 386 v1 - /etc/init - process control initialization
     6                                  ;
     7                                  ; [ Last Modification: 17/11/2015 ]
     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                                  
    15                                  ; (Retro UNIX 386 v1 - init386.s, NASM 2.11)
    16                                  ; Derived from INIT09.ASM (17/01/2014, Retro UNIX 8086 v1, MASM 6.11)  
    17                                  ; Derived from 'init.s' file of original UNIX v1
    18                                  ; INIT09.ASM, 17/01/2014 
    19                                  
    20                                  ; Retro UNIX 386 v1.2 test - 25/12/2021
    21                                  
    22                                  ; 23/10/2015 (init3.s)
    23                                  ; 14/10/2015 (init2.s)
    24                                  ; 17/09/2015
    25                                  ; 03/09/2015
    26                                  ; 27/08/2015
    27                                  ; 13/08/2015
    28                                  ; 07/08/2015
    29                                  ; 30/06/2015
    30                                  ; 14/07/2013
    31                                  
    32                                  ; UNIX v1 system calls
    33                                  _rele 	equ 0
    34                                  _exit 	equ 1
    35                                  _fork 	equ 2
    36                                  _read 	equ 3
    37                                  _write	equ 4
    38                                  _open	equ 5
    39                                  _close 	equ 6
    40                                  _wait 	equ 7
    41                                  _creat 	equ 8
    42                                  _link 	equ 9
    43                                  _unlink	equ 10
    44                                  _exec	equ 11
    45                                  _chdir	equ 12
    46                                  _time 	equ 13
    47                                  _mkdir 	equ 14
    48                                  _chmod	equ 15
    49                                  _chown	equ 16
    50                                  _break	equ 17
    51                                  _stat	equ 18
    52                                  _seek	equ 19
    53                                  _tell 	equ 20
    54                                  _mount	equ 21
    55                                  _umount	equ 22
    56                                  _setuid	equ 23
    57                                  _getuid	equ 24
    58                                  _stime	equ 25
    59                                  _quit	equ 26	
    60                                  _intr	equ 27
    61                                  _fstat	equ 28
    62                                  _emt 	equ 29
    63                                  _mdate 	equ 30
    64                                  _stty 	equ 31
    65                                  _gtty	equ 32
    66                                  _ilgins	equ 33
    67                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    68                                  _msg    equ 35 ; Retro UNIX 386 v1 feature only !
    69                                  
    70                                  ;;;
    71                                  ESCKey equ 1Bh
    72                                  EnterKey equ 0Dh
    73                                  
    74                                  %macro sys 1-4
    75                                      ; 03/09/2015	
    76                                      ; 13/04/2015
    77                                      ; Retro UNIX 386 v1 system call.		
    78                                      %if %0 >= 2   
    79                                          mov ebx, %2
    80                                          %if %0 >= 3    
    81                                              mov ecx, %3
    82                                              %if %0 = 4
    83                                                 mov edx, %4   
    84                                              %endif
    85                                          %endif
    86                                      %endif
    87                                      mov eax, %1
    88                                      int 30h	   
    89                                  %endmacro
    90                                  
    91                                  ; Retro UNIX 386 v1 system call format:
    92                                  ; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
    93                                  
    94                                  [BITS 32] ; We need 32-bit intructions for protected mode
    95                                  
    96                                  [ORG 0] 
    97                                  
    98                                  START_CODE:
    99                                  	sys _intr, 0  ; disable time-out function 
   100                              <1> 
   101                              <1> 
   102                              <1> 
   103                              <1>  %if %0 >= 2
   104 00000000 BB00000000          <1>  mov ebx, %2
   105                              <1>  %if %0 >= 3
   106                              <1>  mov ecx, %3
   107                              <1>  %if %0 = 4
   108                              <1>  mov edx, %4
   109                              <1>  %endif
   110                              <1>  %endif
   111                              <1>  %endif
   112 00000005 B81B000000          <1>  mov eax, %1
   113 0000000A CD30                <1>  int 30h
   114                                  	sys _quit, 0  ; disable quit (ctrl+brk) signal
   115                              <1> 
   116                              <1> 
   117                              <1> 
   118                              <1>  %if %0 >= 2
   119 0000000C BB00000000          <1>  mov ebx, %2
   120                              <1>  %if %0 >= 3
   121                              <1>  mov ecx, %3
   122                              <1>  %if %0 = 4
   123                              <1>  mov edx, %4
   124                              <1>  %endif
   125                              <1>  %endif
   126                              <1>  %endif
   127 00000011 B81A000000          <1>  mov eax, %1
   128 00000016 CD30                <1>  int 30h
   129                                  
   130                                          sys _open, ctty, 0 ; open tty0
   131                              <1> 
   132                              <1> 
   133                              <1> 
   134                              <1>  %if %0 >= 2
   135 00000018 BB[06030000]        <1>  mov ebx, %2
   136                              <1>  %if %0 >= 3
   137 0000001D B900000000          <1>  mov ecx, %3
   138                              <1>  %if %0 = 4
   139                              <1>  mov edx, %4
   140                              <1>  %endif
   141                              <1>  %endif
   142                              <1>  %endif
   143 00000022 B805000000          <1>  mov eax, %1
   144 00000027 CD30                <1>  int 30h
   145 00000029 7268                    	jc  short error
   146                                  
   147                                          sys _open, ctty, 1 ; for read and write
   148                              <1> 
   149                              <1> 
   150                              <1> 
   151                              <1>  %if %0 >= 2
   152 0000002B BB[06030000]        <1>  mov ebx, %2
   153                              <1>  %if %0 >= 3
   154 00000030 B901000000          <1>  mov ecx, %3
   155                              <1>  %if %0 = 4
   156                              <1>  mov edx, %4
   157                              <1>  %endif
   158                              <1>  %endif
   159                              <1>  %endif
   160 00000035 B805000000          <1>  mov eax, %1
   161 0000003A CD30                <1>  int 30h
   162 0000003C 7255                    	jc  short error
   163                                  
   164                                  	sys _write, 1, msg_te, sizeof_mte
   165                              <1> 
   166                              <1> 
   167                              <1> 
   168                              <1>  %if %0 >= 2
   169 0000003E BB01000000          <1>  mov ebx, %2
   170                              <1>  %if %0 >= 3
   171 00000043 B9[8E030000]        <1>  mov ecx, %3
   172                              <1>  %if %0 = 4
   173 00000048 BA55000000          <1>  mov edx, %4
   174                              <1>  %endif
   175                              <1>  %endif
   176                              <1>  %endif
   177 0000004D B804000000          <1>  mov eax, %1
   178 00000052 CD30                <1>  int 30h
   179 00000054 723D                            jc  short error
   180                                  i0:
   181                                  	sys _write, 1, msg_ok, sizeof_msgok ; temporary ; 25/12/2021
   182                              <1> 
   183                              <1> 
   184                              <1> 
   185                              <1>  %if %0 >= 2
   186 00000056 BB01000000          <1>  mov ebx, %2
   187                              <1>  %if %0 >= 3
   188 0000005B B9[30040000]        <1>  mov ecx, %3
   189                              <1>  %if %0 = 4
   190 00000060 BA0A000000          <1>  mov edx, %4
   191                              <1>  %endif
   192                              <1>  %endif
   193                              <1>  %endif
   194 00000065 B804000000          <1>  mov eax, %1
   195 0000006A CD30                <1>  int 30h
   196                                  
   197                                  	sys _read, 0, tchar, 1
   198                              <1> 
   199                              <1> 
   200                              <1> 
   201                              <1>  %if %0 >= 2
   202 0000006C BB00000000          <1>  mov ebx, %2
   203                              <1>  %if %0 >= 3
   204 00000071 B9[04030000]        <1>  mov ecx, %3
   205                              <1>  %if %0 = 4
   206 00000076 BA01000000          <1>  mov edx, %4
   207                              <1>  %endif
   208                              <1>  %endif
   209                              <1>  %endif
   210 0000007B B803000000          <1>  mov eax, %1
   211 00000080 CD30                <1>  int 30h
   212 00000082 720F                    	jc  short error
   213                                  
   214                                  	;sys _close, 0 ; close input file/tty
   215                                  	;jc  short error
   216                                          ;sys _close, 1 ; close output file/tty
   217                                  	;jc  short error
   218                                  
   219 00000084 A0[04030000]            	mov al, [tchar]
   220                                  
   221 00000089 3C0D                            cmp al, EnterKey
   222 0000008B 7479                    	je  short multiuser
   223                                  
   224 0000008D 3C1B                    	cmp al, ESCKey
   225 0000008F 7428                    	je  short singleuser
   226                                  
   227 00000091 EBC3                    	jmp short i0
   228                                  		
   229                                  error:
   230                                  	sys _msg, error_msg, 255, 0Ch ; error message with red color (max. 255 chars)
   231                              <1> 
   232                              <1> 
   233                              <1> 
   234                              <1>  %if %0 >= 2
   235 00000093 BB[03040000]        <1>  mov ebx, %2
   236                              <1>  %if %0 >= 3
   237 00000098 B9FF000000          <1>  mov ecx, %3
   238                              <1>  %if %0 = 4
   239 0000009D BA0C000000          <1>  mov edx, %4
   240                              <1>  %endif
   241                              <1>  %endif
   242                              <1>  %endif
   243 000000A2 B823000000          <1>  mov eax, %1
   244 000000A7 CD30                <1>  int 30h
   245                                  exit:
   246                                  	sys _exit
   247                              <1> 
   248                              <1> 
   249                              <1> 
   250                              <1>  %if %0 >= 2
   251                              <1>  mov ebx, %2
   252                              <1>  %if %0 >= 3
   253                              <1>  mov ecx, %3
   254                              <1>  %if %0 = 4
   255                              <1>  mov edx, %4
   256                              <1>  %endif
   257                              <1>  %endif
   258                              <1>  %endif
   259 000000A9 B801000000          <1>  mov eax, %1
   260 000000AE CD30                <1>  int 30h
   261                                  
   262                                  haltsys:
   263                                  	;hlt
   264                                  	; 17/09/2015
   265 000000B0 90                      	nop
   266 000000B1 90                      	nop
   267 000000B2 90                      	nop
   268 000000B3 90                      	nop
   269 000000B4 90                      	nop
   270 000000B5 90                      	nop
   271 000000B6 90                      	nop
   272 000000B7 EBF7                    	jmp short haltsys
   273                                  
   274                                  singleuser:
   275                                  i1:
   276                                  help:
   277                                  	sys _close, 0 ; close input file/tty
   278                              <1> 
   279                              <1> 
   280                              <1> 
   281                              <1>  %if %0 >= 2
   282 000000B9 BB00000000          <1>  mov ebx, %2
   283                              <1>  %if %0 >= 3
   284                              <1>  mov ecx, %3
   285                              <1>  %if %0 = 4
   286                              <1>  mov edx, %4
   287                              <1>  %endif
   288                              <1>  %endif
   289                              <1>  %endif
   290 000000BE B806000000          <1>  mov eax, %1
   291 000000C3 CD30                <1>  int 30h
   292                                  	;jc short error
   293                                  	sys _close, 1 ; close output file/tty
   294                              <1> 
   295                              <1> 
   296                              <1> 
   297                              <1>  %if %0 >= 2
   298 000000C5 BB01000000          <1>  mov ebx, %2
   299                              <1>  %if %0 >= 3
   300                              <1>  mov ecx, %3
   301                              <1>  %if %0 = 4
   302                              <1>  mov edx, %4
   303                              <1>  %endif
   304                              <1>  %endif
   305                              <1>  %endif
   306 000000CA B806000000          <1>  mov eax, %1
   307 000000CF CD30                <1>  int 30h
   308                                  	;jc short error
   309                                  	sys _open, ctty, 0 ; open control tty
   310                              <1> 
   311                              <1> 
   312                              <1> 
   313                              <1>  %if %0 >= 2
   314 000000D1 BB[06030000]        <1>  mov ebx, %2
   315                              <1>  %if %0 >= 3
   316 000000D6 B900000000          <1>  mov ecx, %3
   317                              <1>  %if %0 = 4
   318                              <1>  mov edx, %4
   319                              <1>  %endif
   320                              <1>  %endif
   321                              <1>  %endif
   322 000000DB B805000000          <1>  mov eax, %1
   323 000000E0 CD30                <1>  int 30h
   324                                  	;jc short error
   325                                          sys _open, ctty, 1 ; for read an write
   326                              <1> 
   327                              <1> 
   328                              <1> 
   329                              <1>  %if %0 >= 2
   330 000000E2 BB[06030000]        <1>  mov ebx, %2
   331                              <1>  %if %0 >= 3
   332 000000E7 B901000000          <1>  mov ecx, %3
   333                              <1>  %if %0 = 4
   334                              <1>  mov edx, %4
   335                              <1>  %endif
   336                              <1>  %endif
   337                              <1>  %endif
   338 000000EC B805000000          <1>  mov eax, %1
   339 000000F1 CD30                <1>  int 30h
   340                                  	;jc short error
   341                                  	;
   342                                  	sys _exec, shell, shellp
   343                              <1> 
   344                              <1> 
   345                              <1> 
   346                              <1>  %if %0 >= 2
   347 000000F3 BB[10030000]        <1>  mov ebx, %2
   348                              <1>  %if %0 >= 3
   349 000000F8 B9[54030000]        <1>  mov ecx, %3
   350                              <1>  %if %0 = 4
   351                              <1>  mov edx, %4
   352                              <1>  %endif
   353                              <1>  %endif
   354                              <1>  %endif
   355 000000FD B80B000000          <1>  mov eax, %1
   356 00000102 CD30                <1>  int 30h
   357                                  	;
   358 00000104 EBB3                     	jmp short i1
   359                                  
   360                                  multiuser:
   361                                  	sys _close, 0 ; close input file/tty
   362                              <1> 
   363                              <1> 
   364                              <1> 
   365                              <1>  %if %0 >= 2
   366 00000106 BB00000000          <1>  mov ebx, %2
   367                              <1>  %if %0 >= 3
   368                              <1>  mov ecx, %3
   369                              <1>  %if %0 = 4
   370                              <1>  mov edx, %4
   371                              <1>  %endif
   372                              <1>  %endif
   373                              <1>  %endif
   374 0000010B B806000000          <1>  mov eax, %1
   375 00000110 CD30                <1>  int 30h
   376                                  	;jc short error
   377                                          sys _close, 1 ; close output file/tty
   378                              <1> 
   379                              <1> 
   380                              <1> 
   381                              <1>  %if %0 >= 2
   382 00000112 BB01000000          <1>  mov ebx, %2
   383                              <1>  %if %0 >= 3
   384                              <1>  mov ecx, %3
   385                              <1>  %if %0 = 4
   386                              <1>  mov edx, %4
   387                              <1>  %endif
   388                              <1>  %endif
   389                              <1>  %endif
   390 00000117 B806000000          <1>  mov eax, %1
   391 0000011C CD30                <1>  int 30h
   392                                  	;jc short error
   393                                  	;
   394                                  	sys _mount, fd1, usr  ; root directory on mounted fd1
   395                              <1> 
   396                              <1> 
   397                              <1> 
   398                              <1>  %if %0 >= 2
   399 0000011E BB[20030000]        <1>  mov ebx, %2
   400                              <1>  %if %0 >= 3
   401 00000123 B9[1A030000]        <1>  mov ecx, %3
   402                              <1>  %if %0 = 4
   403                              <1>  mov edx, %4
   404                              <1>  %endif
   405                              <1>  %endif
   406                              <1>  %endif
   407 00000128 B815000000          <1>  mov eax, %1
   408 0000012D CD30                <1>  int 30h
   409                                  			      ; disk is /usr
   410                                  
   411                                  	sys _creat, utmp, 14  ; truncate /tmp/utmp
   412                              <1> 
   413                              <1> 
   414                              <1> 
   415                              <1>  %if %0 >= 2
   416 0000012F BB[2A030000]        <1>  mov ebx, %2
   417                              <1>  %if %0 >= 3
   418 00000134 B90E000000          <1>  mov ecx, %3
   419                              <1>  %if %0 = 4
   420                              <1>  mov edx, %4
   421                              <1>  %endif
   422                              <1>  %endif
   423                              <1>  %endif
   424 00000139 B808000000          <1>  mov eax, %1
   425 0000013E CD30                <1>  int 30h
   426                                  	;jc short error
   427                                  
   428                                  	sys _close, eax	      ; close it
   429                              <1> 
   430                              <1> 
   431                              <1> 
   432                              <1>  %if %0 >= 2
   433 00000140 89C3                <1>  mov ebx, %2
   434                              <1>  %if %0 >= 3
   435                              <1>  mov ecx, %3
   436                              <1>  %if %0 = 4
   437                              <1>  mov edx, %4
   438                              <1>  %endif
   439                              <1>  %endif
   440                              <1>  %endif
   441 00000142 B806000000          <1>  mov eax, %1
   442 00000147 CD30                <1>  int 30h
   443                                  
   444 00000149 C605[44040000]00        	mov byte [zero+8], 0  ; put identifier 
   445                                  			      ; in output buffer
   446                                  
   447 00000150 E847010000              	call wtmprec	      ; go to write acting info
   448                                  
   449                                  	; 23/10/2015
   450                                  	;
   451                                  	;; 10/12/2013
   452                                  	;; 'Enable Multi Tasking' (Time-Out)
   453                                  	;; system call (Retro UNIX 8086 v1 feature only !)
   454                                  	sys _emt, 1
   455                              <1> 
   456                              <1> 
   457                              <1> 
   458                              <1>  %if %0 >= 2
   459 00000155 BB01000000          <1>  mov ebx, %2
   460                              <1>  %if %0 >= 3
   461                              <1>  mov ecx, %3
   462                              <1>  %if %0 = 4
   463                              <1>  mov edx, %4
   464                              <1>  %endif
   465                              <1>  %endif
   466                              <1>  %endif
   467 0000015A B81D000000          <1>  mov eax, %1
   468 0000015F CD30                <1>  int 30h
   469                                  	;;
   470                                  
   471 00000161 BE[64030000]            	mov esi, itab         ; address of table to eSI
   472                                  
   473                                  ; create shell processes
   474                                  i2:
   475 00000166 66AD                    	lodsw 		      ; 'x', x=0, 1... to AX
   476 00000168 6621C0                  	and ax, ax
   477 0000016B 7412                    	jz  short pwait	      ; branch if table end
   478                                  	
   479 0000016D A2[46030000]            	mov [ttyx+8], al      ; put symbol in ttyx
   480 00000172 89F7                    	mov edi, esi	
   481 00000174 E8A1000000              	call dfork            ; go to make new init for this ttyx
   482 00000179 66AB                    	stosw		      ; save child id in word offer
   483                                  	 		      ;	'0', '1',...etc.
   484 0000017B 89FE                    	mov esi, edi
   485 0000017D EBE7                    	jmp short i2          ; set up next child
   486                                  
   487                                  ; wait for process to die
   488                                  pwait:
   489                                  	;sys _write, 1, beep, 1 ; 10/12/2013
   490                                  	;
   491                                          sys _wait       ; wait for user to terminate process
   492                              <1> 
   493                              <1> 
   494                              <1> 
   495                              <1>  %if %0 >= 2
   496                              <1>  mov ebx, %2
   497                              <1>  %if %0 >= 3
   498                              <1>  mov ecx, %3
   499                              <1>  %if %0 = 4
   500                              <1>  mov edx, %4
   501                              <1>  %endif
   502                              <1>  %endif
   503                              <1>  %endif
   504 0000017F B807000000          <1>  mov eax, %1
   505 00000184 CD30                <1>  int 30h
   506 00000186 BE[64030000]            	mov esi, itab   ; initialize for search
   507 0000018B 6689C2                  	mov dx, ax
   508                                  
   509                                  ; search for process id
   510                                  i3:
   511 0000018E 66AD                    	lodsw		; bump eSI to child id location
   512 00000190 6609C0                  	or  ax, ax
   513 00000193 74EA                    	jz  short pwait ; ? something silly
   514                                  
   515 00000195 66AD                    	lodsw
   516 00000197 6639C2                  	cmp dx, ax      ; which process has terminated
   517 0000019A 75F2                    	jne short i3  	; not this one
   518                                  
   519                                  ; take name out of utmp
   520 0000019C B904000000              	mov ecx, 4
   521                                  	;sub esi, 4     ; process is found, point x to 'x'
   522                                  		        ; for it
   523 000001A1 29CE                    	sub esi, ecx  ; 4
   524                                  	;push esi	; save address on stack
   525 000001A3 668B06                  	mov ax, [esi] 	; move 'x' to AX
   526 000001A6 6683E830                	sub ax, '0'	; remove zone bits from character
   527                                  	;shl ax, 4	; generate proper offset for seek
   528 000001AA 66D3E0                  	shl ax, cl ; 4
   529 000001AD 0FB7D0                  	movzx edx, ax	
   530 000001B0 BF[3C040000]            	mov edi, zero
   531 000001B5 31C0                    	xor eax, eax ; 0 ; clear	
   532                                  	;mov ecx, 4	 ; output buffer	
   533 000001B7 F3AB                    	rep stosd	 
   534                                  	sys _open, utmp, 1 ; open file for writing
   535                              <1> 
   536                              <1> 
   537                              <1> 
   538                              <1>  %if %0 >= 2
   539 000001B9 BB[2A030000]        <1>  mov ebx, %2
   540                              <1>  %if %0 >= 3
   541 000001BE B901000000          <1>  mov ecx, %3
   542                              <1>  %if %0 = 4
   543                              <1>  mov edx, %4
   544                              <1>  %endif
   545                              <1>  %endif
   546                              <1>  %endif
   547 000001C3 B805000000          <1>  mov eax, %1
   548 000001C8 CD30                <1>  int 30h
   549 000001CA 722F                    	jc  short i4	 ; if can't open, create user anyway
   550 000001CC 0FB7F8                  	movzx edi, ax	 ; save file desc
   551                                  	sys _seek, eax, edx, 0 ; move to proper pointer position
   552                              <1> 
   553                              <1> 
   554                              <1> 
   555                              <1>  %if %0 >= 2
   556 000001CF 89C3                <1>  mov ebx, %2
   557                              <1>  %if %0 >= 3
   558 000001D1 89D1                <1>  mov ecx, %3
   559                              <1>  %if %0 = 4
   560 000001D3 BA00000000          <1>  mov edx, %4
   561                              <1>  %endif
   562                              <1>  %endif
   563                              <1>  %endif
   564 000001D8 B813000000          <1>  mov eax, %1
   565 000001DD CD30                <1>  int 30h
   566                                  	sys _write, edi, zero, 16 ; zero this position in
   567                              <1> 
   568                              <1> 
   569                              <1> 
   570                              <1>  %if %0 >= 2
   571 000001DF 89FB                <1>  mov ebx, %2
   572                              <1>  %if %0 >= 3
   573 000001E1 B9[3C040000]        <1>  mov ecx, %3
   574                              <1>  %if %0 = 4
   575 000001E6 BA10000000          <1>  mov edx, %4
   576                              <1>  %endif
   577                              <1>  %endif
   578                              <1>  %endif
   579 000001EB B804000000          <1>  mov eax, %1
   580 000001F0 CD30                <1>  int 30h
   581                                  	sys _close, edi  ; close file
   582                              <1> 
   583                              <1> 
   584                              <1> 
   585                              <1>  %if %0 >= 2
   586 000001F2 89FB                <1>  mov ebx, %2
   587                              <1>  %if %0 >= 3
   588                              <1>  mov ecx, %3
   589                              <1>  %if %0 = 4
   590                              <1>  mov edx, %4
   591                              <1>  %endif
   592                              <1>  %endif
   593                              <1>  %endif
   594 000001F4 B806000000          <1>  mov eax, %1
   595 000001F9 CD30                <1>  int 30h
   596                                  
   597                                  ; re-create user process
   598                                  i4:
   599                                  	;pop esi	; restore 'x' to eSI
   600 000001FB 66AD                    	lodsw		; move it to AX
   601 000001FD 89F7                    	mov edi, esi 
   602 000001FF A2[46030000]            	mov [ttyx+8], al ; get correct ttyx
   603 00000204 A2[44040000]            	mov [zero+8], al
   604                                  	 		; move identifier to output buffer
   605 00000209 E88E000000              	call wtmprec    ; go to write accting into
   606 0000020E E807000000              	call dfork      ; fork
   607 00000213 66AB                    	stosw		; save id of child
   608 00000215 E965FFFFFF              	jmp pwait ; go to wait for next process end
   609                                  
   610                                  dfork:
   611 0000021A BB[29020000]            	mov ebx, i5 ; return address for new process
   612                                  	sys _fork
   613                              <1> 
   614                              <1> 
   615                              <1> 
   616                              <1>  %if %0 >= 2
   617                              <1>  mov ebx, %2
   618                              <1>  %if %0 >= 3
   619                              <1>  mov ecx, %3
   620                              <1>  %if %0 = 4
   621                              <1>  mov edx, %4
   622                              <1>  %endif
   623                              <1>  %endif
   624                              <1>  %endif
   625 0000021F B802000000          <1>  mov eax, %1
   626 00000224 CD30                <1>  int 30h
   627 00000226 72F2                    	jc  short dfork ; try again
   628 00000228 C3                      	retn
   629                                  
   630                                  i5: ; to new copy of init
   631                                  	;sys _quit, 0  ; disable quit (ctrl+brk) signal
   632                                  	;sys _intr, 0  ; disable time-out function 
   633                                  	;sys _chown, ttyx, 0
   634                                  	;sys _chmod; ttyx, 15
   635                                  o0:	
   636 00000229 31DB                    	xor ebx, ebx
   637                                  	;xor ch, ch
   638                                  	;mov cl, [ttyx+8]
   639 0000022B 0FB60D[46030000]        	movzx ecx, byte [ttyx+8]
   640 00000232 80E930                  	sub cl, '0'
   641                                  	; 17/01/2014
   642                                  	; 07/12/2013
   643                                  	; set console tty for current process
   644                                  	;mov dx, 0FF00h
   645                                  	;mov dh, 0FFh ; do not set cursor position
   646                                  		     ; do not set serial port parameters	
   647 00000235 BA00FF0000              	mov edx, 0FF00h	
   648                                  	sys _stty
   649                              <1> 
   650                              <1> 
   651                              <1> 
   652                              <1>  %if %0 >= 2
   653                              <1>  mov ebx, %2
   654                              <1>  %if %0 >= 3
   655                              <1>  mov ecx, %3
   656                              <1>  %if %0 = 4
   657                              <1>  mov edx, %4
   658                              <1>  %endif
   659                              <1>  %endif
   660                              <1>  %endif
   661 0000023A B81F000000          <1>  mov eax, %1
   662 0000023F CD30                <1>  int 30h
   663 00000241 724D                            jc  short terminate
   664                                  o1:
   665                                  	; 16/11/2015	
   666                                  	sys _open, ttyx, 0 ; open this ttyx for reading
   667                              <1> 
   668                              <1> 
   669                              <1> 
   670                              <1>  %if %0 >= 2
   671 00000243 BB[3E030000]        <1>  mov ebx, %2
   672                              <1>  %if %0 >= 3
   673 00000248 B900000000          <1>  mov ecx, %3
   674                              <1>  %if %0 = 4
   675                              <1>  mov edx, %4
   676                              <1>  %endif
   677                              <1>  %endif
   678                              <1>  %endif
   679 0000024D B805000000          <1>  mov eax, %1
   680 00000252 CD30                <1>  int 30h
   681                                  	;jnc  short o2
   682                                  	;call wait_for_terminal
   683                                  	;jmp  short o1
   684                                  	; 17/11/2015
   685 00000254 723A                    	jc  short terminate	
   686                                  o2:
   687                                  	; 16/11/2015
   688                                  	sys _open, ttyx, 1 ; open this ttyx for writing
   689                              <1> 
   690                              <1> 
   691                              <1> 
   692                              <1>  %if %0 >= 2
   693 00000256 BB[3E030000]        <1>  mov ebx, %2
   694                              <1>  %if %0 >= 3
   695 0000025B B901000000          <1>  mov ecx, %3
   696                              <1>  %if %0 = 4
   697                              <1>  mov edx, %4
   698                              <1>  %endif
   699                              <1>  %endif
   700                              <1>  %endif
   701 00000260 B805000000          <1>  mov eax, %1
   702 00000265 CD30                <1>  int 30h
   703                                  	;jnc short o3
   704                                  	;call wait_for_terminal
   705                                  	;jmp  short o2
   706                                  	; 17/11/2015
   707 00000267 7227                    	jc  short terminate			
   708                                  o3:	
   709                                  	sys _exec, getty, gettyp ; getty types <login> and
   710                              <1> 
   711                              <1> 
   712                              <1> 
   713                              <1>  %if %0 >= 2
   714 00000269 BB[48030000]        <1>  mov ebx, %2
   715                              <1>  %if %0 >= 3
   716 0000026E B9[5C030000]        <1>  mov ecx, %3
   717                              <1>  %if %0 = 4
   718                              <1>  mov edx, %4
   719                              <1>  %endif
   720                              <1>  %endif
   721                              <1>  %endif
   722 00000273 B80B000000          <1>  mov eax, %1
   723 00000278 CD30                <1>  int 30h
   724                                  		           	 ; executes login which logs user
   725                                  			         ; in and executes sh-
   726                                  	; 14/10/2015
   727                                  	sys _msg, getty_error_msg, 255, 0Ch ; error msg with red color
   728                              <1> 
   729                              <1> 
   730                              <1> 
   731                              <1>  %if %0 >= 2
   732 0000027A BB[19040000]        <1>  mov ebx, %2
   733                              <1>  %if %0 >= 3
   734 0000027F B9FF000000          <1>  mov ecx, %3
   735                              <1>  %if %0 = 4
   736 00000284 BA0C000000          <1>  mov edx, %4
   737                              <1>  %endif
   738                              <1>  %endif
   739                              <1>  %endif
   740 00000289 B823000000          <1>  mov eax, %1
   741 0000028E CD30                <1>  int 30h
   742                                  
   743                                  terminate:
   744                                  	sys _exit  ; HELP!
   745                              <1> 
   746                              <1> 
   747                              <1> 
   748                              <1>  %if %0 >= 2
   749                              <1>  mov ebx, %2
   750                              <1>  %if %0 >= 3
   751                              <1>  mov ecx, %3
   752                              <1>  %if %0 = 4
   753                              <1>  mov edx, %4
   754                              <1>  %endif
   755                              <1>  %endif
   756                              <1>  %endif
   757 00000290 B801000000          <1>  mov eax, %1
   758 00000295 CD30                <1>  int 30h
   759                                  ;help1:
   760 00000297 E91DFEFFFF              	jmp help
   761                                  
   762                                  ;wait_for_terminal:
   763                                  ;	; 16/11/2015
   764                                  ;	; 24/10/2015
   765                                  ;	mov al, [ttyx+8]
   766                                  ;	sub al, '0'
   767                                  ;	cmp al, 8
   768                                  ;	jb  short sysexit
   769                                  ;	sys _sleep
   770                                  ;	retn
   771                                  ;sysexit:
   772                                  ;	pop eax ;return address	
   773                                  ;	jmp short terminate
   774                                  
   775                                  wtmprec:
   776                                  	; 23/10/2015 (wtmp_err)
   777 0000029C 803D[4C040000]00        	cmp byte [wtmp_err], 0
   778 000002A3 7750                    	ja  short i6 	
   779                                  
   780                                  	sys _time  ; get time
   781                              <1> 
   782                              <1> 
   783                              <1> 
   784                              <1>  %if %0 >= 2
   785                              <1>  mov ebx, %2
   786                              <1>  %if %0 >= 3
   787                              <1>  mov ecx, %3
   788                              <1>  %if %0 = 4
   789                              <1>  mov edx, %4
   790                              <1>  %endif
   791                              <1>  %endif
   792                              <1>  %endif
   793 000002A5 B80D000000          <1>  mov eax, %1
   794 000002AA CD30                <1>  int 30h
   795 000002AC A3[46040000]            	mov [zero+10], eax ; move to output buffer
   796                                  
   797                                  	sys _open, wtmp, 1 ; open accounting file
   798                              <1> 
   799                              <1> 
   800                              <1> 
   801                              <1>  %if %0 >= 2
   802 000002B1 BB[34030000]        <1>  mov ebx, %2
   803                              <1>  %if %0 >= 3
   804 000002B6 B901000000          <1>  mov ecx, %3
   805                              <1>  %if %0 = 4
   806                              <1>  mov edx, %4
   807                              <1>  %endif
   808                              <1>  %endif
   809                              <1>  %endif
   810 000002BB B805000000          <1>  mov eax, %1
   811 000002C0 CD30                <1>  int 30h
   812 000002C2 7232                    	jc  short i7
   813                                  
   814 000002C4 89C6                    	mov esi, eax ; save file descriptor
   815                                  
   816                                  	sys _seek, eax, 0, 2 ; move pointer to end of file
   817                              <1> 
   818                              <1> 
   819                              <1> 
   820                              <1>  %if %0 >= 2
   821 000002C6 89C3                <1>  mov ebx, %2
   822                              <1>  %if %0 >= 3
   823 000002C8 B900000000          <1>  mov ecx, %3
   824                              <1>  %if %0 = 4
   825 000002CD BA02000000          <1>  mov edx, %4
   826                              <1>  %endif
   827                              <1>  %endif
   828                              <1>  %endif
   829 000002D2 B813000000          <1>  mov eax, %1
   830 000002D7 CD30                <1>  int 30h
   831                                  	;;push esi    ; save file descriptor
   832                                  	;jc  short i7
   833                                  
   834                                  	sys _write, esi, zero, 16 ; write accting info
   835                              <1> 
   836                              <1> 
   837                              <1> 
   838                              <1>  %if %0 >= 2
   839 000002D9 89F3                <1>  mov ebx, %2
   840                              <1>  %if %0 >= 3
   841 000002DB B9[3C040000]        <1>  mov ecx, %3
   842                              <1>  %if %0 = 4
   843 000002E0 BA10000000          <1>  mov edx, %4
   844                              <1>  %endif
   845                              <1>  %endif
   846                              <1>  %endif
   847 000002E5 B804000000          <1>  mov eax, %1
   848 000002EA CD30                <1>  int 30h
   849                                  	;;pop ebx	  ; restore file descriptor
   850                                  	;jc short i7
   851                                  
   852                                  	sys _close, esi ; close file
   853                              <1> 
   854                              <1> 
   855                              <1> 
   856                              <1>  %if %0 >= 2
   857 000002EC 89F3                <1>  mov ebx, %2
   858                              <1>  %if %0 >= 3
   859                              <1>  mov ecx, %3
   860                              <1>  %if %0 = 4
   861                              <1>  mov edx, %4
   862                              <1>  %endif
   863                              <1>  %endif
   864                              <1>  %endif
   865 000002EE B806000000          <1>  mov eax, %1
   866 000002F3 CD30                <1>  int 30h
   867                                  i6:
   868 000002F5 C3                      	retn
   869                                  i7:
   870 000002F6 FE05[4C040000]          	inc  byte [wtmp_err] ; 23/10/2015
   871 000002FC C3                      	retn
   872                                  
   873                                  here:
   874 000002FD F4                      	hlt  ; General Protection Fault ! 17/09/2015
   875                                  	;jmp short here
   876 000002FE E9ADFDFFFF              	jmp haltsys	
   877                                  
   878 00000303 90                      align 2
   879 00000304 00                      tchar:  db 0
   880 00000305 90                      align 2
   881 00000306 2F6465762F74747900      ctty:   db "/dev/tty", 0
   882 0000030F 90                      align 2
   883 00000310 2F62696E2F736800        shell:  db "/bin/sh", 0
   884 00000318 2D00                    shellm: db "-", 0
   885                                  ;align 2
   886 0000031A 2F75737200              usr:	db "/usr",0
   887 0000031F 90                      align 2
   888 00000320 2F6465762F66643100      fd1:	db "/dev/fd1", 0
   889                                  ;
   890 00000329 90                      align 2
   891 0000032A 2F746D702F75746D70-     utmp:   db "/tmp/utmp", 0
   892 00000333 00                 
   893 00000334 2F746D702F77746D70-     wtmp:   db "/tmp/wtmp", 0
   894 0000033D 00                 
   895 0000033E 2F6465762F74747978-     ttyx:   db "/dev/ttyx", 0
   896 00000347 00                 
   897 00000348 2F6574632F67657474-     getty:  db "/etc/getty",0
   898 00000351 7900               
   899                                  
   900 00000353 90                      align 2
   901                                  ; 27/08/2015 (dw -> dd)
   902 00000354 [18030000]              shellp: dd shellm
   903 00000358 00000000                        dd 0
   904 0000035C [48030000]              gettyp: dd getty
   905 00000360 00000000                	dd 0
   906                                  itab:
   907 00000364 30000000                	db '0',0, 0,0
   908 00000368 31000000                	db '1',0, 0,0
   909 0000036C 32000000                	db '2',0, 0,0
   910 00000370 33000000                	db '3',0, 0,0
   911 00000374 34000000                	db '4',0, 0,0
   912 00000378 35000000                	db '5',0, 0,0
   913 0000037C 36000000                	db '6',0, 0,0
   914 00000380 37000000                	db '7',0, 0,0
   915                                  	; serial ports (COM1, COM2)
   916 00000384 38000000                	db '8',0, 0,0
   917 00000388 39000000                	db '9',0, 0,0
   918 0000038C 0000                    	dw 0
   919                                  
   920                                  ;-----------------------------------------------------------------
   921                                  ;  messages
   922                                  ;-----------------------------------------------------------------
   923                                  
   924                                  msg_te:
   925 0000038E 0D0A                            db 0Dh, 0Ah
   926 00000390 5479706520454E5445-             db 'Type ENTER to start in multi user mode', 0Dh, 0Ah
   927 00000399 5220746F2073746172-
   928 000003A2 7420696E206D756C74-
   929 000003AB 692075736572206D6F-
   930 000003B4 64650D0A           
   931 000003B8 6F7220747970652045-             db 'or type ESC to start in single user mode.'
   932 000003C1 534320746F20737461-
   933 000003CA 727420696E2073696E-
   934 000003D3 676C65207573657220-
   935 000003DC 6D6F64652E         
   936 000003E1 0D0A                            db 0Dh, 0Ah
   937                                  sizeof_mte equ $ - msg_te 
   938 000003E3 00                              db 0
   939                                  copy_right_msg:
   940 000003E4 286329204572646F67-     	db  '(c) Erdogan TAN - 17/11/2015'
   941 000003ED 616E2054414E202D20-
   942 000003F6 31372F31312F323031-
   943 000003FF 35                 
   944 00000400 0D0A00                  	db  0Dh, 0Ah, 0
   945                                  error_msg:
   946 00000403 0D0A07                  	db 0Dh, 0Ah, 07h
   947 00000406 2F6574632F696E6974-     	db '/etc/init error ! '
   948 0000040F 206572726F72202120 
   949 00000418 00                      	db 0
   950                                  
   951                                  getty_error_msg:
   952                                  	; 14/10/2015
   953 00000419 0D0A07                  	db 0Dh, 0Ah, 07h
   954 0000041C 2F6574632F67657474-     	db '/etc/getty error ! '
   955 00000425 79206572726F722021-
   956 0000042E 20                 
   957 0000042F 00                      	db 0
   958                                  
   959                                  	; 25/12/2021
   960                                  msg_ok:
   961 00000430 0D0A                    	db 0Dh, 0Ah
   962 00000432 4F4B2E2E2E20            	db "OK... "
   963 00000438 0D0A                    	db 0Dh, 0Ah
   964                                  sizeof_msgok equ $ - msg_ok
   965 0000043A 00                      	db 0
   966                                  
   967                                  ; 23/10/2015
   968 0000043B 90                      align 2
   969                                  zero:
   970 0000043C 00<rept>                	times 8 db 0
   971 00000444 00<rept>                	times 6 db 0
   972 0000044A 00<rept>                	times 2 db 0	 
   973                                  
   974                                  wtmp_err:
   975 0000044C 00                      	db 0
   976                                  	
   977                                  ;-----------------------------------------------------------------
   978                                  ; Original UNIX v1 - /etc/init source code (init.s)
   979                                  ;		     in PDP-11 (unix) assembly language
   980                                  ;-----------------------------------------------------------------
   981                                  
   982                                  ; / init -- process control initialization
   983                                  ;
   984                                  ;	sys	intr; 0
   985                                  ;	sys	quit; 0
   986                                  ;	sys	38. / get console switches
   987                                  ;	cmp	r0,$173030
   988                                  ;	bne	1f
   989                                  ;help:
   990                                  ;	clr	r0
   991                                  ;	sys	close
   992                                  ;	mov	$1,r0
   993                                  ;	sys	close
   994                                  ;	sys	open; ctty; 0
   995                                  ;	sys	open; ctty; 1
   996                                  ;	sys	exec; shell; shellp
   997                                  ;	br	help
   998                                  ;1:
   999                                  ;	sys	mount; rk1; usr
  1000                                  ;	sys	mount; rk2; ssys
  1001                                  ;	sys	mount; rk3; crp
  1002                                  ;	mov	$'0,r1
  1003                                  ;1:
  1004                                  ;	movb	r1,tapx+8
  1005                                  ;	sys	chmod; tapx; 17
  1006                                  ;	inc	r1
  1007                                  ;	cmp	r1,$'8
  1008                                  ;	blo	1b
  1009                                  ;	sys	creat; utmp; 16
  1010                                  ;	sys	close
  1011                                  ;	sys	unlink; dpdlock
  1012                                  ;	sys	fork
  1013                                  ;		br daemon
  1014                                  ;	sys	fork
  1015                                  ;		br dirass
  1016                                  ;	sys	fork
  1017                                  ;		br dds
  1018                                  ;	movb	$'x,zero+8.
  1019                                  ;	jsr	pc,wtmprec
  1020                                  ;	mov	$itab,r1
  1021                                  ;	br	1f
  1022                                  ;
  1023                                  ;daemon:
  1024                                  ;	sys	exec; etcdpd; etcdpdp
  1025                                  ;	sys	exit
  1026                                  ;
  1027                                  ;dirass:
  1028                                  ;	sys	chdir; usrmel
  1029                                  ;	sys	exec; melda; meldap
  1030                                  ;	sys	exit
  1031                                  ;
  1032                                  ;dds:
  1033                                  ;	sys	exec; usrdd; usrddp
  1034                                  ;	sys	exit
  1035                                  ;
  1036                                  ;/ create shell processes
  1037                                  ;
  1038                                  ;1:
  1039                                  ;	mov	(r1)+,r0
  1040                                  ;	beq	pwait
  1041                                  ;	movb	r0,ttyx+8
  1042                                  ;	jsr	pc,dfork
  1043                                  ;	mov	r0,(r1)+
  1044                                  ;	br	1b
  1045                                  ;
  1046                                  ;/ wait for process to die
  1047                                  ;
  1048                                  ;pwait:
  1049                                  ;	sys	wait
  1050                                  ;	mov	$itab,r1
  1051                                  ;
  1052                                  ;/ search for process id
  1053                                  ;
  1054                                  ;2:
  1055                                  ;	tst	(r1)+
  1056                                  ;	beq	pwait
  1057                                  ;	cmp	r0,(r1)+
  1058                                  ;	bne	2b
  1059                                  ;
  1060                                  ;/ take name out of utmp
  1061                                  ;
  1062                                  ;	sub	$4,r1
  1063                                  ;	mov	r1,-(sp)
  1064                                  ;	mov	(r1),r1
  1065                                  ;	sub	$'0,r1
  1066                                  ;	cmp	r1,$'a-'0
  1067                                  ;	blo	2f
  1068                                  ;	sub	$'a-'0-10.,r1	/ map a-z into 10. on
  1069                                  ;2:
  1070                                  ;	asl	r1
  1071                                  ;	asl	r1
  1072                                  ;	asl	r1
  1073                                  ;	asl	r1
  1074                                  ;	mov	r1,0f
  1075                                  ;	mov	$zero,r1
  1076                                  ;2:
  1077                                  ;	clr	(r1)+
  1078                                  ;	cmp	r1,$zero+16.
  1079                                  ;	blo	2b
  1080                                  ;	sys	open; utmp; 1
  1081                                  ;	bes	2f
  1082                                  ;	mov	r0,r1
  1083                                  ;	sys	seek; 0:..; 0
  1084                                  ;	mov	r1,r0
  1085                                  ;	sys	write; zero; 16.
  1086                                  ;	mov	r1,r0
  1087                                  ;	sys	close
  1088                                  ;
  1089                                  ;/ re-create user process
  1090                                  ;
  1091                                  ;2:
  1092                                  ;	mov	(sp)+,r1
  1093                                  ;	mov	(r1)+,r0
  1094                                  ;	movb	r0,ttyx+8
  1095                                  ;	movb	r0,zero+8.
  1096                                  ;	jsr	pc,wtmprec
  1097                                  ;	jsr	pc,dfork
  1098                                  ;	mov	r0,(r1)+
  1099                                  ;	br	pwait
  1100                                  ;
  1101                                  ;dfork:
  1102                                  ;	sys	fork
  1103                                  ;		br 1f
  1104                                  ;	bes	dfork
  1105                                  ;	rts	pc
  1106                                  ;1:
  1107                                  ;	sys	quit; 0
  1108                                  ;	sys	intr; 0
  1109                                  ;	sys	chown; ttyx; 0
  1110                                  ;	sys	chmod; ttyx; 15
  1111                                  ;	sys	open; ttyx; 0
  1112                                  ;	bes	help1
  1113                                  ;	sys	open; ttyx; 1
  1114                                  ;	bes	help1
  1115                                  ;	sys	exec; getty; gettyp
  1116                                  ;	sys	exit			/ HELP!
  1117                                  ;
  1118                                  ;help1:
  1119                                  ;	jmp	help
  1120                                  ;
  1121                                  ;wtmprec:
  1122                                  ;	mov	r1,-(sp)
  1123                                  ;	sys	time
  1124                                  ;	mov	r0,zero+10.
  1125                                  ;	mov	r1,zero+12.
  1126                                  ;	sys	open; wtmp; 1
  1127                                  ;	bes	2f
  1128                                  ;	mov	r0,r2
  1129                                  ;	sys	seek; 0; 2
  1130                                  ;	mov	r2,r0
  1131                                  ;	sys	write; zero; 16.
  1132                                  ;	mov	r2,r0
  1133                                  ;	sys	close
  1134                                  ;2:
  1135                                  ;	mov	(sp)+,r1
  1136                                  ;	rts	pc
  1137                                  ;
  1138                                  ;etcdpdp:
  1139                                  ;	etcdpd; 0
  1140                                  ;meldap:
  1141                                  ;	melda; 0
  1142                                  ;usrddp:
  1143                                  ;	usrdd; 0
  1144                                  ;usrdd:	</usr/demo/dds\0>
  1145                                  ;melda:	</usr/mel/da\0>
  1146                                  ;usrmel:</usr/mel\0>
  1147                                  ;rk1:	</dev/rk1\0>
  1148                                  ;rk2:	</dev/rk2\0>
  1149                                  ;rk3:	</dev/rk3\0>
  1150                                  ;usr:	</usr\0>
  1151                                  ;ssys:	</sys\0>
  1152                                  ;crp:	</crp\0>
  1153                                  ;ctty:	</dev/tty\0>
  1154                                  ;shell:	</bin/sh\0>
  1155                                  ;shellm:<-\0>
  1156                                  ;dpdlock:
  1157                                  ;	</usr/dpd/lock\0>
  1158                                  ;etcdpd:
  1159                                  ;	</etc/dpd\0>
  1160                                  ;tapx:	</dev/tapx\0>
  1161                                  ;utmp:	</tmp/utmp\0>
  1162                                  ;wtmp:	</tmp/wtmp\0>
  1163                                  ;ttyx:	</dev/ttyx\0>
  1164                                  ;getty:	</etc/getty\0>
  1165                                  ;	.even
  1166                                  ;
  1167                                  ;shellp:shellm
  1168                                  ;	0
  1169                                  ;gettyp:getty
  1170                                  ;	0
  1171                                  ;itab:
  1172                                  ;	'0; ..
  1173                                  ;	'1; ..
  1174                                  ;	'2; ..
  1175                                  ;	'3; ..
  1176                                  ;	'4; ..
  1177                                  ;	'5; ..
  1178                                  ;	'6; ..
  1179                                  ;	'7; ..
  1180                                  ;	'8; ..
  1181                                  ;	'a; ..
  1182                                  ;	'b; ..
  1183                                  ;	 0
  1184                                  ;
  1185                                  ;	.bss
  1186                                  ;offset:.=.+2
  1187                                  ;zero:	.=.+8.; .=.+6; .=.+2.
