     1                                  ; ****************************************************************************
     2                                  ; passwd8086.s (passwd0.s) - by Erdogan Tan - 30/04/2022
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Retro UNIX 8086 v1 - passwd - change user's password
     5                                  ;
     6                                  ; [ Last Modification: 01/05/2022 ]
     7                                  ;
     8                                  ; Derived from (original) UNIX v5 'passwd.s' source Code
     9                                  ; Ref:
    10                                  ; www.tuhs.org (https://minnie.tuhs.org)
    11                                  ; v5root.tar.gz
    12                                  ; ****************************************************************************
    13                                  ; [ usr/source/s2/passwd.s (archive date: 27-11-1974) ]
    14                                  
    15                                  ; passwd0.s - Retro UNIX 8086 v1 (16 bit version of 'passwd1.s')
    16                                  ; passwd1.s - Retro UNIX 386 v1 & v1.1 (unix v1 inode structure)
    17                                  ; passwd2.s - Retro UNIX 386 v1.2 (& v2) (modified unix v7 inode)
    18                                  
    19                                  ; UNIX v1 system calls
    20                                  _rele 	equ 0
    21                                  _exit 	equ 1
    22                                  _fork 	equ 2
    23                                  _read 	equ 3
    24                                  _write	equ 4
    25                                  _open	equ 5
    26                                  _close 	equ 6
    27                                  _wait 	equ 7
    28                                  _creat 	equ 8
    29                                  _link 	equ 9
    30                                  _unlink	equ 10
    31                                  _exec	equ 11
    32                                  _chdir	equ 12
    33                                  _time 	equ 13
    34                                  _mkdir 	equ 14
    35                                  _chmod	equ 15
    36                                  _chown	equ 16
    37                                  _break	equ 17
    38                                  _stat	equ 18
    39                                  _seek	equ 19
    40                                  _tell 	equ 20
    41                                  _mount	equ 21
    42                                  _umount	equ 22
    43                                  _setuid	equ 23
    44                                  _getuid	equ 24
    45                                  _stime	equ 25
    46                                  _quit	equ 26	
    47                                  _intr	equ 27
    48                                  _fstat	equ 28
    49                                  _emt 	equ 29
    50                                  _mdate 	equ 30
    51                                  _stty 	equ 31
    52                                  _gtty	equ 32
    53                                  _ilgins	equ 33
    54                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    55                                  _msg    equ 35 ; Retro UNIX 386 v1 feature only !
    56                                  
    57                                  ;;;
    58                                  ESCKey equ 1Bh
    59                                  EnterKey equ 0Dh
    60                                  
    61                                  ;%macro sys 1-4
    62                                  ;   ; 03/09/2015	
    63                                  ;   ; 13/04/2015
    64                                  ;   ; Retro UNIX 386 v1 system call.
    65                                  ;   %if %0 >= 2   
    66                                  ;       mov ebx, %2
    67                                  ;       %if %0 >= 3    
    68                                  ;           mov ecx, %3
    69                                  ;           ;%if %0 = 4
    70                                  ;           %if	%0 >= 4 ; 11/03/2022
    71                                  ;		mov edx, %4   
    72                                  ;           %endif
    73                                  ;       %endif
    74                                  ;   %endif
    75                                  ;   mov eax, %1
    76                                  ;   int 30h	   
    77                                  ;%endmacro
    78                                  
    79                                  %macro sys 1-4
    80                                      ; Retro UNIX 8086 v1 system call.
    81                                      %if %0 >= 2   
    82                                          mov bx, %2
    83                                          %if %0 >= 3
    84                                              mov cx, %3
    85                                              %if %0 >= 4
    86                                                 mov dx, %4
    87                                              %endif
    88                                          %endif
    89                                      %endif
    90                                      mov ax, %1
    91                                      int 20h
    92                                  %endmacro
    93                                  
    94                                  ;; Retro UNIX 386 v1 system call format:
    95                                  ;; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
    96                                  
    97                                  ;; 11/03/2022
    98                                  ;; Note: Above 'sys' macro has limitation about register positions;
    99                                  ;;	ebx, ecx, edx registers must not be used after their
   100                                  ;;	positions in sys macro.
   101                                  ;; for example:
   102                                  ;;	'sys _write, 1, msg, ecx' is defective, because
   103                                  ;;	 ecx will be used/assigned before edx in 'sys' macro.
   104                                  ;; correct order may be:
   105                                  ;;	'sys _write, 1, msg, eax ; (eax = byte count)
   106                                  
   107                                  ; Retro UNIX 8086 v1 system call format:
   108                                  ; sys systemcall (ax) <arg1 (bx)>, <arg2 (cx)>, <arg3 (dx)>
   109                                  
   110                                  ; ----------------------------------------------------------------------------
   111                                  
   112                                  [BITS 16] ; 16-bit intructions (8086/8088 - Real Mode)
   113                                  
   114                                  [ORG 0] 
   115                                  
   116                                  START_CODE:
   117                                  	; 01/05/2022 - Retro UNIX 8086 v1
   118                                  	;	32 bit to 16 bit conversion
   119                                  	;	-----
   120                                  	;	eax, edx, ecx, ebx -> ax, dx, cx, bx
   121                                  	;	esi, edi, ebp, esp -> si, di, bp, sp
   122                                  	;	register+4 -> register+2
   123                                  	;	dword values on stack -> word values on stack
   124                                  	;
   125                                  	; 01/05/2022
   126                                  	; 30/04/2022
   127                                  
   128                                  	;cmp	(sp)+,$3
   129                                  	;bge	1f
   130                                  	;jsr	r5,mesg
   131                                  	;	<Usage: passwd uid password\n\0>; .even
   132                                  	;sys	exit
   133                                  
   134 00000000 58                      	pop	ax ; ax = number of arguments
   135                                  
   136                                  	;cmp	ax, 3
   137 00000001 3C03                    	cmp	al, 3
   138 00000003 730B                    	jnb	short pswd_1
   139                                  
   140 00000005 B8[5A03]                	mov	ax, usage_msg
   141 00000008 E89E01                  	call	print_msg
   142                                  exit:
   143                                  	sys	_exit
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82                              <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84                              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 0000000B B80100              <1>  mov ax, %1
    91 0000000E CD20                <1>  int 20h
   144                                  ;hang:
   145                                  ;	nop
   146                                  ;	jmp	short hang
   147                                  
   148                                  pswd_1:
   149                                  ;1:
   150                                  	;tst	(sp)+
   151                                  	;mov	(sp)+,uidp
   152                                  	;mov	(sp)+,r0
   153                                  	;tstb	(r0)
   154                                  	;beq	1f
   155                                  	;jsr	pc,crypt
   156                                  	;clrb	8(r0)
   157                                  
   158 00000010 58                      	pop	ax ; argument 0 - binary file name
   159                                  
   160 00000011 8F06[F40C]              	pop	word [uidp] ; argument 1 - user id
   161 00000015 5E                      	pop	si ; argument 2 - password
   162                                  
   163                                  	; 01/05/2022
   164 00000016 E8AC01                  	call	strlen 
   165 00000019 3C08                    	cmp	al, 8
   166 0000001B 7723                    	ja	short max_8_chars
   167                                  
   168 0000001D E8AD01                  	call	crypt
   169                                  	; si = encyrpted password address
   170 00000020 C6440800                	mov	byte [si+8], 0
   171                                  pswd_2:
   172                                  ;1:
   173                                  	;mov	r0,cryptp
   174                                  	;mov	$passwf,r0
   175                                  	;jsr	r5,fopen; ibuf
   176                                  	;bec	1f
   177                                  	;jsr	r5,mesg
   178                                  	;	<cannot open password file\n\0>; .even
   179                                  	;sys	exit
   180                                  
   181 00000024 8936[F20C]              	mov	[cryptp], si
   182                                  	
   183                                  	sys	_open, passwf, 0
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82 00000028 BB[4403]            <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84 0000002B B90000              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 0000002E B80500              <1>  mov ax, %1
    91 00000031 CD20                <1>  int 20h
   184 00000033 7310                    	jnc	short pswd_3
   185                                  
   186 00000035 B8[7903]                	mov	ax, cant_open_msg
   187                                  write_msg_and_exit:
   188 00000038 E86E01                  	call	print_msg
   189                                  	sys	_exit
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82                              <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84                              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 0000003B B80100              <1>  mov ax, %1
    91 0000003E CD20                <1>  int 20h
   190                                  
   191                                  ;hangemhigh:
   192                                  ;	nop
   193                                  ;	jmp	short hangemhigh
   194                                  
   195                                  max_8_chars:
   196 00000040 B8[3D04]                	mov	ax, long_pswd_msg
   197 00000043 EBF3                    	jmp	short write_msg_and_exit
   198                                  
   199                                  pswd_3:
   200                                  ;1:
   201                                  	; 01/05/2022 (16 bit code)
   202                                  	; 30/04/2022
   203 00000045 A3[E608]                	mov	[ibuf], ax  ; file descriptor
   204                                  pswd_4:
   205                                  	;sys	stat; tempf; obuf+20.
   206                                  	;bec	2f
   207                                  	;sys	creat; tempf; 222
   208                                  	;bec	1f
   209                                  
   210                                  	sys	_stat, tempf, obuf+20
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82 00000048 BB[5003]            <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84 0000004B B9[000B]            <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 0000004E B81200              <1>  mov ax, %1
    91 00000051 CD20                <1>  int 20h
   211 00000053 730D                    	jnc	short pswd_5
   212                                  
   213                                  	; set write permission only
   214                                  	sys	_creat, tempf, 101b ; unix v1 inode
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82 00000055 BB[5003]            <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84 00000058 B90500              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 0000005B B80800              <1>  mov ax, %1
    91 0000005E CD20                <1>  int 20h
   215                                  	;sys	_creat, tempf, 110010010b ; runix v2 inode
   216 00000060 7305                    	jnc	short pswd_6
   217                                  pswd_5:
   218                                  ;2:
   219                                  	;jsr	r5,mesg
   220                                  	;	<temp file busy -- try again\n\0>; .even
   221                                  	;sys	exit
   222                                  
   223 00000062 B8[9703]                	mov	ax, tmpf_bsy_msg
   224 00000065 EBD1                    	jmp	short write_msg_and_exit
   225                                  
   226                                  pswd_6:
   227                                  ;1:
   228                                  	;mov	r0,obuf
   229                                  
   230                                  	; 01/05/2022
   231                                  	; (*) Retro UNIX 8086 v1 kernel has unknown
   232                                  	; -for now- bug, prevents correct read write
   233                                  	; (same file) just after creating a file.
   234                                  	; Lets close and open tempf file again
   235                                  	; as temporary solution to problem.
   236                                  	;
   237                                  	sys	_close, ax ; (*)
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82 00000067 89C3                <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84                              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 00000069 B80600              <1>  mov ax, %1
    91 0000006C CD20                <1>  int 20h
   238                                  	sys	_open, tempf, 1 ; (*) ; open for write
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82 0000006E BB[5003]            <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84 00000071 B90100              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 00000074 B80500              <1>  mov ax, %1
    91 00000077 CD20                <1>  int 20h
   239 00000079 72E7                    	jc	short pswd_5
   240                                  	;
   241                                  	
   242 0000007B A3[EC0A]                	mov	[obuf], ax ; file descriptor
   243                                  
   244                                  ; / search for uid
   245                                  
   246                                  comp:
   247                                  	;mov	uidp,r1
   248 0000007E 8B36[F40C]              	mov	si, [uidp] ; 01/05/2022
   249                                  cmp_1:
   250                                  ;1:
   251                                  	;jsr	pc,pcop
   252                                  	;cmp	r0,$':
   253                                  	;beq	1f
   254                                  	;cmpb	r0,(r1)+
   255                                  	;beq	1b
   256                                  
   257 00000082 E80B01                  	call	pcop
   258 00000085 3C3A                    	cmp	al, ':'
   259 00000087 7413                    	je	short cmp_3
   260 00000089 88C4                    	mov	ah, al
   261 0000008B AC                      	lodsb
   262 0000008C 38E0                    	cmp	al, ah
   263 0000008E 74F2                    	je	short cmp_1
   264                                  cmp_2:
   265                                  ;2:
   266                                  	;jsr	pc,pcop
   267                                  	;cmp	r0,$'\n
   268                                  	;bne	2b
   269                                  	;br	comp
   270                                  
   271 00000090 E8FD00                  	call	pcop
   272                                  	; (skip remain bytes on row, get next line/row)
   273                                  	; check cr byte of crlf (end of line chars)
   274 00000093 3C0D                    	cmp	al, EnterKey  ; cmp al, 0Dh
   275 00000095 75F9                    	jne	short cmp_2
   276                                  	; get lf byte of crlf out
   277 00000097 E8F600                  	call	pcop
   278 0000009A EBE2                    	jmp	short comp ; next line
   279                                  	
   280                                  cmp_3:
   281                                  ;1:
   282                                  	;tstb	(r1)+
   283                                  	;bne	2b
   284                                  
   285                                  	; check end of uid input (match condition)
   286 0000009C AC                      	lodsb	
   287 0000009D 08C0                    	or	al, al
   288 0000009F 75EF                    	jnz	short cmp_2 ; uid is not same
   289                                  			; skip remain bytes on line/row
   290                                  
   291                                  	; uid (input) matches with uid in passwd file 
   292                                  	
   293                                  ; / skip over old password			
   294                                  
   295                                  pswd_7:
   296                                  ;1:
   297                                  	;jsr	pc,pget
   298                                  	;cmp	r0,$':
   299                                  	;bne	1b
   300                                  
   301 000000A1 E8DD00                  	call	pget
   302 000000A4 3C3A                    	cmp	al, ':'
   303 000000A6 75F9                    	jne	short pswd_7
   304                                  
   305                                  ; / copy in new password
   306                                  
   307                                  	;mov	cryptp,r1	
   308 000000A8 8B36[F20C]              	mov	si, [cryptp] ; ptr to encyrpted passwd
   309                                  pswd_8:
   310                                  ;1:	
   311                                  	;movb	(r1)+,r0
   312                                  	;beq	1f
   313                                  	;jsr	pc,pput
   314                                  	;br	1b
   315                                  
   316 000000AC AC                      	lodsb
   317 000000AD 20C0                    	and	al, al
   318 000000AF 7405                    	jz	short pswd_9
   319 000000B1 E8CA00                  	call	pput
   320 000000B4 EBF6                    	jmp	short pswd_8
   321                                  pswd_9:
   322                                  ;1:
   323                                  	;mov	$':,r0
   324                                  	;jsr	pc,pput
   325                                  
   326 000000B6 B03A                    	mov	al, ':'
   327 000000B8 E8C300                  	call	pput
   328                                  
   329                                  ; / validate permission
   330                                  
   331                                  	;clr	r1
   332 000000BB 29C9                    	sub	cx, cx ; 0
   333 000000BD BF0A00                  	mov	di, 10
   334                                  pswd_10:	
   335                                  ;1:
   336                                  	;jsr	pc,pcop
   337                                  	;cmp	r0,$':
   338                                  	;beq	1f
   339                                  	;mpy	$10.,r1
   340                                  	;sub	$'0,r0
   341                                  	;add	r0,r1
   342                                  	;br	1b
   343                                  
   344 000000C0 51                      	push	cx
   345 000000C1 E8CC00                  	call	pcop
   346 000000C4 59                      	pop	cx
   347                                  	; (ax <= 255)
   348 000000C5 3C3A                    	cmp	al, ':'
   349 000000C7 740A                    	je	short pswd_11
   350 000000C9 91                      	xchg	ax, cx
   351 000000CA F7E7                    	mul	di ; * 10
   352 000000CC 80E930                  	sub	cl, '0'
   353 000000CF 01C1                    	add	cx, ax
   354 000000D1 EBED                    	jmp	short pswd_10	
   355                                  
   356                                  pswd_11:
   357                                  ;1:
   358                                  	;sys	getuid
   359                                  	;tst	r0
   360                                  	;beq	1f
   361                                  
   362                                  	; cx = uid (as in passwd file)
   363                                  
   364                                  	sys	_getuid
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82                              <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84                              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 000000D3 B81800              <1>  mov ax, %1
    91 000000D6 CD20                <1>  int 20h
   365                                  
   366                                  	;or	al, al
   367 000000D8 09C0                    	or	ax, ax
   368 000000DA 740C                    	jz	short pswd_12 ; root (superuser)	
   369                                  
   370                                  	;cmp	r0,r1
   371                                  	;beq	1f
   372                                  	
   373                                  	;cmp	cl, al
   374 000000DC 39C1                    	cmp	cx, ax
   375 000000DE 7408                    	je	short pswd_12
   376                                  
   377                                  	;jsr	r5,mesg
   378                                  	;	<permission denied\n\0>; .even
   379                                  	;br	done
   380                                  
   381 000000E0 B8[B703]                	mov	ax, p_denied_msg
   382 000000E3 E8C300                  	call	print_msg
   383 000000E6 EB09                    	jmp	short done
   384                                  
   385                                  pswd_12:
   386                                  ;1:
   387                                  	;inc	sflg
   388                                  
   389                                  	; set 1st stage (cmpleted) flag
   390 000000E8 FE06[F60C]              	inc	byte [sflg] ; 1st stage is ok
   391                                  pswd_13:
   392                                  ;1:
   393                                  	;jsr	pc,pcop
   394                                  	;br	1b
   395                                  	
   396 000000EC E8A100                  	call	pcop
   397                                  	
   398                                  	; pcop will return/jump to 'done'
   399                                  	; after the last byte of (old) passwd file
   400                                  	; (call return address will be discarded)
   401                                  	
   402                                  	; (but if there is a next byte to read/write
   403                                  	;  cpu will return here)
   404                                  	
   405 000000EF EBFB                    	jmp	short pswd_13 ; r/w next byte
   406                                  
   407                                  ; ---------------------------
   408                                  
   409                                  	; 01/05/2022 (16 bit code)
   410                                  done:
   411                                  	;jsr	r5,flush; obuf
   412                                  	;mov	obuf,r0
   413                                  	;sys	close
   414                                  	
   415                                  	;mov	bx, obuf
   416 000000F1 E8FE01                  	call	flush 
   417                                  		; (write buffer content to disk)
   418 000000F4 8B1E[EC0A]              	mov	bx, [obuf] 
   419                                  	sys	_close ; (close output file)
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82                              <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84                              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 000000F8 B80600              <1>  mov ax, %1
    91 000000FB CD20                <1>  int 20h
   420                                  
   421                                  	;mov	ibuf,r0
   422                                  	;sys	close
   423                                  	
   424 000000FD 8B1E[E608]              	mov	bx, [ibuf]
   425                                  	sys	_close ; (close input file)
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82                              <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84                              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 00000101 B80600              <1>  mov ax, %1
    91 00000104 CD20                <1>  int 20h
   426                                  
   427                                  	;tst	sflg
   428                                  	;beq	1f
   429                                  	;tst	dflg
   430                                  	;bne	1f
   431                                  	;inc	dflg
   432                                  
   433 00000106 F606[F60C]FF            	test	byte [sflg], 0FFh
   434 0000010B 7464                    	jz	short done_4 ; 1st stage failed 
   435                                  			     ; unlink/remove tempf
   436                                  	; 1st stage is ok
   437 0000010D F606[F70C]FF            	test	byte [dflg], 0FFh
   438 00000112 755D                    	jnz	short done_4 ; 2nd stage is ok (completed)
   439                                  	
   440                                  	; 2nd stage
   441                                  	; (writing to tempf at 1st stage is ok)	
   442 00000114 FE06[F70C]              	inc	byte [dflg]  ; set 2nd stage flag 
   443                                  			     ; (open tempf for read and
   444                                  			     ;  write to new passwd file)
   445                                  	;mov	$tempf,r0
   446                                  	;jsr	r5,fopen; ibuf
   447                                  	;bec	2f
   448                                  
   449                                  	sys	_open, tempf, 0 ; open tempf for read
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82 00000118 BB[5003]            <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84 0000011B B90000              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 0000011E B80500              <1>  mov ax, %1
    91 00000121 CD20                <1>  int 20h
   450 00000123 7308                    	jnc	short done_1		
   451                                  
   452                                  	;jsr	r5,mesg
   453                                  	;	<cannot reopen temp file\n\0>; .even
   454                                  	;br	1f
   455                                  
   456 00000125 B8[CD03]                	mov	ax, cnro_tmpf_msg
   457 00000128 E87E00                  	call	print_msg
   458 0000012B EB44                    	jmp	short done_4
   459                                  done_1:
   460                                  ;2:
   461 0000012D A3[E608]                	mov	[ibuf], ax
   462                                  	; 04/05/2022
   463 00000130 31C0                    	xor	ax, ax ; 0
   464 00000132 A3[E808]                	mov	[ibuf+2], ax
   465 00000135 A3[EA08]                	mov	[ibuf+4], ax
   466                                  
   467                                  	;mov	$passwf,r0
   468                                  	;jsr	r5,fcreat; obuf
   469                                  	;bec	2f
   470                                  
   471                                  	; retro unix v1 inode
   472                                  	sys	_creat, passwf, 1100b ; rw--
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82 00000138 BB[4403]            <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84 0000013B B90C00              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 0000013E B80800              <1>  mov ax, %1
    91 00000141 CD20                <1>  int 20h
   473                                  	; retro unix v2 inode
   474                                  	;sys	_creat, passwf, 110000000b ; rw-------
   475 00000143 7308                    	jnc	short done_2
   476                                  done_5:
   477                                  	;jsr	r5,mesg
   478                                  	;	<cannot reopen password file\n\0>; .even
   479                                  	;br	1f
   480                                  
   481 00000145 B8[E903]                	mov	ax, cnro_pswdf_msg
   482 00000148 E85E00                  	call	print_msg
   483 0000014B EB24                    	jmp	short done_4
   484                                  done_2:
   485                                  ;2:
   486                                  	;jsr	pc,pcop
   487                                  	;br	2b
   488                                  
   489                                  	; 01/05/2022
   490                                  	; (*) Note: Retro UNIX 8086 v1 kernel has unknown
   491                                  	; -for now- bug, prevents correct read write
   492                                  	; (same file) just after creating a file.
   493                                  	; Lets close and open passwf file again
   494                                  	; as temporary solution to problem.
   495                                  	;
   496                                  	sys	_close, ax ; (*)
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82 0000014D 89C3                <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84                              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 0000014F B80600              <1>  mov ax, %1
    91 00000152 CD20                <1>  int 20h
   497                                  	sys	_open, passwf, 1 ; (*) ; open for write
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82 00000154 BB[4403]            <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84 00000157 B90100              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 0000015A B80500              <1>  mov ax, %1
    91 0000015D CD20                <1>  int 20h
   498 0000015F 72E4                    	jc	short done_5
   499                                  	;
   500                                  
   501 00000161 A3[EC0A]                	mov	[obuf], ax
   502 00000164 29C0                    	sub	ax, ax ; 0
   503                                  	; 01/05/2022
   504 00000166 A3[EE0A]                	mov	[obuf+2], ax
   505 00000169 A3[F00A]                	mov	[obuf+4], ax
   506                                  done_3:	; 01/05/2022
   507 0000016C E82100                  	call	pcop
   508 0000016F EBFB                    	jmp	short done_3
   509                                  
   510                                  done_4:
   511                                  ;1:
   512                                  	;sys	unlink; tempf
   513                                  	;sys	exit
   514                                  
   515                                  	sys	_unlink, tempf
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82 00000171 BB[5003]            <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84                              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 00000174 B80A00              <1>  mov ax, %1
    91 00000177 CD20                <1>  int 20h
   516                                  	sys	_exit
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82                              <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84                              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 00000179 B80100              <1>  mov ax, %1
    91 0000017C CD20                <1>  int 20h
   517                                  
   518                                  ; ---------------------------
   519                                  
   520                                  pput:
   521                                  	;jsr	r5,putc; obuf
   522                                  	;rts	pc
   523                                  
   524                                  	;mov	bx, obuf
   525                                  	;call	putc
   526                                  	;retn
   527 0000017E E95901                  	jmp	putc
   528                                  
   529                                  ; ---------------------------
   530                                  
   531                                  pget:
   532                                  	;jsr	r5,getc; ibuf
   533                                  	;bes	1f
   534                                  	;rts	pc
   535                                  
   536                                  	;mov	bx, ibuf
   537 00000181 E82201                  	call	getc
   538 00000184 7201                    	jc	short pget_1
   539 00000186 C3                      	retn
   540                                  pget_1:
   541                                  ;1:
   542                                  	;jsr	r5,mesg
   543                                  	;	<format error on password file\n\0>; .even
   544                                  	;br	done
   545                                  
   546 00000187 B8[0904]                	mov	ax, format_err_msg
   547 0000018A E81C00                  	call	print_msg
   548                                  
   549 0000018D E961FF                  	jmp	done
   550                                  
   551                                  ; ---------------------------
   552                                  
   553                                  	; 01/05/2022 (16 bit code)
   554                                  	; 30/04/2022
   555                                  pcop:
   556                                  	;jsr	r5,getc; ibuf
   557                                  	;bes	1f
   558                                  	;jsr	r5,putc; obuf
   559                                  	;rts	pc
   560                                  
   561                                  	;mov	bx, ibuf
   562 00000190 E81301                  	call	getc
   563 00000193 7203                    	jc	short pcop_1
   564                                  	;mov	bx, obuf
   565                                  	;call	putc
   566                                  	;retn
   567 00000195 E94201                  	jmp	putc
   568                                  pcop_1:
   569                                  ;1:
   570                                  	;tst	sflg
   571                                  	;bne	1f
   572                                  	;jsr	r5,mesg
   573                                  	;	<uid not valid\n\0>; .even
   574                                  
   575 00000198 F606[F60C]FF            	test	byte [sflg], 0FFh
   576 0000019D 7506                    	jnz	short pcop_2
   577                                  
   578 0000019F B8[2B04]                	mov	ax, not_valid_msg
   579 000001A2 E80400                  	call	print_msg
   580                                  pcop_2:
   581                                  ;1:
   582                                  	;br	done
   583 000001A5 58                      	pop	ax ; discard call return addr
   584 000001A6 E948FF                  	jmp	done
   585                                  
   586                                  ; ---------------------------
   587                                  
   588                                  print_msg:
   589                                  	; 01/05/2022
   590                                  	; 29/04/2022 
   591                                  	; Modified registers: ax, bx, cx, dx
   592                                  
   593 000001A9 E80D00                  	call	_strlen ; 01/05/2022
   594                                  print_str:
   595 000001AC 89DA                    	mov	dx, bx
   596                                  	sys	_write, 1, ax
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82 000001AE BB0100              <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84 000001B1 89C1                <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 000001B3 B80400              <1>  mov ax, %1
    91 000001B6 CD20                <1>  int 20h
   597                                  	;
   598 000001B8 C3                      	retn
   599                                  
   600                                  	; 01/05/2022
   601                                  	; 29/04/2022
   602                                  _strlen:
   603                                  	; ax = asciiz string address
   604 000001B9 89C3                    	mov	bx, ax
   605 000001BB 4B                      	dec	bx
   606                                  nextchr:
   607 000001BC 43                      	inc	bx
   608 000001BD 803F00                  	cmp	byte [bx], 0
   609 000001C0 77FA                    	ja	short nextchr
   610                                  	;cmp	[bx], 0Dh
   611                                  	;ja	short nextchr
   612 000001C2 29C3                    	sub	bx, ax
   613                                  	; bx = asciiz string length
   614 000001C4 C3                      	retn
   615                                  
   616                                  	; 01/05/2022
   617                                  strlen:
   618 000001C5 89F0                    	mov	ax, si
   619 000001C7 E8EFFF                  	call	_strlen
   620 000001CA 89D8                    	mov	ax, bx
   621 000001CC C3                      	retn
   622                                  
   623                                  ; ---------------------------------------------------
   624                                  ; 01/05/2022
   625                                  ; 'crypt' assembly source code
   626                                  ; copied from: 'login04.asm' (Erdogan Tan, 31/1/2022)
   627                                  ; ---------------------------------------------------
   628                                   
   629                                  ;/ crypt -- password incoding
   630                                  ;
   631                                  ;; Original Unix v5 (PDP-11) 'crypt'
   632                                  ;; code has been converted to 
   633                                  ;; Retro UNIX 8086 v1 'crypt' 
   634                                  ;; procedure in 'login.asm'
   635                                  ;; (by Erdogan Tan - 12/11/2013).
   636                                  ; 
   637                                  ;
   638                                  ;crypt:
   639                                  ;	mov	r1,-(sp)
   640                                  ;	mov	r2,-(sp)
   641                                  ;	mov	r3,-(sp)
   642                                  ;	mov	r4,-(sp)
   643                                  ;	mov	r5,-(sp)
   644                                  ;
   645                                  ;	mov	r0,r1
   646                                  ;	mov	$key,r0
   647                                  ;	movb	$004,(r0)+
   648                                  ;	movb	$034,(r0)+
   649                                  
   650                                  crypt:
   651                                  	;mov	si, passwd
   652                                  	; 31/01/2022
   653 000001CD BF[5C04]                	mov	di, key
   654 000001D0 B004                    	mov	al, 4
   655 000001D2 AA                      	stosb
   656 000001D3 B01C                    	mov	al, 28
   657 000001D5 AA                      	stosb
   658                                  
   659                                  ;1:
   660                                  ;	cmp	r0,$key+64.
   661                                  ;	bhis	1f
   662                                  ;	movb	(r1)+,(r0)+
   663                                  ;	bne	1b
   664                                  ;1:
   665                                  ;	dec	r0
   666                                  
   667                                  cryp0:
   668 000001D6 AC                      	lodsb
   669 000001D7 AA                      	stosb
   670 000001D8 20C0                    	and	al, al
   671 000001DA 7406                    	jz	short cryp1
   672                                  	; 31/01/2022
   673 000001DC 81FF[9C04]              	cmp	di, key+64
   674 000001E0 72F4                    	jb	short cryp0
   675                                  cryp1:
   676 000001E2 4F                       	dec	di
   677                                  
   678                                  ;/
   679                                  ;/	fill out key space with clever junk
   680                                  ;/
   681                                  ;	mov	$key,r1
   682                                  ;1:
   683                                  ;	movb	-1(r0),r2
   684                                  ;	movb	(r1)+,r3
   685                                  ;	xor	r3,r2
   686                                  ;	movb	r2,(r0)+
   687                                  ;	cmp	r0,$key+128.
   688                                  ;	blo	1b
   689                                  
   690                                  ;/	fill out key space with clever junk
   691                                  
   692                                  	; 31/01/2022
   693 000001E3 BE[5C04]                	mov	si, key
   694                                  cryp2:
   695 000001E6 8A5DFF                  	mov	bl, [di-1]
   696 000001E9 AC                      	lodsb
   697 000001EA 30D8                    	xor	al, bl
   698 000001EC AA                      	stosb
   699                                  	; 31/01/2022
   700 000001ED 81FF[DC04]              	cmp	di, key+128
   701 000001F1 72F3                    	jb	short cryp2
   702                                  
   703                                  ;/
   704                                  ;/	establish wheel codes and cage codes
   705                                  ;/
   706                                  ;	mov	$wheelcode,r4
   707                                  ;	mov	$cagecode,r5
   708                                  ;	mov	$256.,-(sp)
   709                                  ;2:
   710                                  ;	clr	r2
   711                                  ;	clr	(r4)
   712                                  ;	mov	$wheeldiv,r3
   713                                  ;3:
   714                                  ;	clr	r0
   715                                  ;	mov	(sp),r1
   716                                  ;	div	(r3)+,r0
   717                                  ;	add	r1,r2
   718                                  ;	bic	$40,r2
   719                                  ;	bis	shift(r2),(r4)
   720                                  ;	cmp	r3,$wheeldiv+6.
   721                                  ;	bhis	4f
   722                                  ;	bis	shift+4(r2),(r5)
   723                                  ;4:
   724                                  ;	cmp	r3,$wheeldiv+10.
   725                                  ;	blo	3b
   726                                  ;	sub	$2,(sp)
   727                                  ;	tst	(r4)+
   728                                  ;	tst	(r5)+
   729                                  ;	cmp	r4,$wheelcode+256.
   730                                  ;	blo	2b
   731                                  ;	tst	(sp)+
   732                                  ;/	
   733                                  
   734                                  ;/	establish wheel codes and cage codes
   735                                  
   736                                  	; 31/01/2022
   737 000001F3 BE[E607]                	mov	si, wheelcode
   738 000001F6 BF[E606]                	mov	di, cagecode
   739 000001F9 B80001                  	mov	ax, 256
   740 000001FC 50                      	push	ax ; *
   741 000001FD 89E5                    	mov	bp, sp
   742                                  cryp3:
   743 000001FF 29D2                    	sub	dx, dx ; 0
   744 00000201 8914                    	mov	[si], dx ; 0
   745 00000203 BB[3F03]                	mov	bx, wheeldiv
   746                                  cryp4:
   747 00000206 8B4600                  	mov	ax, [bp]	
   748 00000209 8A0F                    	mov 	cl, [bx]
   749 0000020B F6F1                    	div	cl
   750 0000020D 00E2                    	add	dl, ah
   751 0000020F 43                      	inc	bx
   752 00000210 80E21F                  	and	dl, 01Fh
   753 00000213 53                      	push	bx
   754 00000214 BB[1B03]                	mov	bx, shift
   755 00000217 01D3                    	add	bx, dx
   756 00000219 8B07                    	mov	ax, [bx] 
   757 0000021B 0904                    	or	[si], ax
   758 0000021D 59                      	pop	cx
   759 0000021E 81F9[4203]              	cmp	cx, wheeldiv+3
   760 00000222 7307                    	jnb	short cryp5
   761 00000224 83C304                  	add	bx, 4
   762 00000227 8B07                    	mov	ax, [bx]
   763 00000229 0905                    	or	[di], ax 	 
   764                                  cryp5:
   765 0000022B 89CB                    	mov	bx, cx
   766 0000022D 81FB[4403]              	cmp	bx, wheeldiv+5
   767 00000231 72D3                    	jb	short cryp4
   768 00000233 836E0002                	sub	word [bp], 2
   769 00000237 AD                      	lodsw
   770 00000238 47                      	inc	di
   771 00000239 47                      	inc	di
   772                                  	; 31/01/2022
   773 0000023A 81FE[E608]              	cmp	si, wheelcode+256
   774 0000023E 72BF                    	jb	short cryp3
   775 00000240 58                      	pop	ax ; *
   776                                  
   777                                  ;	.data
   778                                  ;shift:	1;2;4;10;20;40;100;200;400;1000;2000;4000;10000;20000;40000;100000
   779                                  ;	1;2
   780                                  ;wheeldiv: 32.; 18.; 10.; 6.; 4.
   781                                  ;	.bss
   782                                  ;cagecode: .=.+256.
   783                                  ;wheelcode: .=.+256.
   784                                  ;	.text
   785                                  ;/
   786                                  ;/
   787                                  ;/	make the internal settings of the machine
   788                                  ;/	both the lugs on the 128 cage bars and the lugs
   789                                  ;/	on the 16 wheels are set from the expanded key
   790                                  ;/
   791                                  ;	mov	$key,r0
   792                                  ;	mov	$cage,r2
   793                                  ;	mov	$wheel,r3
   794                                  ;1:
   795                                  ;	movb	(r0)+,r1
   796                                  ;	bic	$!177,r1
   797                                  ;	asl	r1
   798                                  ;	mov	cagecode(r1),(r2)+
   799                                  ;	mov	wheelcode(r1),(r3)+
   800                                  ;	cmp	r0,$key+128.
   801                                  ;	blo	1b
   802                                  
   803                                  ;/	make the internal settings of the machine
   804                                  ;/	both the lugs on the 128 cage bars and the lugs
   805                                  ;/	on the 16 wheels are set from the expanded key
   806                                  
   807                                  cryp6:
   808                                  	; 31/01/2022
   809 00000241 BB[5C04]                	mov	bx, key
   810 00000244 BE[E604]                	mov	si, cage
   811 00000247 BF[E605]                	mov	di, wheel
   812                                  cryp7:
   813 0000024A 8A0F                    	mov	cl, [bx]
   814 0000024C 43                      	inc	bx
   815 0000024D 83E17F                  	and	cx, 7Fh
   816 00000250 D0E1                    	shl	cl, 1
   817 00000252 87CB                    	xchg	cx, bx
   818 00000254 8B87[E606]              	mov	ax, [bx+cagecode]
   819 00000258 8904                    	mov	[si], ax
   820 0000025A 46                      	inc	si
   821 0000025B 46                      	inc	si
   822 0000025C 8B87[E607]              	mov	ax, [bx+wheelcode]
   823 00000260 AB                      	stosw
   824 00000261 89CB                    	mov	bx, cx
   825                                  	; 31/01/2022
   826 00000263 81FB[DC04]              	cmp	bx, key+128
   827 00000267 72E1                    	jb	short cryp7
   828                                  
   829                                  ;/
   830                                  ;/	now spin the cage against the wheel to produce output.
   831                                  ;/
   832                                  ;	mov	$word,r4
   833                                  ;	mov	$wheel+128.,r3
   834                                  ;3:
   835                                  ;	mov	-(r3),r2
   836                                  ;	mov	$cage,r0
   837                                  ;	clr	r5
   838                                  ;1:
   839                                  ;	bit	r2,(r0)+
   840                                  ;	beq	2f
   841                                  ;	incb	r5
   842                                  ;2:
   843                                  ;	cmp	r0,$cage+256.
   844                                  ;	blo	1b
   845                                  
   846                                  ;/
   847                                  ;/	now spin the cage against the wheel to produce output.
   848                                  ;/
   849                                  cryp8:
   850                                  	; 31/01/2022
   851 00000269 BF[DC04]                	mov	di, _word
   852 0000026C BB[6606]                	mov	bx, wheel+128
   853                                  cryp9:
   854 0000026F 4B                      	dec	bx
   855 00000270 4B                      	dec	bx
   856 00000271 8B17                    	mov	dx, [bx]
   857                                  	; 31/01/2022
   858 00000273 BE[E604]                	mov	si, cage
   859 00000276 29C9                    	sub	cx, cx ; 0
   860                                  cryp10:
   861 00000278 AD                      	lodsw
   862 00000279 85D0                    	test	ax, dx
   863 0000027B 7402                    	jz	short cryp11
   864 0000027D FEC1                    	inc	cl
   865                                  cryp11:
   866                                  	; 31/01/2022
   867 0000027F 81FE[E605]              	cmp	si, cage+256
   868 00000283 72F3                    	jb	short cryp10
   869                                  
   870                                  ;/
   871                                  ;/	we have a piece of output from current wheel
   872                                  ;/	it needs to be folded to remove lingering hopes of
   873                                  ;/	inverting the function
   874                                  ;/
   875                                  ;	mov	r4,-(sp)
   876                                  ;	clr	r4
   877                                  ;	div	$26.+26.+10.,r4
   878                                  ;	add	$'0,r5
   879                                  ;	cmp	r5,$'9
   880                                  ;	blos	1f
   881                                  ;	add	$'A-'9-1,r5
   882                                  ;	cmp	r5,$'Z
   883                                  ;	blos	1f
   884                                  ;	add	$'a-'Z-1,r5
   885                                  ;1:
   886                                  ;	mov	(sp)+,r4
   887                                  ;	movb	r5,(r4)+
   888                                  ;	cmp	r4,$word+8.
   889                                  ;	blo	3b
   890                                  ;/
   891                                  ;
   892                                  ;	mov	(sp)+,r5
   893                                  ;	mov	(sp)+,r4
   894                                  ;	mov	(sp)+,r3
   895                                  ;	mov	(sp)+,r2
   896                                  ;	mov	(sp)+,r1
   897                                  ;	mov	$word,r0
   898                                  ;	rts	pc
   899                                  ;	.bss
   900                                  ;key:	.=.+128.
   901                                  ;word:	.=.+32.
   902                                  ;cage:	.=.+256.
   903                                  ;wheel:	.=.+256.
   904                                  
   905                                  ;/
   906                                  ;/	we have a piece of output from current wheel
   907                                  ;/	it needs to be folded to remove lingering hopes of
   908                                  ;/	inverting the function
   909                                  ;/
   910 00000285 89C8                    	mov	ax, cx
   911 00000287 B23E                    	mov	dl, 26+26+10
   912 00000289 F6F2                    	div	dl
   913 0000028B 88E0                    	mov	al, ah
   914 0000028D 0430                    	add	al, '0'
   915 0000028F 3C39                    	cmp	al, '9'
   916 00000291 7608                    	jna	short cryp12
   917 00000293 0407                    	add	al, 'A'-'9'-1
   918 00000295 3C5A                    	cmp	al, 'Z'
   919 00000297 7602                    	jna	short cryp12
   920 00000299 0406                    	add	al, 'a'-'Z'-1
   921                                  cryp12:
   922 0000029B AA                      	stosb
   923                                  	; 31/01/2022
   924 0000029C 81FF[E404]              	cmp	di, _word+8	
   925 000002A0 72CD                    	jb	short cryp9
   926 000002A2 BE[DC04]                	mov	si, _word
   927 000002A5 C3                      	retn
   928                                  
   929                                  ; ---------------------------------------------------
   930                                  ; 01/05/2022
   931                                  ; 'getc' assembly source code
   932                                  ; copied from: 'chown0.s' (Erdogan Tan, 30/04/2022)
   933                                  ;	(derived from unix v5 'get.s')
   934                                  ; ---------------------------------------------------
   935                                  
   936                                  getc:
   937                                  	; 01/05/2022
   938                                  	; 30/04/2022
   939                                  	; 29/04/2022
   940                                  
   941                                  	; INPUT:
   942                                  	;    ibuf = read buffer (header) address
   943                                  	; OUTPUT:
   944                                  	;    al = character (if cf=0)
   945                                  	;    (if cf = 1 -> read error)
   946                                  
   947                                  	; Modified registers: ax, bx, cx, dx, bp	
   948                                  
   949 000002A6 BD[E608]                	mov	bp, ibuf
   950 000002A9 8B4602                  	mov	ax, [bp+2] ; char count
   951                                  	;and	ax, ax
   952 000002AC 21C0                    	and	ax, ax
   953 000002AE 751A                    	jnz	short gch1
   954                                  gch0:
   955 000002B0 8B5E00                  	mov	bx, [bp]
   956 000002B3 B9[EC08]                	mov	cx, ibuf+6 ; read buff. (data) addr.
   957 000002B6 894E04                  	mov 	[bp+4], cx ; char offset
   958                                  	;mov	[bp+2], ax ; 0
   959                                  	;sub	dx, dx
   960 000002B9 28D2                    	sub	dl, dl
   961 000002BB B602                    	mov	dh, 2 
   962                                  	;mov 	dx, 512 
   963                                  	sys	_read ; sys _read, bx, cx, dx
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82                              <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84                              <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86                              <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 000002BD B80300              <1>  mov ax, %1
    91 000002C0 CD20                <1>  int 20h
   964 000002C2 7215                    	jc	short gch2
   965 000002C4 09C0                    	or	ax, ax
   966                                  	;jz	short gch3
   967 000002C6 7502                    	jnz	short gch1
   968                                  	;
   969 000002C8 F9                      	stc
   970 000002C9 C3                      	retn
   971                                  gch1:
   972 000002CA 48                      	dec	ax
   973 000002CB 894602                  	mov	[bp+2], ax
   974 000002CE 8B5E04                  	mov	bx, [bp+4]
   975 000002D1 30E4                    	xor	ah, ah
   976 000002D3 8A07                    	mov	al, [bx]
   977 000002D5 43                      	inc	bx
   978 000002D6 895E04                  	mov	[bp+4], bx
   979                                  	;retn 	
   980                                  gch2:
   981                                  	;xor	ax, ax
   982 000002D9 C3                      	retn
   983                                  ;gch3:
   984                                  	;stc
   985                                  	;retn
   986                                  	
   987                                  ; ---------------------------------------------------
   988                                  ; 30/04/2022 (Erdogan Tan)
   989                                  ; 'putc' assembly source code 
   990                                  ;	(derived from unix v5 'put.s')
   991                                  ; ---------------------------------------------------
   992                                  
   993                                  putc:
   994                                  	; 01/05/2022 (16 bit code)
   995                                  	; 30/04/2022
   996                                  	; 29/04/2022
   997                                  
   998                                  	; INPUT:
   999                                  	;      al = character (to be written)
  1000                                  	;    obuf = write buffer (header) address
  1001                                  	; OUTPUT:
  1002                                  	;    al = character (if cf=0)
  1003                                  	;    (if cf = 1 -> write error)
  1004                                  
  1005                                  	; Modified registers: ax, bx, cx, dx, bp	
  1006                                  
  1007 000002DA BD[EC0A]                	mov	bp, obuf
  1008                                  pch0:
  1009 000002DD FF4E02                  	dec	word [bp+2] ; char count
  1010 000002E0 7D07                    	jge	short pch1
  1011 000002E2 50                      	push	ax
  1012 000002E3 E80F00                  	call	_fl_
  1013 000002E6 58                      	pop	ax
  1014 000002E7 EBF4                    	jmp	short pch0
  1015                                  pch1:
  1016 000002E9 8B5E04                  	mov	bx, [bp+4] ; char offset
  1017 000002EC 8807                    	mov	[bx], al
  1018                                  	;inc	bx
  1019                                  	;mov	[bp+4], bx
  1020 000002EE FF4604                  	inc	word [bp+4]
  1021 000002F1 C3                      	retn
  1022                                  flush:
  1023 000002F2 BD[EC0A]                	mov	bp, obuf
  1024                                  _fl_:
  1025 000002F5 89EA                    	mov	dx, bp ; buffer header address
  1026 000002F7 83C206                  	add	dx, 6 ; +6
  1027                                  	; dx = buffer data address
  1028 000002FA 52                      	push	dx
  1029 000002FB 8B4604                  	mov	ax, [bp+4] ; char offset
  1030 000002FE 09C0                    	or 	ax, ax
  1031 00000300 7410                    	jz	short pch2 ; empty/new buffer
  1032 00000302 29D0                    	sub	ax, dx ; char count
  1033                                  	; [bp] = file descriptor
  1034 00000304 8B4E00                  	mov	cx, [bp]
  1035                                  	sys	_write, cx, dx, ax
    80                              <1> 
    81                              <1>  %if %0 >= 2
    82 00000307 89CB                <1>  mov bx, %2
    83                              <1>  %if %0 >= 3
    84 00000309 89D1                <1>  mov cx, %3
    85                              <1>  %if %0 >= 4
    86 0000030B 89C2                <1>  mov dx, %4
    87                              <1>  %endif
    88                              <1>  %endif
    89                              <1>  %endif
    90 0000030D B80400              <1>  mov ax, %1
    91 00000310 CD20                <1>  int 20h
  1036                                  pch2:
  1037 00000312 8F4604                  	pop	word [bp+4]; character offset
  1038 00000315 C746020002              	mov	word [bp+2], 512 
  1039                                  			; available char count
  1040                                  			; to write in buffer
  1041                                  			; (before flushing)
  1042 0000031A C3                      	retn
  1043                                  
  1044                                  ;-----------------------------------------------------------------
  1045                                  ;  data - initialized data
  1046                                  ;-----------------------------------------------------------------
  1047                                  
  1048                                  ;align 4
  1049                                  
  1050                                  ; 30/04/2022
  1051                                  
  1052                                  ; cryprt.s
  1053                                  
  1054 0000031B 010002000400080010-     shift:	dw 1, 2, 4, 8, 16, 32, 64, 128, 256, 512
  1054 00000324 002000400080000001-
  1054 0000032D 0002               
  1055 0000032F 000400080010002000-     	dw 1024, 2048, 4096, 8192, 16384, 32768
  1055 00000338 400080             
  1056 0000033B 01000200                	dw 1, 2
  1057                                  wheeldiv:
  1058 0000033F 20120A0604              	db 32, 18, 10, 6, 4
  1059                                  
  1060                                  ; passwd.s
  1061                                  
  1062 00000344 2F6574632F70617373-     passwf:	db "/etc/passwd", 0 ; password file
  1062 0000034D 776400             
  1063 00000350 2F746D702F70746D70-     tempf:	db "/tmp/ptmp", 0 ; temporary file	
  1063 00000359 00                 
  1064                                  
  1065                                  usage_msg:
  1066 0000035A 0D0A                    	db 0Dh, 0Ah 
  1067 0000035C 55736167653A207061-     	db "Usage: passwd uid password", 0Dh, 0Ah, 0
  1067 00000365 737377642075696420-
  1067 0000036E 70617373776F72640D-
  1067 00000377 0A00               
  1068                                  cant_open_msg:
  1069 00000379 0D0A                    	db 0Dh, 0Ah
  1070 0000037B 63616E6E6F74206F70-     	db "cannot open password file", 0Dh, 0Ah, 0
  1070 00000384 656E2070617373776F-
  1070 0000038D 72642066696C650D0A-
  1070 00000396 00                 
  1071                                  tmpf_bsy_msg:
  1072 00000397 0D0A                    	db 0Dh, 0Ah
  1073 00000399 74656D702066696C65-     	db "temp file busy -- try again", 0Dh, 0Ah, 0
  1073 000003A2 2062757379202D2D20-
  1073 000003AB 74727920616761696E-
  1073 000003B4 0D0A00             
  1074                                  p_denied_msg:
  1075 000003B7 0D0A                    	db 0Dh, 0Ah
  1076 000003B9 7065726D697373696F-     	db "permission denied", 0Dh, 0Ah, 0
  1076 000003C2 6E2064656E6965640D-
  1076 000003CB 0A00               
  1077                                  cnro_tmpf_msg:
  1078 000003CD 0D0A                    	db 0Dh, 0Ah
  1079 000003CF 63616E6E6F74207265-     	db "cannot reopen temp file", 0Dh, 0Ah, 0
  1079 000003D8 6F70656E2074656D70-
  1079 000003E1 2066696C650D0A00   
  1080                                  cnro_pswdf_msg:
  1081 000003E9 0D0A                    	db 0Dh, 0Ah
  1082 000003EB 63616E6E6F74207265-     	db "cannot reopen password file", 0Dh, 0Ah, 0
  1082 000003F4 6F70656E2070617373-
  1082 000003FD 776F72642066696C65-
  1082 00000406 0D0A00             
  1083                                  format_err_msg:
  1084 00000409 0D0A                    	db 0Dh, 0Ah
  1085 0000040B 666F726D6174206572-     	db "format error on password file", 0Dh, 0Ah, 0
  1085 00000414 726F72206F6E207061-
  1085 0000041D 7373776F7264206669-
  1085 00000426 6C650D0A00         
  1086                                  not_valid_msg:
  1087 0000042B 0D0A                    	db 0Dh, 0Ah
  1088 0000042D 756964206E6F742076-     	db "uid not valid", 0Dh, 0Ah, 0
  1088 00000436 616C69640D0A00     
  1089                                  
  1090                                  	; 01/05/2022
  1091                                  long_pswd_msg:
  1092 0000043D 0D0A                    	db 0Dh, 0Ah
  1093 0000043F 70617373776F726420-     	db "password length > 8 chars", 0Dh, 0Ah, 0	
  1093 00000448 6C656E677468203E20-
  1093 00000451 382063686172730D0A-
  1093 0000045A 00                 
  1094                                  
  1095                                  ;-----------------------------------------------------------------
  1096                                  ;  bss - uninitialized data
  1097                                  ;-----------------------------------------------------------------	
  1098                                  
  1099 0000045B 90                      align 4
  1100                                  
  1101                                  bss_start:
  1102                                  
  1103                                  ABSOLUTE bss_start
  1104                                  
  1105                                  ; 30/04/2022
  1106                                  
  1107                                  ; crypt.s
  1108                                  
  1109 0000045C <res 80h>               key:	resb 128
  1110                                  ;_word:	resb 32
  1111 000004DC <res Ah>                _word:	resb 10
  1112                                  	;resb 2 ; 01/05/2022
  1113 000004E6 <res 100h>              cage:	resb 256
  1114 000005E6 <res 100h>              wheel:	resb 256
  1115                                  ; 01/05/2022
  1116 000006E6 <res 100h>              cagecode:  resb 256 ; resw 256
  1117 000007E6 <res 100h>              wheelcode: resb 256 ; resw 256 
  1118                                  
  1119                                  ; passwd.s
  1120                                  
  1121                                  ; 01/05/2022 (16 bit modifications for Retro UNIX 8086 v1)
  1122 000008E6 <res 206h>              ibuf:	resb 518 ; 512+6
  1123 00000AEC <res 206h>              obuf:	resb 518 ; 512+6
  1124 00000CF2 ????                    cryptp:	resw 1
  1125 00000CF4 ????                    uidp:	resw 1
  1126 00000CF6 ??                      sflg:	resb 1 ; resw 1
  1127 00000CF7 ??                      dflg:	resb 1 ; resw 1
  1128                                  
  1129                                  ; 30/04/2022
  1130                                  
  1131                                  ;-----------------------------------------------------------------
  1132                                  ; Original UNIX v5 - /bin/passwd source code (passwd.s)
  1133                                  ;		     in PDP-11 (unix) assembly language
  1134                                  ;-----------------------------------------------------------------
  1135                                  ;
  1136                                  ;/ passwd -- change user's password
  1137                                  ;
  1138                                  ;.globl	mesg
  1139                                  ;.globl	crypt
  1140                                  ;.globl	getc
  1141                                  ;.globl	flush
  1142                                  ;.globl	fcreat
  1143                                  ;.globl	putc
  1144                                  ;.globl	fopen
  1145                                  ;
  1146                                  ;	cmp	(sp)+,$3
  1147                                  ;	bge	1f
  1148                                  ;	jsr	r5,mesg
  1149                                  ;		<Usage: passwd uid password\n\0>; .even
  1150                                  ;	sys	exit
  1151                                  ;1:
  1152                                  ;	tst	(sp)+
  1153                                  ;	mov	(sp)+,uidp
  1154                                  ;	mov	(sp)+,r0
  1155                                  ;	tstb	(r0)
  1156                                  ;	beq	1f
  1157                                  ;	jsr	pc,crypt
  1158                                  ;	clrb	8(r0)
  1159                                  ;1:
  1160                                  ;	mov	r0,cryptp
  1161                                  ;	mov	$passwf,r0
  1162                                  ;	jsr	r5,fopen; ibuf
  1163                                  ;	bec	1f
  1164                                  ;	jsr	r5,mesg
  1165                                  ;		<cannot open password file\n\0>; .even
  1166                                  ;	sys	exit
  1167                                  ;1:
  1168                                  ;	sys	stat; tempf; obuf+20.
  1169                                  ;	bec	2f
  1170                                  ;	sys	creat; tempf; 222
  1171                                  ;	bec	1f
  1172                                  ;2:
  1173                                  ;	jsr	r5,mesg
  1174                                  ;		<temp file busy -- try again\n\0>; .even
  1175                                  ;	sys	exit
  1176                                  ;1:
  1177                                  ;	mov	r0,obuf
  1178                                  ;
  1179                                  ;/ search for uid
  1180                                  ;
  1181                                  ;comp:
  1182                                  ;	mov	uidp,r1
  1183                                  ;1:
  1184                                  ;	jsr	pc,pcop
  1185                                  ;	cmp	r0,$':
  1186                                  ;	beq	1f
  1187                                  ;	cmpb	r0,(r1)+
  1188                                  ;	beq	1b
  1189                                  ;2:
  1190                                  ;	jsr	pc,pcop
  1191                                  ;	cmp	r0,$'\n
  1192                                  ;	bne	2b
  1193                                  ;	br	comp
  1194                                  ;1:
  1195                                  ;	tstb	(r1)+
  1196                                  ;	bne	2b
  1197                                  ;
  1198                                  ;/ skip over old password
  1199                                  ;
  1200                                  ;1:
  1201                                  ;	jsr	pc,pget
  1202                                  ;	cmp	r0,$':
  1203                                  ;	bne	1b
  1204                                  ;
  1205                                  ;/ copy in new password
  1206                                  ;
  1207                                  ;	mov	cryptp,r1
  1208                                  ;1:
  1209                                  ;	movb	(r1)+,r0
  1210                                  ;	beq	1f
  1211                                  ;	jsr	pc,pput
  1212                                  ;	br	1b
  1213                                  ;1:
  1214                                  ;	mov	$':,r0
  1215                                  ;	jsr	pc,pput
  1216                                  ;
  1217                                  ;/ validate permission
  1218                                  ;
  1219                                  ;	clr	r1
  1220                                  ;1:
  1221                                  ;	jsr	pc,pcop
  1222                                  ;	cmp	r0,$':
  1223                                  ;	beq	1f
  1224                                  ;	mpy	$10.,r1
  1225                                  ;	sub	$'0,r0
  1226                                  ;	add	r0,r1
  1227                                  ;	br	1b
  1228                                  ;1:
  1229                                  ;	sys	getuid
  1230                                  ;	tst	r0
  1231                                  ;	beq	1f
  1232                                  ;	cmp	r0,r1
  1233                                  ;	beq	1f
  1234                                  ;	jsr	r5,mesg
  1235                                  ;		<permission denied\n\0>; .even
  1236                                  ;	br	done
  1237                                  ;1:
  1238                                  ;	inc	sflg
  1239                                  ;1:
  1240                                  ;	jsr	pc,pcop
  1241                                  ;	br	1b
  1242                                  ;
  1243                                  ;done:
  1244                                  ;	jsr	r5,flush; obuf
  1245                                  ;	mov	obuf,r0
  1246                                  ;	sys	close
  1247                                  ;	mov	ibuf,r0
  1248                                  ;	sys	close
  1249                                  ;	tst	sflg
  1250                                  ;	beq	1f
  1251                                  ;	tst	dflg
  1252                                  ;	bne	1f
  1253                                  ;	inc	dflg
  1254                                  ;	mov	$tempf,r0
  1255                                  ;	jsr	r5,fopen; ibuf
  1256                                  ;	bec	2f
  1257                                  ;	jsr	r5,mesg
  1258                                  ;		<cannot reopen temp file\n\0>; .even
  1259                                  ;	br	1f
  1260                                  ;2:
  1261                                  ;	mov	$passwf,r0
  1262                                  ;	jsr	r5,fcreat; obuf
  1263                                  ;	bec	2f
  1264                                  ;	jsr	r5,mesg
  1265                                  ;		<cannot reopen password file\n\0>; .even
  1266                                  ;	br	1f
  1267                                  ;2:
  1268                                  ;	jsr	pc,pcop
  1269                                  ;	br	2b
  1270                                  ;1:
  1271                                  ;	sys	unlink; tempf
  1272                                  ;	sys	exit
  1273                                  ;
  1274                                  ;pput:
  1275                                  ;	jsr	r5,putc; obuf
  1276                                  ;	rts	pc
  1277                                  ;
  1278                                  ;pget:
  1279                                  ;	jsr	r5,getc; ibuf
  1280                                  ;	bes	1f
  1281                                  ;	rts	pc
  1282                                  ;1:
  1283                                  ;	jsr	r5,mesg
  1284                                  ;		<format error on password file\n\0>; .even
  1285                                  ;	br	done
  1286                                  ;
  1287                                  ;pcop:
  1288                                  ;	jsr	r5,getc; ibuf
  1289                                  ;	bes	1f
  1290                                  ;	jsr	r5,putc; obuf
  1291                                  ;	rts	pc
  1292                                  ;1:
  1293                                  ;	tst	sflg
  1294                                  ;	bne	1f
  1295                                  ;	jsr	r5,mesg
  1296                                  ;		<uid not valid\n\0>; .even
  1297                                  ;1:
  1298                                  ;	br	done
  1299                                  ;
  1300                                  ;.data
  1301                                  ;passwf: </etc/passwd\0>
  1302                                  ;tempf:	</tmp/ptmp\0>
  1303                                  ;.even
  1304                                  ;.bss
  1305                                  ;ibuf:	.=.+520.
  1306                                  ;obuf:	.=.+520.
  1307                                  ;cryptp: .=.+2
  1308                                  ;uidp:	.=.+2
  1309                                  ;sflg:	.=.+2
  1310                                  ;dflg:	.=.+2
  1311                                  
  1312                                  ; 30/04/2022
  1313                                  
  1314                                  ;-----------------------------------------------------------------
  1315                                  ; Original UNIX v5 - 'crypt' source code (crypt.s)
  1316                                  ;		     in PDP-11 (unix) assembly language
  1317                                  ;-----------------------------------------------------------------
  1318                                  ;/usr/source/s3/crypt.s -- password incoding
  1319                                  ;
  1320                                  ;/ crypt -- password incoding
  1321                                  ;
  1322                                  ;/	mov	$key,r0
  1323                                  ;/	jsr	pc,crypt
  1324                                  ;
  1325                                  ;.globl	crypt, word
  1326                                  ;
  1327                                  ;crypt:
  1328                                  ;	mov	r1,-(sp)
  1329                                  ;	mov	r2,-(sp)
  1330                                  ;	mov	r3,-(sp)
  1331                                  ;	mov	r4,-(sp)
  1332                                  ;	mov	r5,-(sp)
  1333                                  ;
  1334                                  ;	mov	r0,r1
  1335                                  ;	mov	$key,r0
  1336                                  ;	movb	$004,(r0)+
  1337                                  ;	movb	$034,(r0)+
  1338                                  ;1:
  1339                                  ;	cmp	r0,$key+64.
  1340                                  ;	bhis	1f
  1341                                  ;	movb	(r1)+,(r0)+
  1342                                  ;	bne	1b
  1343                                  ;1:
  1344                                  ;	dec	r0
  1345                                  ;/
  1346                                  ;/
  1347                                  ;/	fill out key space with clever junk
  1348                                  ;/
  1349                                  ;	mov	$key,r1
  1350                                  ;1:
  1351                                  ;	movb	-1(r0),r2
  1352                                  ;	movb	(r1)+,r3
  1353                                  ;	xor	r3,r2
  1354                                  ;	movb	r2,(r0)+
  1355                                  ;	cmp	r0,$key+128.
  1356                                  ;	blo	1b
  1357                                  ;/
  1358                                  ;/
  1359                                  ;/	establish wheel codes and cage codes
  1360                                  ;/
  1361                                  ;	mov	$wheelcode,r4
  1362                                  ;	mov	$cagecode,r5
  1363                                  ;	mov	$256.,-(sp)
  1364                                  ;2:
  1365                                  ;	clr	r2
  1366                                  ;	clr	(r4)
  1367                                  ;	mov	$wheeldiv,r3
  1368                                  ;3:
  1369                                  ;	clr	r0
  1370                                  ;	mov	(sp),r1
  1371                                  ;	div	(r3)+,r0
  1372                                  ;	add	r1,r2
  1373                                  ;	bic	$40,r2
  1374                                  ;	bis	shift(r2),(r4)
  1375                                  ;	cmp	r3,$wheeldiv+6.
  1376                                  ;	bhis	4f
  1377                                  ;	bis	shift+4(r2),(r5)
  1378                                  ;4:
  1379                                  ;	cmp	r3,$wheeldiv+10.
  1380                                  ;	blo	3b
  1381                                  ;	sub	$2,(sp)
  1382                                  ;	tst	(r4)+
  1383                                  ;	tst	(r5)+
  1384                                  ;	cmp	r4,$wheelcode+256.
  1385                                  ;	blo	2b
  1386                                  ;	tst	(sp)+
  1387                                  ;/
  1388                                  ;	.data
  1389                                  ;shift:	1;2;4;10;20;40;100;200;400;1000;2000;4000;10000;20000;40000;100000
  1390                                  ;	1;2
  1391                                  ;wheeldiv: 32.; 18.; 10.; 6.; 4.
  1392                                  ;	.bss
  1393                                  ;cagecode: .=.+256.
  1394                                  ;wheelcode: .=.+256.
  1395                                  ;	.text
  1396                                  ;/
  1397                                  ;/
  1398                                  ;/	make the internal settings of the machine
  1399                                  ;/	both the lugs on the 128 cage bars and the lugs
  1400                                  ;/	on the 16 wheels are set from the expanded key
  1401                                  ;/
  1402                                  ;	mov	$key,r0
  1403                                  ;	mov	$cage,r2
  1404                                  ;	mov	$wheel,r3
  1405                                  ;1:
  1406                                  ;	movb	(r0)+,r1
  1407                                  ;	bic	$!177,r1
  1408                                  ;	asl	r1
  1409                                  ;	mov	cagecode(r1),(r2)+
  1410                                  ;	mov	wheelcode(r1),(r3)+
  1411                                  ;	cmp	r0,$key+128.
  1412                                  ;	blo	1b
  1413                                  ;/
  1414                                  ;/
  1415                                  ;/	now spin the cage against the wheel to produce output.
  1416                                  ;/
  1417                                  ;	mov	$word,r4
  1418                                  ;	mov	$wheel+128.,r3
  1419                                  ;3:
  1420                                  ;	mov	-(r3),r2
  1421                                  ;	mov	$cage,r0
  1422                                  ;	clr	r5
  1423                                  ;1:
  1424                                  ;	bit	r2,(r0)+
  1425                                  ;	beq	2f
  1426                                  ;	incb	r5
  1427                                  ;2:
  1428                                  ;	cmp	r0,$cage+256.
  1429                                  ;	blo	1b
  1430                                  ;/
  1431                                  ;/	we have a piece of output from current wheel
  1432                                  ;/	it needs to be folded to remove lingering hopes of
  1433                                  ;/	inverting the function
  1434                                  ;/
  1435                                  ;	mov	r4,-(sp)
  1436                                  ;	clr	r4
  1437                                  ;	div	$26.+26.+10.,r4
  1438                                  ;	add	$'0,r5
  1439                                  ;	cmp	r5,$'9
  1440                                  ;	blos	1f
  1441                                  ;	add	$'A-'9-1,r5
  1442                                  ;	cmp	r5,$'Z
  1443                                  ;	blos	1f
  1444                                  ;	add	$'a-'Z-1,r5
  1445                                  ;1:
  1446                                  ;	mov	(sp)+,r4
  1447                                  ;	movb	r5,(r4)+
  1448                                  ;	cmp	r4,$word+8.
  1449                                  ;	blo	3b
  1450                                  ;/
  1451                                  ;
  1452                                  ;	mov	(sp)+,r5
  1453                                  ;	mov	(sp)+,r4
  1454                                  ;	mov	(sp)+,r3
  1455                                  ;	mov	(sp)+,r2
  1456                                  ;	mov	(sp)+,r1
  1457                                  ;	mov	$word,r0
  1458                                  ;	rts	pc
  1459                                  ;
  1460                                  ;	.bss
  1461                                  ;key:	.=.+128.
  1462                                  ;word:	.=.+32.
  1463                                  ;cage:	.=.+256.
  1464                                  ;wheel:	.=.+256.
  1465                                  
  1466                                  ; 30/04/2022
  1467                                  
  1468                                  ;-----------------------------------------------------------------
  1469                                  ; Original UNIX v5 - 'getc' & 'fopen' source code (get.s)
  1470                                  ;		     in PDP-11 (unix) assembly language
  1471                                  ;-----------------------------------------------------------------
  1472                                  ;/usr/source/s3/get.s
  1473                                  ;--------------------
  1474                                  ;/ getw/getc -- get words/characters from input file
  1475                                  ;/ fopen -- open a file for use by get(c|w)
  1476                                  ;/
  1477                                  ;/ calling sequences --
  1478                                  ;/
  1479                                  ;/   mov $filename,r0
  1480                                  ;/   jsr r5,fopen; ioptr
  1481                                  ;/
  1482                                  ;/  on return ioptr buffer is set up or error bit is set if
  1483                                  ;/  file could not be opened.
  1484                                  ;/
  1485                                  ;/   jsr r5,get(c|w)1; ioptr
  1486                                  ;/
  1487                                  ;/  on return char/word is in r0; error bit is
  1488                                  ;/  set on error or end of file.
  1489                                  ;/
  1490                                  ;/  ioptr is the address of a 518-byte buffer
  1491                                  ;/  whose layout is as follows:
  1492                                  ;/
  1493                                  ;/  ioptr: .=.+2    / file descriptor
  1494                                  ;/         .=.+2    / charact+2    / pointer to next character (reset if no. chars=0)
  1495                                  ;/         .=.+512. / the buffer
  1496                                  ;
  1497                                  ;	.globl	getc,getw,fopen
  1498                                  ;
  1499                                  ;fopen:
  1500                                  ;	mov	r1,-(sp)
  1501                                  ;	mov	(r5)+,r1
  1502                                  ;	mov	r0,0f
  1503                                  ;	sys	0; 9f
  1504                                  ;.data
  1505                                  ;9:
  1506                                  ;	sys	open; 0:..; 0
  1507                                  ;.text
  1508                                  ;	bes	1f
  1509                                  ;	mov	r0,(r1)+
  1510                                  ;	clr	(r1)+
  1511                                  ;	mov	(sp)+,r1
  1512                                  ;	rts	r5
  1513                                  ;1:
  1514                                  ;	mov	$-1,(r1)
  1515                                  ;	mov	(sp)+,r1
  1516                                  ;	sec
  1517                                  ;	rts	r5
  1518                                  ;
  1519                                  ;.data
  1520                                  ;getw:
  1521                                  ;	mov	(r5),9f
  1522                                  ;	mov	(r5)+,8f
  1523                                  ;	jsr	r5,getc; 8:..
  1524                                  ;	bec	1f
  1525                                  ;	rts	r5
  1526                                  ;1:
  1527                                  ;	mov	r0,-(sp)
  1528                                  ;	jsr	r5,getc; 9:..
  1529                                  ;	swab	r0
  1530                                  ;	bis	(sp)+,r0
  1531                                  ;	rts	r5
  1532                                  ;.text
  1533                                  ;
  1534                                  ;getc:
  1535                                  ;	mov	r1,-(sp)
  1536                                  ;	mov	(r5)+,r1
  1537                                  ;	dec	2(r1)
  1538                                  ;	bge	1f
  1539                                  ;	mov	r1,r0
  1540                                  ;	add	$6,r0
  1541                                  ;	mov	r0,0f
  1542                                  ;	mov	r0,4(r1)
  1543                                  ;	mov	(r1),r0
  1544                                  ;	sys	0; 9f
  1545                                  ;.data
  1546                                  ;9:
  1547                                  ;	sys	read; 0:..; 512.
  1548                                  ;.text
  1549                                  ;	bes	2f
  1550                                  ;	tst	r0
  1551                                  ;	bne	3f
  1552                                  ;2:
  1553                                  ;	mov	(sp)+,r1
  1554                                  ;	sec
  1555                                  ;	rts	r5
  1556                                  ;3:
  1557                                  ;	dec	r0
  1558                                  ;	mov	r0,2(r1)
  1559                                  ;1:
  1560                                  ;	clr	r0
  1561                                  ;	bisb	*4(r1),r0
  1562                                  ;	inc	4(r1)
  1563                                  ;	mov	(sp)+,r1
  1564                                  ;	rts	r5
  1565                                  
  1566                                  ; 30/04/2022
  1567                                  
  1568                                  ;-----------------------------------------------------------------
  1569                                  ; Original UNIX v5 - 'putc' & 'flush' & 'fcreat' source code
  1570                                  ;		     (put.s) in PDP-11 (unix) assembly language
  1571                                  ;-----------------------------------------------------------------
  1572                                  ;/usr/source/s3/put.s
  1573                                  ;--------------------
  1574                                  ;/ putw/putc -- write words/characters on output file
  1575                                  ;/
  1576                                  ;/ fcreat -- create an output file for use by put(w|c)
  1577                                  ;/
  1578                                  ;/ calling sequences --
  1579                                  ;/
  1580                                  ;/   mov $filename,r0
  1581                                  ;/  jsr r5,fcreat; ioptr
  1582                                  ;/
  1583                                  ;/ on return ioptr is set up for use by put or error
  1584                                  ;/ bit is set if file could not be created.
  1585                                  ;/
  1586                                  ;/   mov(b) thing,r0
  1587                                  ;/   jsr r5,put(w|c)1; ioptr
  1588                                  ;/
  1589                                  ;/ the character or word is written out.
  1590                                  ;/
  1591                                  ;/   jsr r5,flush; ioptr
  1592                                  ;/
  1593                                  ;/ the buffer is fled.
  1594                                  ;/
  1595                                  ;
  1596                                  ;	.globl	putc, putw, flush, fcreat
  1597                                  ;
  1598                                  ;fcreat:
  1599                                  ;	mov	r1,-(sp)
  1600                                  ;	mov	(r5)+,r1
  1601                                  ;	mov	r0,0f
  1602                                  ;	sys	0; 9f
  1603                                  ;.data
  1604                                  ;9:
  1605                                  ;	sys	creat; 0:..; 666
  1606                                  ;.text
  1607                                  ;	bes	1f
  1608                                  ;	mov	r0,(r1)+
  1609                                  ;2:
  1610                                  ;	clr	(r1)+
  1611                                  ;	clr	(r1)+
  1612                                  ;	mov	(sp)+,r1
  1613                                  ;	rts	r5
  1614                                  ;1:
  1615                                  ;	mov	$-1,(r1)+
  1616                                  ;	mov	(sp)+,r1
  1617                                  ;	sec
  1618                                  ;	rts	r5
  1619                                  ;
  1620                                  ;.data
  1621                                  ;putw:
  1622                                  ;	mov	(r5),8f
  1623                                  ;	mov	(r5)+,9f
  1624                                  ;	mov	r0,-(sp)
  1625                                  ;	jsr	r5,putc; 8:..
  1626                                  ;	mov	(sp)+,r0
  1627                                  ;	swab	r0
  1628                                  ;	jsr	r5,putc; 9:..
  1629                                  ;	rts	r5
  1630                                  ;.text
  1631                                  ;
  1632                                  ;putc:
  1633                                  ;	mov	r1,-(sp)
  1634                                  ;	mov	(r5)+,r1
  1635                                  ;1:
  1636                                  ;	dec	2(r1)
  1637                                  ;	bge	1f
  1638                                  ;	mov	r0,-(sp)
  1639                                  ;	jsr	pc,fl
  1640                                  ;	mov	(sp)+,r0
  1641                                  ;	br	1b
  1642                                  ;1:
  1643                                  ;	movb	r0,*4(r1)
  1644                                  ;	inc	4(r1)
  1645                                  ;	mov	(sp)+,r1
  1646                                  ;	rts	r5
  1647                                  ;
  1648                                  ;flush:
  1649                                  ;	mov	r0,-(sp)
  1650                                  ;	mov	r1,-(sp)
  1651                                  ;	mov	(r5)+,r1
  1652                                  ;	jsr	pc,fl
  1653                                  ;	mov	(sp)+,r1
  1654                                  ;	mov	(sp)+,r0
  1655                                  ;	rts	r5
  1656                                  ;
  1657                                  ;fl:
  1658                                  ;	mov	r1,r0
  1659                                  ;	add	$6,r0
  1660                                  ;	mov	r0,-(sp)
  1661                                  ;	mov	r0,0f
  1662                                  ;	mov	4(r1),0f+2
  1663                                  ;	beq	1f
  1664                                  ;	sub	(sp),0f+2
  1665                                  ;	mov	(r1),r0
  1666                                  ;	sys	0; 9f
  1667                                  ;.data
  1668                                  ;9:
  1669                                  ;	sys	write; 0:..; ..
  1670                                  ;.text
  1671                                  ;1:
  1672                                  ;	mov	(sp)+,4(r1)
  1673                                  ;	mov	$512.,2(r1)
  1674                                  ;	rts	pc
