     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: 19/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                                  ENTERKEY  equ 0Dh
   106                                  NEXTLINE  equ 0Ah
   107                                  BACKSPACE equ 08h 
   108                                  
   109                                  ; Retro UNIX 386 v1 system call format:
   110                                  ; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
   111                                  
   112                                  [BITS 32] ; We need 32-bit intructions for protected mode
   113                                  
   114                                  [ORG 0] 
   115                                  
   116                                  START_CODE:
   117                                  	;; / cat -- concatenate files
   118                                  
   119                                  	;sys	_write, 1, nl, 2
   120                                  
   121                                  	; 18/06/2022
   122                                  	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[B0030000]        <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
   123 00000011 E867000000              	call	rwcount
   124 00000016 8815[9C030000]          	mov	[stdout], dl ; 19/06/2022
   125 0000001C 20D2                    	and 	dl, dl
   126 0000001E 7424                    	jz	short cat_@ ; block device or regular file
   127 00000020 803D[B0030000]09        	cmp	byte [iobuf], 9  ; /dev/lpr
   128 00000027 741B                    	je	short cat_@
   129                                  	; /dev/tty0 .. /dev/tty9
   130                                  	; next line
   131                                  	sys	_write, 1, nl, 2 
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000029 BB01000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 0000002E B9[8C030000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 00000033 BA02000000          <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000038 B804000000          <1>  mov eax, %1
    86 0000003D CD30                <1>  int 30h
   132 0000003F B9[B0030000]            	mov	ecx, iobuf
   133                                  cat_@:
   134 00000044 5D                      	pop	ebp
   135 00000045 5A                      	pop	edx
   136                                  	; esi = 0 ; ('sysexec' sets regs to zero)
   137                                  	; 05/03/2022
   138                                  	;mov	esi, fin 
   139                                  	;mov    edi, obuf
   140                                  	;EAX = 2 (written byte count)
   141 00000046 30C0                    	xor	al, al ; 0
   142                                          ; 18/06/2022  
   143                                  	;mov	ecx, iobuf ; 17/06/2022
   144 00000048 4D                      	dec     ebp
   145                                  	;jz	short cat_3	
   146                                  		;;mov	(sp)+,r5
   147                                  		;;tst	(sp)+
   148                                  		;;mov	$obuf,r2
   149                                  		;;cmp	r5,$1
   150                                  		;;beq	3f
   151                                  	; 18/06/ 2022
   152                                  	;jnz	short cat_0
   153                                  ;cat_y:
   154                                  	;jmp	cat_3
   155                                  	; 19/06/2022
   156 00000049 7475                    	jz	short cat_10
   157                                  cat_0:	
   158 0000004B 5B                      	pop	ebx
   159 0000004C 803B2D                  	cmp	byte [ebx], '-'
   160                                  	;je	short cat_3 ; 16/06/2022
   161                                  		;;dec	r5
   162                                  		;;ble	done
   163                                  		;;mov	(sp)+,r0
   164                                  		;;cmpb	(r0),$'-
   165                                  		;;bne	2f
   166                                  		;;clr	fin
   167                                  		;;br	3f
   168                                  	; 18/06/2022
   169                                  	;je	short cat_y
   170 0000004F 7505                    	jne	short cat_1
   171 00000051 E986000000              	jmp	cat_3
   172                                  cat_1:
   173                                  	;;2:
   174                                  	; ebx = file name offset
   175 00000056 31C9                    	xor 	ecx, ecx ; 0
   176                                  	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 00000058 B805000000          <1>  mov eax, %1
    86 0000005D CD30                <1>  int 30h
   177                                  	;jc	short cat_7
   178                                  	; 05/03/2022 ; (+)
   179 0000005F 7305                    	jnc	short cat_2
   180 00000061 E9A3010000              	jmp	cat_7
   181                                  cat_2:
   182                                  	; 05/03/2022
   183 00000066 89C6                    	mov	esi, eax
   184                                  	;mov	[esi], eax ; 05/03/2022
   185                                  	;mov	[esi], ax
   186                                  		;;mov	r0,0f
   187                                  		;;sys	open; 0:..; 0
   188                                  		;;bes	loop
   189                                  		;;mov	r0,fin
   190                                  
   191                                  	; 05/03/2022 ; (+)
   192                                  	; convert user's file number to inode number
   193                                  	; (get 34 byte inode details, inode num + inode)
   194                                  	; (get 66 byte inode details for runix 386 v1.2)
   195                                  	sys	_fstat, esi, iobuf 
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000068 89F3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 0000006A B9[B0030000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 0000006F B81C000000          <1>  mov eax, %1
    86 00000074 CD30                <1>  int 30h
   196                                  	;jc	short cat_7
   197                                  	; 05/03/2022 ; (+)
   198 00000076 7321                    	jnc	short cat_f
   199 00000078 E983010000              	jmp	cat_k ; 15/06/2022
   200                                  
   201                                  rwcount:
   202                                  	; 17/06/2022 - Retro UNIX 386 v1 (& v1.1)
   203 0000007D 31D2                    	xor	edx, edx
   204 0000007F 66A1[B0030000]          	mov	ax, [iobuf] ; inode number
   205 00000085 6683F829                	cmp	ax, 41 ; regular file (or directory) ?
   206 00000089 730B                    	jnb	short rwc_2 ; yes
   207                                  
   208 0000008B 3C08                    	cmp	al, 8 ; > hd3 inode number ?
   209 0000008D 7704                    	ja	short rwc_1 ; yes ; character device
   210                                  
   211 0000008F 3C03                    	cmp	al, 3 ; >= fd0 inode number ?
   212 00000091 7303                    	jnb	short rwc_2 ; yes ; block device
   213                                  rwc_1:
   214                                  	; no, character device
   215                                  	; read/write count = 1
   216 00000093 FEC2                    	inc	dl
   217 00000095 C3                      	retn
   218                                  rwc_2:
   219                                  	; regular file or block device
   220                                  	; read/write count = 512
   221 00000096 B602                    	mov	dh, 2 ; edx = 512
   222 00000098 C3                      	retn
   223                                  	
   224                                  cat_f:
   225                                  	; 15/06/2022
   226 00000099 E8DFFFFFFF              	call	rwcount
   227 0000009E 8915[AC030000]          	mov	[filerwc], edx
   228                                  
   229                                  	; 18/06/2022
   230                                  	; (check if input file is a tty)
   231 000000A4 C605[9D030000]00        	mov	byte [chrin], 0
   232 000000AB 20D2                    	and	dl, dl
   233 000000AD 742D                    	jz	short cat_3  ; not a character device
   234                                  	;mov	ax, [iobuf]
   235 000000AF A0[B0030000]            	mov	al, [iobuf] ; inode number
   236 000000B4 3C13                    	cmp	al, 19 ; tty9
   237 000000B6 7724                    	ja	short cat_3
   238 000000B8 3C0A                    	cmp	al, 10 ; tty0
   239 000000BA 7319                    	jnb	short cat_g
   240                                  
   241                                  	; [chrin] = 0
   242 000000BC 3C01                    	cmp	al, 1  ; /dev/tty
   243 000000BE 751C                    	jne	short cat_3
   244                                  cat_10:
   245                                  	sys	_gtty, 0, 1  ; get status of console tty (w)
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000000C0 BB00000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 000000C5 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 000000CA B820000000          <1>  mov eax, %1
    86 000000CF CD30                <1>  int 30h
   246 000000D1 FEC0                    	inc	al  ; console tty number + 1
   247 000000D3 EB02                    	jmp	short cat_h
   248                                  cat_g:
   249 000000D5 2C09                    	sub 	al, 9
   250                                  cat_h:
   251 000000D7 A2[9D030000]            	mov	[chrin], al
   252                                  	; [chrin] = tty number + 1
   253                                  
   254                                  cat_3:	
   255                                  	; 05/03/2022 ; (+)
   256                                  	; get inode number of current tty (stdin)
   257                                  	; (get 34 byte inode details, inode num + inode)
   258                                  	; (get 66 byte inode details for runix 386 v1.2)
   259                                  	;sys	_fstat, 0, iobuf 
   260                                  	;;jc	short cat_n
   261                                  	;mov	ecx, iobuf	
   262                                  	sys	_fstat, 0  ; get stdin file inode details
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000000DC 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 000000E1 B81C000000          <1>  mov eax, %1
    86 000000E6 CD30                <1>  int 30h
   263                                  
   264                                  	; 15/06/2022
   265 000000E8 E890FFFFFF              	call	rwcount
   266 000000ED 8915[A8030000]          	mov	[stdinrw], edx
   267                                  
   268                                  cat_n:	;;3:
   269                                  	; get keyboard status of console tty
   270 000000F3 31DB                    	xor	ebx, ebx  ; 0
   271 000000F5 31C9                    	xor	ecx, ecx ; 0
   272                                  	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 000000F7 B820000000          <1>  mov eax, %1
    86 000000FC CD30                <1>  int 30h
   273                                  	; 19/06/2022
   274 000000FE A2[9F030000]            	mov	[consol], al
   275                                  	; 18/06/2022
   276 00000103 FEC0                    	inc	al ; tty number + 1
   277 00000105 3A05[9D030000]          	cmp	al, [chrin]
   278 0000010B 750E                    	jne	short cat_b
   279                                  	; input (source) file and stdin (console tty) is same
   280                                  	;sub	esi, esi ; 0
   281                                  ;cat_l:
   282                                  	; 19/06/2022
   283 0000010D F605[9C030000]01        	test	byte [stdout], 1
   284 00000114 7505                    	jnz	short cat_b ; stdout is not a file		
   285                                  
   286                                  	;call	writeline
   287                                  	;cmp	al, 1Bh ; ESCape
   288                                  	;jne	short cat_l
   289                                  	;jmp	cat_exit
   290                                  	; 19/06/2022
   291 00000116 E944010000              	jmp	writeline 
   292                                  		; write tty/user input line(s) to file
   293                                  cat_b:	
   294                                  	; 15/06/2022
   295 0000011B 21DB                    	and	ebx, ebx ; is there a waiting char ?
   296 0000011D 7524                    	jnz	short cat_x ; yes
   297                                  
   298 0000011F 08F6                    	or	dh, dh  ; is stdin a character device (tty) ?
   299 00000121 752A                    	jnz	short cat_p
   300                                  			; no, stdin is a file or block device
   301                                  
   302                                  	; is there a file to read ?
   303 00000123 09F6                    	or	esi, esi ; 05/03/2022
   304 00000125 7556                    	jnz	short cat_r ; yes
   305                                  
   306                                  	; edx = [stdinrw]
   307 00000127 B9[B0030000]            	mov	ecx, iobuf
   308                                  	; ebx = 0
   309                                  	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 0000012C B803000000          <1>  mov eax, %1
    86 00000131 CD30                <1>  int 30h
   310                                  	;sys	_read, 0, iobuf
   311                                  		; read one char into [iobuf]
   312                                  	;jc	short cat_6 ; (ebx = 0)
   313                                  	; 18/06/2022
   314 00000133 722B                    	jc	short cat_d
   315                                  
   316 00000135 803D[B0030000]1B        	cmp	byte [iobuf], 1Bh ; 27 ; ESCape key ?
   317 0000013C 7579                    	jne	short cat_c 
   318                                  
   319 0000013E E9F6000000              	jmp	cat_exit ; yes, exit
   320                                  
   321                                  cat_x:
   322 00000143 80FB1B                  	cmp	bl, 1Bh ; 27 ; ESCape key ?
   323                                  	;je	short cat_exit ; yes, exit
   324                                  	; 18/06/2022
   325 00000146 7505                    	jne	short cat_p
   326 00000148 E9C4000000              	jmp	cat_q
   327                                  cat_p:
   328                                  	; edx = [stdinrw]
   329                                  	sys	_read, 0, iobuf
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 0000014D BB00000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000152 B9[B0030000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000157 B803000000          <1>  mov eax, %1
    86 0000015C CD30                <1>  int 30h
   330                                  		; read one char into [iobuf]
   331                                  	;jc	short cat_6 ; (ebx = 0)
   332                                  	; 18/06/2022
   333 0000015E 7305                    	jnc	short cat_s
   334                                  cat_d:
   335 00000160 E997000000              	jmp	cat_6
   336                                  cat_s:	
   337                                  	; eax = read count
   338                                  	; read (redirected) stdin at first then other files 
   339 00000165 09C0                    	or	eax, eax
   340                                  	;jnz	short cat_w ; write bytes in buffer
   341                                  	; 18/06/2022
   342 00000167 7406                    	jz	short cat_u
   343 00000169 20F6                    	and	dh, dh
   344 0000016B 744A                    	jz	short cat_c ; console tty (crlf check)
   345 0000016D EB78                    	jmp	short cat_w
   346                                  cat_u:
   347                                  	; 16/06/2022
   348                                  	; trick to skip reading stdin file
   349                                  	;xor	edx, edx
   350                                  	; edx = 0
   351                                  	;mov	[stdinrw], edx  ; end of stdin file
   352                                  	; 18/06/2022
   353 0000016F A3[A8030000]            	mov	[stdinrw], eax  ; 0 ; end of stdin file
   354                                  	;jmp	short cat_c ; read (input) file
   355                                  
   356                                  	; is there a file to read ?
   357 00000174 21F6                    	and	esi, esi
   358                                  	;jz	short cat_7 ; no
   359                                  	; 18/06/2022
   360 00000176 7505                    	jnz	short cat_r
   361 00000178 E98C000000              	jmp	cat_7 
   362                                  
   363                                  cat_r:
   364                                  	; 18/06/2022
   365 0000017D 8A2D[9D030000]          	mov	ch, [chrin]
   366 00000183 20ED                    	and	ch, ch
   367 00000185 7446                    	jz	short cat_m ; not a tty file
   368 00000187 30C9                    	xor	cl, cl ; 0
   369 00000189 29DB                    	sub	ebx, ebx ; 0	
   370                                  	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 0000018B B820000000          <1>  mov eax, %1
    86 00000190 CD30                <1>  int 30h
   371 00000192 09DB                    	or	ebx, ebx
   372                                  	;jz	short cat_n ; check for console keystroke
   373                                  	; 18/06/2022
   374 00000194 7505                    	jnz	short cat_e
   375 00000196 E958FFFFFF              	jmp	cat_n	
   376                                  
   377                                  cat_e:
   378                                  	sys	_read, esi, iobuf, 1
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 0000019B 89F3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 0000019D B9[B0030000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 000001A2 BA01000000          <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000001A7 B803000000          <1>  mov eax, %1
    86 000001AC CD30                <1>  int 30h
   379 000001AE 803D[B0030000]1B        	cmp	byte [iobuf], 1Bh ; 27 ; ESCape key ?
   380 000001B5 7449                    	je	short cat_k ; close file
   381                                  
   382                                  cat_c:
   383                                  	; is there a file to read ?
   384                                  	;or	esi, esi ; 05/03/2022
   385                                  	;jnz	short cat_r ; yes
   386                                  
   387                                  	; 16/06/2022
   388                                  	;and	edx, edx
   389                                  	;jz	short cat_exit ; end of stdin file
   390                                  
   391                                  	;xor	eax, eax
   392                                  	;inc	al
   393 000001B7 B001                    	mov	al, 1
   394                                  	; eax = 1	
   395 000001B9 803D[B0030000]0D        	cmp	byte [iobuf], 0Dh ; carriage return ?
   396 000001C0 7525                    	jne	short cat_w
   397 000001C2 C605[B1030000]0A        	mov	byte [iobuf+1], 0Ah ; line feed
   398 000001C9 FEC0                    	inc	al
   399                                  	; eax = 2
   400 000001CB EB1A                    	jmp	short cat_w
   401                                  
   402                                  cat_m:
   403                                  	; 05/03/2022
   404                                  	;mov	eax, [esi] ; file descriptor/number
   405                                  	;;mov	ax, [esi]
   406                                  	;
   407                                  	;sys	_read, eax, iobuf, 512 ; 16/07/2015
   408                                  	;;sys 	_read, eax, ibuf, 512 
   409                                  	;jc	short cat_6
   410                                  	; 15/06/2022
   411                                  	; 05/03/2022
   412                                  	; esi = file descriptor/number
   413                                  	sys	_read, esi, iobuf, [filerwc]
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000001CD 89F3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 000001CF B9[B0030000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 000001D4 8B15[AC030000]      <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000001DA B803000000          <1>  mov eax, %1
    86 000001DF CD30                <1>  int 30h
   414                                  		; read 512 chars into [iobuf]
   415                                  	;jc	short cat_6
   416 000001E1 721D                    	jc	short cat_k ; 05/03/2022
   417                                  
   418                                  	; NOTE: If input file is a tty (keyboard)
   419                                  	;	only 1 byte will be read, by ignoring
   420                                  	;	byte count (512).
   421                                  	;	Retro UNIX 8086 v1 kernel ('rtty')
   422                                  	;	has been modified fot that.
   423                                  	;       Erdogan Tan (16/07/2015)
   424                                  	;
   425                                  
   426                                  	; 15/06/2022
   427 000001E3 21C0                    	and	eax, eax ; EAX = 1 for tty (keyboard)
   428                                  	;jz	short cat_6
   429 000001E5 7419                    	jz	short cat_k ; 05/03/2022
   430                                  
   431                                  ;	push	esi
   432                                  	;mov	esi, ibuf
   433                                  ;	mov	esi, ecx ; offset ibuf
   434                                  ;	mov	ecx, eax
   435                                  		;;mov	fin,r0
   436                                  		;;sys	read; ibuf; 512.
   437                                  		;;bes	3f
   438                                  		;;mov	r0,r4
   439                                  		;;beq	3f
   440                                  		;;mov	$ibuf,r3
   441                                  	 ; 16/07/2015
   442                                  ;	mov	edx, eax
   443                                  ;	;add	edx, obuf
   444                                  ;cat_4:
   445                                  ;	;;4:
   446                                  ;	lodsb
   447                                  	;call	putc
   448                                  cat_w:
   449                                  	; 16/07/2015
   450                                  	; write to console tty (stdout)
   451                                  	sys 	_write, 1, iobuf, eax
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000001E7 BB01000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 000001EC B9[B0030000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 000001F1 89C2                <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000001F3 B804000000          <1>  mov eax, %1
    86 000001F8 CD30                <1>  int 30h
   452                                  	;jc	short cat_6
   453                                  	; 05/03/2022
   454                                  	;jnc	short cat_3
   455                                  	; 15/06/2022
   456 000001FA 7347                    	jnc	short cat_8
   457                                  
   458                                  ;	loop	cat_4
   459                                  ;	pop	esi
   460                                  cat_5:
   461                                  	;mov	eax, [esi] ; 05/03/2022
   462                                  	;;mov	ax, [esi]
   463                                  	;jmp	short cat_3
   464                                  		;;movb	(r3)+,r0
   465                                  		;;jsr	pc,putc
   466                                  		;;dec	r4
   467                                  		;;bne	4b
   468                                  		;;br	3b
   469                                  	;; 05/03/2022
   470                                  	;; ebx = file descriptor/input
   471                                  	;;and	ebx, ebx ; console input ?
   472                                  	;;jnz	short cat_3 ; no, file input
   473                                  	;;cmp	byte [quit], al ; 0
   474                                  	;;ja	short cat_7 ; ebx = 0
   475                                  	;;jmp	short cat_z
   476                                  	; 05/03/2022
   477                                  	;jmp	short cat_3
   478                                  		
   479                                  cat_6:	;;3:
   480                                  	; 05/03/2022
   481                                  	; ebx = file descriptor/number
   482                                  	;movzx	ebx, word [esi]
   483                                  	;
   484 000001FC 09F6                    	or	esi, esi
   485 000001FE 7409                    	jz	short cat_7
   486                                  cat_k:
   487                                  	sys	_close, esi
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000200 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 00000202 B806000000          <1>  mov eax, %1
    86 00000207 CD30                <1>  int 30h
   488                                  	;
   489                                  	;or	ebx, ebx
   490                                  	;jz	short cat_7
   491                                  	;sys 	_close
   492                                  		;;mov	fin,r0
   493                                  		;;beq	loop
   494                                  		;;sys	close
   495                                  		;;br	loop
   496                                  cat_7:	
   497                                  	;;loop:
   498 00000209 4D                      	dec	ebp
   499                                  	;;jz	short cat_8
   500                                  	; 28/12/2015
   501                                  	;jg	short cat_0
   502                                  	; 05/03/2022
   503 0000020A 7E2D                    	jng	short cat_exit
   504 0000020C E93AFEFFFF              	jmp	cat_0
   505                                  
   506                                  	; 18/06/2022 (ESCape)
   507                                  cat_q:
   508                                  	; open console tty for read
   509                                  	sys	_open, consoletty, 0
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000211 BB[8F030000]        <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000216 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 0000021B B805000000          <1>  mov eax, %1
    86 00000220 CD30                <1>  int 30h
   510 00000222 7215                    	jc	short cat_exit
   511                                  	sys	_read, eax, iobuf
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000224 89C3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000226 B9[B0030000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 0000022B B803000000          <1>  mov eax, %1
    86 00000230 CD30                <1>  int 30h
   512                                  	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 00000232 B806000000          <1>  mov eax, %1
    86 00000237 CD30                <1>  int 30h
   513                                  
   514                                  	;jmp	short cat_exit	
   515                                  
   516                                  cat_exit:
   517                                  	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 00000239 B801000000          <1>  mov eax, %1
    86 0000023E CD30                <1>  int 30h
   518                                  here:
   519 00000240 90                      	nop
   520 00000241 EBFD                    	jmp	short here
   521                                  
   522                                  cat_8:
   523                                  	; 15/06/2022
   524 00000243 39D0                    	cmp	eax, edx
   525 00000245 72B5                    	jb	short cat_6
   526 00000247 8B15[A8030000]          	mov	edx, [stdinrw]
   527 0000024D 09D2                    	or 	edx, edx  ; end of stdin file
   528 0000024F 7405                    	jz	short cat_9
   529 00000251 E99DFEFFFF              	jmp	cat_n
   530                                  
   531                                  cat_9:
   532                                  	; 16/06/2022
   533 00000256 21F6                    	and	esi, esi
   534 00000258 74A6                    	jz	short cat_k
   535 0000025A E91EFFFFFF              	jmp	cat_r ; 18/06/2022
   536                                  
   537                                  ;cat_8:
   538                                  ;	;;done:
   539                                  ;	sub	di, obuf
   540                                  ;	jz	short cat_9
   541                                  ;	sys	_write, 1, obuf, di 
   542                                  		;;sub	$obuf,r2
   543                                  		;;beq	1f
   544                                  		;;mov	r2,0f
   545                                  		;;mov	$1,r0
   546                                  		;;sys	write; obuf; 0:..
   547                                  ;cat_9:	
   548                                  ;	;;1:
   549                                  ;	sys	_exit
   550                                  		;;sys	exit
   551                                  	;;
   552                                  ;putc:	
   553                                  ;	;;putc:
   554                                  ;	stosb
   555                                  ;	cmp	di, dx ; 16/07/2015
   556                                  	;cmp	di, obuf + 512
   557                                  ;	jb	short cat_10
   558                                  ;	push	cx
   559                                  	 ; 16/07/2015
   560                                  ;	mov	di, obuf
   561                                  ;	sub	dx, di ; byte (char) count
   562                                  	; 
   563                                  ;	sys 	_write, 1, obuf
   564                                  	;sys 	_write, 1, obuf, 512
   565                                  	;mov	di, obuf
   566                                  		;;movb	r0,(r2)+
   567                                  		;;cmp	r2,$obuf+512.
   568                                  		;;blo	1f
   569                                  		;;mov	$1,r0
   570                                  		;;sys	write; obuf; 512.
   571                                  		;;mov	$obuf,r2
   572                                  ;	pop	cx
   573                                  ;cat_10:	
   574                                  	;;1:
   575                                  ;	retn
   576                                  		;;rts	pc
   577                                  
   578                                  writeline:
   579                                  	; 19/06/2022
   580                                  
   581                                  	; write line(s) to file (with echo)
   582                                  	; stdin = console tty
   583                                  	; stdout = file or another tty
   584                                  
   585                                  	;mov	edi, linebuf
   586                                   
   587                                  	;sys	_gtty, 0, 1  ; get status of console tty (w)	
   588                                  	;jc	short done3
   589                                  	
   590 0000025F A0[9F030000]            	mov	al, [consol]
   591                                  	; al = console tty number
   592 00000264 0430                    	add	al, '0'
   593 00000266 A2[97030000]            	mov	[consoletty+8], al
   594                                  
   595 0000026B C605[9E030000]1B        	mov	byte [chr], 1Bh ; ESCape
   596                                  
   597                                  	sys	_open, consoletty, 0 ; open for read
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000272 BB[8F030000]        <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000277 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 0000027C B805000000          <1>  mov eax, %1
    86 00000281 CD30                <1>  int 30h
   598                                  	;jc	short done3
   599 00000283 7305                    	jnc	short G0
   600 00000285 E995000000              	jmp	done3 
   601                                  G0:
   602 0000028A A3[A0030000]            	mov	[consol_r], eax
   603                                  	sys	_open, consoletty, 1 ; open for write
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 0000028F BB[8F030000]        <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000294 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 00000299 B805000000          <1>  mov eax, %1
    86 0000029E CD30                <1>  int 30h
   604 000002A0 7270                    	jc	short done2
   605 000002A2 A3[A4030000]            	mov	[consol_w], eax
   606                                  	sys	_write, [consol_w], crlf, 2
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000002A7 8B1D[A4030000]      <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 000002AD B9[8C030000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 000002B2 BA02000000          <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000002B7 B804000000          <1>  mov eax, %1
    86 000002BC CD30                <1>  int 30h
   607                                  do:
   608 000002BE BF[B0030000]                    mov	edi, linebuf
   609                                  getc:
   610                                  	sys	_read, [consol_r], chr, 1
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000002C3 8B1D[A0030000]      <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 000002C9 B9[9E030000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 000002CE BA01000000          <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000002D3 B803000000          <1>  mov eax, %1
    86 000002D8 CD30                <1>  int 30h
   611 000002DA 7229                    	jc	short done1
   612                                  	;and	eax, eax
   613                                  	;jz	short done1
   614                                  	;
   615 000002DC A0[9E030000]            	mov	al, [chr]
   616                                  
   617 000002E1 3C1B                    	cmp	al, 1Bh ; ESCape key
   618 000002E3 7420                    	je	short done1
   619                                  
   620 000002E5 3C20                    	cmp	al, 20h
   621 000002E7 7256                    	jb	short G1
   622                                  
   623 000002E9 3C7F                    	cmp	al, 127
   624 000002EB 745F                    	je	short G2
   625                                  
   626 000002ED 81FF[00040000]                  cmp	edi, linebuf+80
   627 000002F3 73CE                    	jnb	short getc
   628                                  putc:
   629 000002F5 AA                      	stosb
   630                                  echo:
   631                                  	;sys	_write, [consol_w], chr, 1
   632                                  	; ecx = chr
   633                                  	; edx = 1
   634                                  	sys	_write, [consol_w]
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000002F6 8B1D[A4030000]      <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 000002FC B804000000          <1>  mov eax, %1
    86 00000301 CD30                <1>  int 30h
   635 00000303 EBBE                    	jmp	short getc
   636                                  
   637                                  done1:
   638                                  	sys	_close, [consol_w]
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000305 8B1D[A4030000]      <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 0000030B B806000000          <1>  mov eax, %1
    86 00000310 CD30                <1>  int 30h
   639                                  done2:
   640                                  	sys	_close, [consol_r]
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000312 8B1D[A0030000]      <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 00000318 B806000000          <1>  mov eax, %1
    86 0000031D CD30                <1>  int 30h
   641                                  done3:
   642                                  	;mov	byte [edi], 0
   643                                  	;mov	al, [chr]
   644                                  	;retn
   645                                  
   646 0000031F 89FA                    	mov	edx, edi
   647 00000321 81EA[B0030000]          	sub	edx, linebuf	
   648 00000327 7411                    	jz	short done4
   649                                  
   650                                  	sys	_write, 1, linebuf
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000329 BB01000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 0000032E B9[B0030000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000333 B804000000          <1>  mov eax, %1
    86 00000338 CD30                <1>  int 30h
   651                                  done4:
   652                                  	;sys	_exit
   653 0000033A E9FAFEFFFF              	jmp	cat_exit
   654                                  
   655                                  G1:
   656 0000033F 3C0D                    	cmp	al, ENTERKEY  ; \r (carriage return)
   657 00000341 7414                            je	short G3
   658                                  
   659                                  	;cmp	al, NEXTLINE  ; \n (next line)
   660                                          ;je	short G3
   661                                  
   662 00000343 3C08                    	cmp	al, BACKSPACE ; \b (back space)
   663 00000345 7405                    	je	short G2
   664                                  getch:
   665 00000347 E977FFFFFF              	jmp	getc
   666                                  G2:
   667                                  	; Backspace
   668 0000034C 81FF[B0030000]          	cmp	edi, linebuf
   669 00000352 76F3                    	jna	short getch
   670 00000354 4F                      	dec	edi
   671 00000355 EB9F                    	jmp	short echo
   672                                  
   673                                  G3:
   674                                  	;mov	al, ENTERKEY ; carriage return
   675 00000357 AA                      	stosb
   676 00000358 B00A                    	mov	al, NEXTLINE ; line feed
   677 0000035A AA                      	stosb
   678                                  	sys	_write, [consol_w], crlf, 2
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 0000035B 8B1D[A4030000]      <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000361 B9[8C030000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 00000366 BA02000000          <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 0000036B B804000000          <1>  mov eax, %1
    86 00000370 CD30                <1>  int 30h
   679 00000372 B9[B0030000]            	mov	ecx, linebuf 
   680 00000377 89FA                    	mov	edx, edi
   681 00000379 29CA                    	sub	edx, ecx ; sub edx, linebuf
   682                                  	; ecx = offset crlf
   683                                  	sys	_write, 1
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 0000037B BB01000000          <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 00000380 B804000000          <1>  mov eax, %1
    86 00000385 CD30                <1>  int 30h
   684 00000387 E932FFFFFF              	jmp	do ; next line
   685                                  
   686                                  ; ;;;;
   687                                  
   688                                  crlf:	; 19/06/2022
   689 0000038C 0D0A00                  nl:	db 0Dh, 0Ah, 0
   690                                  
   691                                  ;; 05/03/2022
   692                                  ;quit:	db 0
   693                                  ; 18/06/2022
   694                                  consoletty:
   695 0000038F 2F6465762F74747900-     	db '/dev/tty', 0, 0 ; ; 19/06/2022
   695 00000398 00                 
   696                                  
   697 00000399 90<rep 3h>              align 4
   698                                  
   699                                  bss_start:
   700                                  
   701                                  ABSOLUTE bss_start
   702                                  
   703                                  ; 19/06/2022
   704 0000039C ??                      stdout:	resb 1
   705                                  ; 18/06/2022
   706 0000039D ??                      chrin:	resb 1
   707                                  ; 19/06/2022
   708 0000039E ??                      chr:	resb 1
   709 0000039F ??                      consol:	resb 1
   710 000003A0 ????????                consol_r: resd 1
   711 000003A4 ????????                consol_w: resd 1
   712                                  
   713                                  ; 15/06/2022
   714 000003A8 ????????                stdinrw: resd 1
   715 000003AC ????????                filerwc: resd 1
   716                                  
   717                                  linebuf: ; 19/06/2022	
   718 000003B0 <res 200h>              iobuf:  resb 512
   719                                  ;ibuf:	resb 512
   720                                  ;obuf:	resb 512
   721                                  ;;fin:	resw 1
   722                                  ;fin:	resd 1 ; 05/03/2022	
   723                                  		;;.bss
   724                                  		;;ibuf:	.=.+512.
   725                                  		;;obuf:	.=.+512.
   726                                  		;;fin:	.=.+2
   727                                  		;;.text
   728                                  ;bss_end:
