     1                                  ; ****************************************************************************
     2                                  ; cat386.s (Retro Unix 386 v1) - /bin/cat - concatenate files
     3                                  ; ----------------------------------------------------------------------------
     4                                  ;
     5                                  ; RETRO UNIX 386 (Retro Unix == Turkish Rational Unix)
     6                                  ; Operating System Project (v0.2) by ERDOGAN TAN (Beginning: 24/12/2013)
     7                                  ;
     8                                  ; Retro UNIX 8086 v1 - '/bin/cat' file
     9                                  ;
    10                                  ; Derived from 'Retro UNIX 8086 v1' source code by Erdogan Tan
    11                                  ; (v0.1 - Beginning: 11/07/2012)
    12                                  ;
    13                                  ; [ Last Modification: 18/06/2022 ] -cat4.s-
    14                                  ;
    15                                  ; Derived from UNIX Operating System (v1.0 for PDP-11) 
    16                                  ; (Original) Source Code by Ken Thompson (Bell Laboratories, 1971-1972)
    17                                  ; ****************************************************************************
    18                                  ; ((nasm cat4.s -l cat4.txt -o cat4 -Z error.txt))
    19                                  ; cat386.s - cat4.s (17/06/2022, Retro UNIX 386 v1 & v1.1)
    20                                  ; cat386.s - cat3.s (15/06/2022, Retro UNIX 386 v1.2)
    21                                  ; cat386.s - cat2.s (14/06/2022, Retro UNIX 386 v1 & v1.1)
    22                                  ; cat386.s - cat1.s (05/03/2022, Retro UNIX 386 v1 & v1.1 & v1.2)
    23                                  ; cat386.s - cat0.s (17/10/2015 - 28/12/2015, Retro UNIX 386 v1, NASM 2.11)
    24                                  ; CAT2.ASM (02/12/2013 - 16/07/2015, Retro UNIX 8086 v1, MASM 6.11) 
    25                                  
    26                                  ; 17/10/2015
    27                                  
    28                                  ; UNIX v1 system calls
    29                                  _rele 	equ 0
    30                                  _exit 	equ 1
    31                                  _fork 	equ 2
    32                                  _read 	equ 3
    33                                  _write	equ 4
    34                                  _open	equ 5
    35                                  _close 	equ 6
    36                                  _wait 	equ 7
    37                                  _creat 	equ 8
    38                                  _link 	equ 9
    39                                  _unlink	equ 10
    40                                  _exec	equ 11
    41                                  _chdir	equ 12
    42                                  _time 	equ 13
    43                                  _mkdir 	equ 14
    44                                  _chmod	equ 15
    45                                  _chown	equ 16
    46                                  _break	equ 17
    47                                  _stat	equ 18
    48                                  _seek	equ 19
    49                                  _tell 	equ 20
    50                                  _mount	equ 21
    51                                  _umount	equ 22
    52                                  _setuid	equ 23
    53                                  _getuid	equ 24
    54                                  _stime	equ 25
    55                                  _quit	equ 26	
    56                                  _intr	equ 27
    57                                  _fstat	equ 28
    58                                  _emt 	equ 29
    59                                  _mdate 	equ 30
    60                                  _stty 	equ 31
    61                                  _gtty	equ 32
    62                                  _ilgins	equ 33
    63                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    64                                  _msg    equ 35 ; Retro UNIX 386 v1 feature only !
    65                                  _geterr	equ 36 ; Retro UNIX 386 v1 feature only !
    66                                  ; 12/01/2022 - Retro UNIX 386 v1.2
    67                                  ; Retro UNIX 386 v2 system calls
    68                                  _setgid	equ 37
    69                                  _getgid	equ 38
    70                                  _sysver	equ 39 ; (get) Retro Unix 386 version
    71                                  
    72                                  %macro sys 1-4
    73                                      ; 03/09/2015	
    74                                      ; 13/04/2015
    75                                      ; Retro UNIX 386 v1 system call.		
    76                                      %if %0 >= 2   
    77                                          mov ebx, %2
    78                                          %if %0 >= 3    
    79                                              mov ecx, %3
    80                                              %if %0 = 4
    81                                                 mov edx, %4   
    82                                              %endif
    83                                          %endif
    84                                      %endif
    85                                      mov eax, %1
    86                                      int 30h	   
    87                                  %endmacro
    88                                  
    89                                  ; 17/06/2022 - Retro UNIX 386 v1
    90                                  ;struc stat
    91                                  ;	; Retro UNIX v1 'sysstat' output !
    92                                  ;	; (34 bytes)
    93                                  ;	.inode:  resw 1	
    94                                  ;	.mode:	 resw 1
    95                                  ;	.nlinks: resb 1
    96                                  ;	.uid:	 resb 1
    97                                  ;	.size:	 resw 1
    98                                  ;	.dskptr: resw 8
    99                                  ;	.ctime:	 resd 1
   100                                  ;	.mtime:	 resd 1
   101                                  ;	.rsvd:   resw 1
   102                                  ;	.strucsize:
   103                                  ;endstruc  
   104                                  
   105                                  ; Retro UNIX 386 v1 system call format:
   106                                  ; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
   107                                  
   108                                  [BITS 32] ; We need 32-bit intructions for protected mode
   109                                  
   110                                  [ORG 0] 
   111                                  
   112                                  START_CODE:
   113                                  	;; / cat -- concatenate files
   114                                  
   115                                  	;sys	_write, 1, nl, 2
   116                                  
   117                                  	; 18/06/2022
   118                                  	sys	_fstat, 1, iobuf 
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000000 BB01000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000005 B9[68020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 0000000A B81C000000          <1>  mov eax, %1
    86 0000000F CD30                <1>  int 30h
   119 00000011 E866000000              	call	rwcount
   120 00000016 20D2                    	and 	dl, dl
   121 00000018 7424                    	jz	short cat_@ ; block device or regular file
   122 0000001A 803D[68020000]09        	cmp	byte [iobuf], 9  ; /dev/lpr
   123 00000021 741B                    	je	short cat_@
   124                                  	; /dev/tty0 .. /dev/tty9
   125                                  	; next line
   126                                  	sys	_write, 1, nl, 2 
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000023 BB01000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000028 B9[4D020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 0000002D BA02000000          <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000032 B804000000          <1>  mov eax, %1
    86 00000037 CD30                <1>  int 30h
   127 00000039 B9[68020000]            	mov	ecx, iobuf
   128                                  cat_@:
   129 0000003E 5D                      	pop	ebp
   130 0000003F 5A                      	pop	edx
   131                                  	; esi = 0 ; ('sysexec' sets regs to zero)
   132                                  	; 05/03/2022
   133                                  	;mov	esi, fin 
   134                                  	;mov    edi, obuf
   135                                  	;EAX = 2 (written byte count)
   136 00000040 30C0                    	xor	al, al ; 0
   137                                          ; 18/06/2022  
   138                                  	;mov	ecx, iobuf ; 17/06/2022
   139 00000042 4D                      	dec     ebp
   140                                  	;jz	short cat_3	
   141                                  		;;mov	(sp)+,r5
   142                                  		;;tst	(sp)+
   143                                  		;;mov	$obuf,r2
   144                                  		;;cmp	r5,$1
   145                                  		;;beq	3f
   146                                  	; 18/06/ 2022
   147 00000043 7505                    	jnz	short cat_0
   148                                  ;cat_y:
   149 00000045 E991000000              	jmp	cat_3
   150                                  cat_0:	
   151 0000004A 5B                      	pop	ebx
   152 0000004B 803B2D                  	cmp	byte [ebx], '-'
   153                                  	;je	short cat_3 ; 16/06/2022
   154                                  		;;dec	r5
   155                                  		;;ble	done
   156                                  		;;mov	(sp)+,r0
   157                                  		;;cmpb	(r0),$'-
   158                                  		;;bne	2f
   159                                  		;;clr	fin
   160                                  		;;br	3f
   161                                  	; 18/06/2022
   162                                  	;je	short cat_y
   163 0000004E 7505                    	jne	short cat_1
   164 00000050 E986000000              	jmp	cat_3
   165                                  cat_1:
   166                                  	;;2:
   167                                  	; ebx = file name offset
   168 00000055 31C9                    	xor 	ecx, ecx ; 0
   169                                  	sys 	_open
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77                              <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79                              <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000057 B805000000          <1>  mov eax, %1
    86 0000005C CD30                <1>  int 30h
   170                                  	;jc	short cat_7
   171                                  	; 05/03/2022 ; (+)
   172 0000005E 7305                    	jnc	short cat_2
   173 00000060 E992010000              	jmp	cat_7
   174                                  cat_2:
   175                                  	; 05/03/2022
   176 00000065 89C6                    	mov	esi, eax
   177                                  	;mov	[esi], eax ; 05/03/2022
   178                                  	;mov	[esi], ax
   179                                  		;;mov	r0,0f
   180                                  		;;sys	open; 0:..; 0
   181                                  		;;bes	loop
   182                                  		;;mov	r0,fin
   183                                  
   184                                  	; 05/03/2022 ; (+)
   185                                  	; convert user's file number to inode number
   186                                  	; (get 34 byte inode details, inode num + inode)
   187                                  	; (get 66 byte inode details for runix 386 v1.2)
   188                                  	sys	_fstat, esi, iobuf 
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000067 89F3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000069 B9[68020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 0000006E B81C000000          <1>  mov eax, %1
    86 00000073 CD30                <1>  int 30h
   189                                  	;jc	short cat_7
   190                                  	; 05/03/2022 ; (+)
   191 00000075 7321                    	jnc	short cat_f
   192 00000077 E972010000              	jmp	cat_k ; 15/06/2022
   193                                  
   194                                  rwcount:
   195                                  	; 17/06/2022 - Retro UNIX 386 v1 (& v1.1)
   196 0000007C 31D2                    	xor	edx, edx
   197 0000007E 66A1[68020000]          	mov	ax, [iobuf] ; inode number
   198 00000084 6683F829                	cmp	ax, 41 ; regular file (or directory) ?
   199 00000088 730B                    	jnb	short rwc_2 ; yes
   200                                  
   201 0000008A 3C08                    	cmp	al, 8 ; > hd3 inode number ?
   202 0000008C 7704                    	ja	short rwc_1 ; yes ; character device
   203                                  
   204 0000008E 3C03                    	cmp	al, 3 ; >= fd0 inode number ?
   205 00000090 7303                    	jnb	short rwc_2 ; yes ; block device
   206                                  rwc_1:
   207                                  	; no, character device
   208                                  	; read/write count = 1
   209 00000092 FEC2                    	inc	dl
   210 00000094 C3                      	retn
   211                                  rwc_2:
   212                                  	; regular file or block device
   213                                  	; read/write count = 512
   214 00000095 B602                    	mov	dh, 2 ; edx = 512
   215 00000097 C3                      	retn
   216                                  	
   217                                  cat_f:
   218                                  	; 15/06/2022
   219 00000098 E8DFFFFFFF              	call	rwcount
   220 0000009D 8915[64020000]          	mov	[filerwc], edx
   221                                  
   222                                  	; 18/06/2022
   223                                  	; (check if input file is a tty)
   224 000000A3 C605[5C020000]00        	mov	byte [chrin], 0
   225 000000AA 20D2                    	and	dl, dl
   226 000000AC 742D                    	jz	short cat_3  ; not a character device
   227                                  	;mov	ax, [iobuf]
   228 000000AE A0[68020000]            	mov	al, [iobuf] ; inode number
   229 000000B3 3C13                    	cmp	al, 19 ; tty9
   230 000000B5 7724                    	ja	short cat_3
   231 000000B7 3C0A                    	cmp	al, 10 ; tty0
   232 000000B9 7319                    	jnb	short cat_g
   233                                  
   234                                  	; [chrin] = 0
   235 000000BB 3C01                    	cmp	al, 1  ; /dev/tty
   236 000000BD 751C                    	jne	short cat_3
   237                                  
   238                                  	sys	_gtty, 0, 1  ; get status of console tty (w)
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000000BF BB00000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 000000C4 B901000000          <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000000C9 B820000000          <1>  mov eax, %1
    86 000000CE CD30                <1>  int 30h
   239 000000D0 FEC0                    	inc	al  ; console tty number + 1
   240 000000D2 EB02                    	jmp	short cat_h
   241                                  cat_g:
   242 000000D4 2C09                    	sub 	al, 9
   243                                  cat_h:
   244 000000D6 A2[5C020000]            	mov	[chrin], al
   245                                  	; [chrin] = tty number + 1
   246                                  
   247                                  cat_3:	
   248                                  	; 05/03/2022 ; (+)
   249                                  	; get inode number of current tty (stdin)
   250                                  	; (get 34 byte inode details, inode num + inode)
   251                                  	; (get 66 byte inode details for runix 386 v1.2)
   252                                  	;sys	_fstat, 0, iobuf 
   253                                  	;;jc	short cat_n
   254                                  	;mov	ecx, iobuf	
   255                                  	sys	_fstat, 0  ; get stdin file inode details
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000000DB BB00000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79                              <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000000E0 B81C000000          <1>  mov eax, %1
    86 000000E5 CD30                <1>  int 30h
   256                                  
   257                                  	; 15/06/2022
   258 000000E7 E890FFFFFF              	call	rwcount
   259 000000EC 8915[60020000]          	mov	[stdinrw], edx
   260                                  
   261                                  cat_n:	;;3:
   262                                  	; get keyboard status of console tty
   263 000000F2 31DB                    	xor	ebx, ebx  ; 0
   264 000000F4 31C9                    	xor	ecx, ecx ; 0
   265                                  	sys	_gtty
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77                              <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79                              <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000000F6 B820000000          <1>  mov eax, %1
    86 000000FB CD30                <1>  int 30h
   266                                  	; 18/06/2022
   267 000000FD FEC0                    	inc	al ; tty number + 1
   268 000000FF 3A05[5C020000]          	cmp	al, [chrin]
   269 00000105 7502                    	jne	short cat_b
   270                                  	; input (source) file and stdin (console tty) is same
   271 00000107 29F6                    	sub	esi, esi ; 0
   272                                  cat_b:	
   273                                  	; 15/06/2022
   274 00000109 21DB                    	and	ebx, ebx ; is there a waiting char ?
   275 0000010B 7524                    	jnz	short cat_x ; yes
   276                                  
   277 0000010D 08F6                    	or	dh, dh  ; is stdin a character device (tty) ?
   278 0000010F 752A                    	jnz	short cat_p
   279                                  			; no, stdin is a file or block device
   280                                  
   281                                  	; is there a file to read ?
   282 00000111 09F6                    	or	esi, esi ; 05/03/2022
   283 00000113 7556                    	jnz	short cat_r ; yes
   284                                  
   285                                  	; edx = [stdinrw]
   286 00000115 B9[68020000]            	mov	ecx, iobuf
   287                                  	; ebx = 0
   288                                  	sys	_read	; 18/06/2022
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77                              <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79                              <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 0000011A B803000000          <1>  mov eax, %1
    86 0000011F CD30                <1>  int 30h
   289                                  	;sys	_read, 0, iobuf
   290                                  		; read one char into [iobuf]
   291                                  	;jc	short cat_6 ; (ebx = 0)
   292                                  	; 18/06/2022
   293 00000121 722B                    	jc	short cat_d
   294                                  
   295 00000123 803D[68020000]1B        	cmp	byte [iobuf], 1Bh ; 27 ; ESCape key ?
   296 0000012A 7579                    	jne	short cat_c 
   297                                  
   298 0000012C E9F6000000              	jmp	cat_exit ; yes, exit
   299                                  
   300                                  cat_x:
   301 00000131 80FB1B                  	cmp	bl, 1Bh ; 27 ; ESCape key ?
   302                                  	;je	short cat_exit ; yes, exit
   303                                  	; 18/06/2022
   304 00000134 7505                    	jne	short cat_p
   305 00000136 E9C4000000              	jmp	cat_q
   306                                  cat_p:
   307                                  	; edx = [stdinrw]
   308                                  	sys	_read, 0, iobuf
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 0000013B BB00000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000140 B9[68020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000145 B803000000          <1>  mov eax, %1
    86 0000014A CD30                <1>  int 30h
   309                                  		; read one char into [iobuf]
   310                                  	;jc	short cat_6 ; (ebx = 0)
   311                                  	; 18/06/2022
   312 0000014C 7305                    	jnc	short cat_s
   313                                  cat_d:
   314 0000014E E997000000              	jmp	cat_6
   315                                  cat_s:	
   316                                  	; eax = read count
   317                                  	; read (redirected) stdin at first then other files 
   318 00000153 09C0                    	or	eax, eax
   319                                  	;jnz	short cat_w ; write bytes in buffer
   320                                  	; 18/06/2022
   321 00000155 7406                    	jz	short cat_u
   322 00000157 20F6                    	and	dh, dh
   323 00000159 744A                    	jz	short cat_c ; console tty (crlf check)
   324 0000015B EB78                    	jmp	short cat_w
   325                                  cat_u:
   326                                  	; 16/06/2022
   327                                  	; trick to skip reading stdin file
   328                                  	;xor	edx, edx
   329                                  	; edx = 0
   330                                  	;mov	[stdinrw], edx  ; end of stdin file
   331                                  	; 18/06/2022
   332 0000015D A3[60020000]            	mov	[stdinrw], eax  ; 0 ; end of stdin file
   333                                  	;jmp	short cat_c ; read (input) file
   334                                  
   335                                  	; is there a file to read ?
   336 00000162 21F6                    	and	esi, esi
   337                                  	;jz	short cat_7 ; no
   338                                  	; 18/06/2022
   339 00000164 7505                    	jnz	short cat_r
   340 00000166 E98C000000              	jmp	cat_7 
   341                                  
   342                                  cat_r:
   343                                  	; 18/06/2022
   344 0000016B 8A2D[5C020000]          	mov	ch, [chrin]
   345 00000171 20ED                    	and	ch, ch
   346 00000173 7446                    	jz	short cat_m ; not a tty file
   347 00000175 30C9                    	xor	cl, cl ; 0
   348 00000177 29DB                    	sub	ebx, ebx ; 0	
   349                                  	sys	_gtty	; get keyboard status of tty
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77                              <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79                              <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000179 B820000000          <1>  mov eax, %1
    86 0000017E CD30                <1>  int 30h
   350 00000180 09DB                    	or	ebx, ebx
   351                                  	;jz	short cat_n ; check for console keystroke
   352                                  	; 18/06/2022
   353 00000182 7505                    	jnz	short cat_e
   354 00000184 E969FFFFFF              	jmp	cat_n	
   355                                  
   356                                  cat_e:
   357                                  	sys	_read, esi, iobuf, 1
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000189 89F3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 0000018B B9[68020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 00000190 BA01000000          <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000195 B803000000          <1>  mov eax, %1
    86 0000019A CD30                <1>  int 30h
   358 0000019C 803D[68020000]1B        	cmp	byte [iobuf], 1Bh ; 27 ; ESCape key ?
   359 000001A3 7449                    	je	short cat_k ; close file
   360                                  
   361                                  cat_c:
   362                                  	; is there a file to read ?
   363                                  	;or	esi, esi ; 05/03/2022
   364                                  	;jnz	short cat_r ; yes
   365                                  
   366                                  	; 16/06/2022
   367                                  	;and	edx, edx
   368                                  	;jz	short cat_exit ; end of stdin file
   369                                  
   370                                  	;xor	eax, eax
   371                                  	;inc	al
   372 000001A5 B001                    	mov	al, 1
   373                                  	; eax = 1	
   374 000001A7 803D[68020000]0D        	cmp	byte [iobuf], 0Dh ; carriage return ?
   375 000001AE 7525                    	jne	short cat_w
   376 000001B0 C605[69020000]0A        	mov	byte [iobuf+1], 0Ah ; line feed
   377 000001B7 FEC0                    	inc	al
   378                                  	; eax = 2
   379 000001B9 EB1A                    	jmp	short cat_w
   380                                  
   381                                  cat_m:
   382                                  	; 05/03/2022
   383                                  	;mov	eax, [esi] ; file descriptor/number
   384                                  	;;mov	ax, [esi]
   385                                  	;
   386                                  	;sys	_read, eax, iobuf, 512 ; 16/07/2015
   387                                  	;;sys 	_read, eax, ibuf, 512 
   388                                  	;jc	short cat_6
   389                                  	; 15/06/2022
   390                                  	; 05/03/2022
   391                                  	; esi = file descriptor/number
   392                                  	sys	_read, esi, iobuf, [filerwc]
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000001BB 89F3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 000001BD B9[68020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 000001C2 8B15[64020000]      <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000001C8 B803000000          <1>  mov eax, %1
    86 000001CD CD30                <1>  int 30h
   393                                  		; read 512 chars into [iobuf]
   394                                  	;jc	short cat_6
   395 000001CF 721D                    	jc	short cat_k ; 05/03/2022
   396                                  
   397                                  	; NOTE: If input file is a tty (keyboard)
   398                                  	;	only 1 byte will be read, by ignoring
   399                                  	;	byte count (512).
   400                                  	;	Retro UNIX 8086 v1 kernel ('rtty')
   401                                  	;	has been modified fot that.
   402                                  	;       Erdogan Tan (16/07/2015)
   403                                  	;
   404                                  
   405                                  	; 15/06/2022
   406 000001D1 21C0                    	and	eax, eax ; EAX = 1 for tty (keyboard)
   407                                  	;jz	short cat_6
   408 000001D3 7419                    	jz	short cat_k ; 05/03/2022
   409                                  
   410                                  ;	push	esi
   411                                  	;mov	esi, ibuf
   412                                  ;	mov	esi, ecx ; offset ibuf
   413                                  ;	mov	ecx, eax
   414                                  		;;mov	fin,r0
   415                                  		;;sys	read; ibuf; 512.
   416                                  		;;bes	3f
   417                                  		;;mov	r0,r4
   418                                  		;;beq	3f
   419                                  		;;mov	$ibuf,r3
   420                                  	 ; 16/07/2015
   421                                  ;	mov	edx, eax
   422                                  ;	;add	edx, obuf
   423                                  ;cat_4:
   424                                  ;	;;4:
   425                                  ;	lodsb
   426                                  	;call	putc
   427                                  cat_w:
   428                                  	; 16/07/2015
   429                                  	; write to console tty (stdout)
   430                                  	sys 	_write, 1, iobuf, eax
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000001D5 BB01000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 000001DA B9[68020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 000001DF 89C2                <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000001E1 B804000000          <1>  mov eax, %1
    86 000001E6 CD30                <1>  int 30h
   431                                  	;jc	short cat_6
   432                                  	; 05/03/2022
   433                                  	;jnc	short cat_3
   434                                  	; 15/06/2022
   435 000001E8 7347                    	jnc	short cat_8
   436                                  
   437                                  ;	loop	cat_4
   438                                  ;	pop	esi
   439                                  cat_5:
   440                                  	;mov	eax, [esi] ; 05/03/2022
   441                                  	;;mov	ax, [esi]
   442                                  	;jmp	short cat_3
   443                                  		;;movb	(r3)+,r0
   444                                  		;;jsr	pc,putc
   445                                  		;;dec	r4
   446                                  		;;bne	4b
   447                                  		;;br	3b
   448                                  	;; 05/03/2022
   449                                  	;; ebx = file descriptor/input
   450                                  	;;and	ebx, ebx ; console input ?
   451                                  	;;jnz	short cat_3 ; no, file input
   452                                  	;;cmp	byte [quit], al ; 0
   453                                  	;;ja	short cat_7 ; ebx = 0
   454                                  	;;jmp	short cat_z
   455                                  	; 05/03/2022
   456                                  	;jmp	short cat_3
   457                                  		
   458                                  cat_6:	;;3:
   459                                  	; 05/03/2022
   460                                  	; ebx = file descriptor/number
   461                                  	;movzx	ebx, word [esi]
   462                                  	;
   463 000001EA 09F6                    	or	esi, esi
   464 000001EC 7409                    	jz	short cat_7
   465                                  cat_k:
   466                                  	sys	_close, esi
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000001EE 89F3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79                              <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000001F0 B806000000          <1>  mov eax, %1
    86 000001F5 CD30                <1>  int 30h
   467                                  	;
   468                                  	;or	ebx, ebx
   469                                  	;jz	short cat_7
   470                                  	;sys 	_close
   471                                  		;;mov	fin,r0
   472                                  		;;beq	loop
   473                                  		;;sys	close
   474                                  		;;br	loop
   475                                  cat_7:	
   476                                  	;;loop:
   477 000001F7 4D                      	dec	ebp
   478                                  	;;jz	short cat_8
   479                                  	; 28/12/2015
   480                                  	;jg	short cat_0
   481                                  	; 05/03/2022
   482 000001F8 7E2D                    	jng	short cat_exit
   483 000001FA E94BFEFFFF              	jmp	cat_0
   484                                  
   485                                  	; 18/06/2022 (ESCape)
   486                                  cat_q:
   487                                  	; open console tty for read
   488                                  	sys	_open, consoletty, 0
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000001FF BB[50020000]        <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000204 B900000000          <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000209 B805000000          <1>  mov eax, %1
    86 0000020E CD30                <1>  int 30h
   489 00000210 7215                    	jc	short cat_exit
   490                                  	sys	_read, eax, iobuf
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000212 89C3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000214 B9[68020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000219 B803000000          <1>  mov eax, %1
    86 0000021E CD30                <1>  int 30h
   491                                  	sys	_close
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77                              <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79                              <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000220 B806000000          <1>  mov eax, %1
    86 00000225 CD30                <1>  int 30h
   492                                  
   493                                  	;jmp	short cat_exit	
   494                                  
   495                                  cat_exit:
   496                                  	sys	_exit
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77                              <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79                              <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000227 B801000000          <1>  mov eax, %1
    86 0000022C CD30                <1>  int 30h
   497                                  here:
   498 0000022E 90                      	nop
   499 0000022F EBFD                    	jmp	short here
   500                                  
   501                                  cat_8:
   502                                  	; 15/06/2022
   503 00000231 39D0                    	cmp	eax, edx
   504 00000233 72B5                    	jb	short cat_6
   505 00000235 8B15[60020000]          	mov	edx, [stdinrw]
   506 0000023B 09D2                    	or 	edx, edx  ; end of stdin file
   507 0000023D 7405                    	jz	short cat_9
   508 0000023F E9AEFEFFFF              	jmp	cat_n
   509                                  
   510                                  cat_9:
   511                                  	; 16/06/2022
   512 00000244 21F6                    	and	esi, esi
   513 00000246 74A6                    	jz	short cat_k
   514 00000248 E91EFFFFFF              	jmp	cat_r ; 18/06/2022
   515                                  
   516                                  ;cat_8:
   517                                  ;	;;done:
   518                                  ;	sub	di, obuf
   519                                  ;	jz	short cat_9
   520                                  ;	sys	_write, 1, obuf, di 
   521                                  		;;sub	$obuf,r2
   522                                  		;;beq	1f
   523                                  		;;mov	r2,0f
   524                                  		;;mov	$1,r0
   525                                  		;;sys	write; obuf; 0:..
   526                                  ;cat_9:	
   527                                  ;	;;1:
   528                                  ;	sys	_exit
   529                                  		;;sys	exit
   530                                  	;;
   531                                  ;putc:	
   532                                  ;	;;putc:
   533                                  ;	stosb
   534                                  ;	cmp	di, dx ; 16/07/2015
   535                                  	;cmp	di, obuf + 512
   536                                  ;	jb	short cat_10
   537                                  ;	push	cx
   538                                  	 ; 16/07/2015
   539                                  ;	mov	di, obuf
   540                                  ;	sub	dx, di ; byte (char) count
   541                                  	; 
   542                                  ;	sys 	_write, 1, obuf
   543                                  	;sys 	_write, 1, obuf, 512
   544                                  	;mov	di, obuf
   545                                  		;;movb	r0,(r2)+
   546                                  		;;cmp	r2,$obuf+512.
   547                                  		;;blo	1f
   548                                  		;;mov	$1,r0
   549                                  		;;sys	write; obuf; 512.
   550                                  		;;mov	$obuf,r2
   551                                  ;	pop	cx
   552                                  ;cat_10:	
   553                                  	;;1:
   554                                  ;	retn
   555                                  		;;rts	pc
   556                                  
   557 0000024D 0D0A00                  nl:	db 0Dh, 0Ah, 0
   558                                  
   559                                  ;; 05/03/2022
   560                                  ;quit:	db 0
   561                                  consoletty:
   562 00000250 2F6465762F74747900      	db '/dev/tty', 0 ; (+)
   563                                  
   564 00000259 90<rep 3h>              align 4
   565                                  
   566                                  bss_start:
   567                                  
   568                                  ABSOLUTE bss_start
   569                                  
   570                                  ; 18/06/2022
   571 0000025C ??                      chrin:	resb 1
   572 0000025D ??????                  	resb 3
   573                                  
   574                                  ; 15/06/2022
   575 00000260 ????????                stdinrw: resd 1
   576 00000264 ????????                filerwc: resd 1
   577                                  
   578 00000268 <res 200h>              iobuf:  resb 512
   579                                  ;ibuf:	resb 512
   580                                  ;obuf:	resb 512
   581                                  ;;fin:	resw 1
   582                                  ;fin:	resd 1 ; 05/03/2022	
   583                                  		;;.bss
   584                                  		;;ibuf:	.=.+512.
   585                                  		;;obuf:	.=.+512.
   586                                  		;;fin:	.=.+2
   587                                  		;;.text
   588                                  ;bss_end:
