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