     1                                  ; ****************************************************************************
     2                                  ; cat386.s (Retro Unix 386 v1.2) - /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 ] -cat3.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 cat3.s -l cat3.txt -o cat3 -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                                  ; 15/06/2022 - Retro UNIX 386 v1.2
    90                                  struc stat
    91                                     ; Retro UNIX v1.2 'sysstat' output !
    92                                     ; (66 bytes)
    93 00000000 ????                       .inode:  resw 1	
    94 00000002 ????                       .mode:   resw 1
    95 00000004 ????                       .nlinks: resw 1 
    96 00000006 ????                       .uid:    resw 1
    97 00000008 ??                         .gid:    resb 1
    98 00000009 ??                         .size_h: resb 1
    99 0000000A ????????                   .size:   resd 1
   100 0000000E <res 28h>                  .dskptr: resd 10
   101 00000036 ????????                   .atime:  resd 1
   102 0000003A ????????                   .mtime:  resd 1
   103 0000003E ????????                   .ctime:  resd 1
   104                                     .strucsize:
   105                                  endstruc
   106                                  
   107                                  ; Retro UNIX 386 v1 system call format:
   108                                  ; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
   109                                  
   110                                  [BITS 32] ; We need 32-bit intructions for protected mode
   111                                  
   112                                  [ORG 0] 
   113                                  
   114                                  START_CODE:
   115                                  	;; / cat -- concatenate files
   116                                  
   117                                  	;sys	_write, 1, nl, 2
   118                                  	
   119                                  	; 18/06/2022
   120                                  	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[58020000]        <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
   121 00000011 E861000000              	call	rwcount
   122 00000016 20D2                    	and 	dl, dl
   123 00000018 7424                    	jz	short cat_@ ; block device or regular file
   124 0000001A 803D[58020000]10        	cmp	byte [iobuf], 16  ; /dev/lpr
   125 00000021 741B                    	je	short cat_@
   126                                  	; /dev/tty0 .. /dev/tty9
   127                                  	; next line
   128                                  	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[3F020000]        <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
   129 00000039 B9[58020000]            	mov	ecx, iobuf
   130                                  cat_@:
   131 0000003E 5D                      	pop	ebp
   132 0000003F 5A                      	pop	edx
   133                                  	; esi = 0 ; ('sysexec' sets regs to zero)
   134                                  	; 05/03/2022
   135                                  	;mov	esi, fin 
   136                                  	;mov    edi, obuf
   137                                  	;EAX = 2 (written byte count)
   138 00000040 30C0                    	xor	al, al ; 0
   139                                          ; 18/06/2022  
   140                                  	;mov	ecx, iobuf ; 17/06/2022
   141 00000042 4D                      	dec     ebp
   142                                  	;jz	short cat_3	
   143                                  		;;mov	(sp)+,r5
   144                                  		;;tst	(sp)+
   145                                  		;;mov	$obuf,r2
   146                                  		;;cmp	r5,$1
   147                                  		;;beq	3f
   148                                  	
   149                                  	; 18/06/2022
   150 00000043 7505                    	jnz	short cat_0
   151 00000045 E983000000              	jmp	cat_3
   152                                  cat_0:	
   153 0000004A 5B                      	pop	ebx
   154 0000004B 803B2D                  	cmp	byte [ebx], '-'
   155 0000004E 747D                    	je	short cat_3 ; 16/06/2022
   156                                  		;;dec	r5
   157                                  		;;ble	done
   158                                  		;;mov	(sp)+,r0
   159                                  		;;cmpb	(r0),$'-
   160                                  		;;bne	2f
   161                                  		;;clr	fin
   162                                  		;;br	3f
   163                                  cat_1:
   164                                  	;;2:
   165                                  	; ebx = file name offset
   166 00000050 31C9                    	xor 	ecx, ecx ; 0
   167                                  	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 00000052 B805000000          <1>  mov eax, %1
    86 00000057 CD30                <1>  int 30h
   168                                  	;jc	short cat_7
   169                                  	; 05/03/2022 ; (+)
   170 00000059 7305                    	jnc	short cat_2
   171 0000005B E989010000              	jmp	cat_7
   172                                  cat_2:
   173                                  	; 05/03/2022
   174 00000060 89C6                    	mov	esi, eax
   175                                  	;mov	[esi], eax ; 05/03/2022
   176                                  	;mov	[esi], ax
   177                                  		;;mov	r0,0f
   178                                  		;;sys	open; 0:..; 0
   179                                  		;;bes	loop
   180                                  		;;mov	r0,fin
   181                                  
   182                                  	; 05/03/2022 ; (+)
   183                                  	; convert user's file number to inode number
   184                                  	; (get 34 byte inode details, inode num + inode)
   185                                  	; (get 66 byte inode details for runix 386 v1.2)
   186                                  	sys	_fstat, esi, iobuf 
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000062 89F3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000064 B9[58020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000069 B81C000000          <1>  mov eax, %1
    86 0000006E CD30                <1>  int 30h
   187                                  	;jc	short cat_7
   188                                  	; 05/03/2022 ; (+)
   189 00000070 731A                    	jnc	short cat_f
   190 00000072 E969010000              	jmp	cat_k ; 15/06/2022
   191                                  
   192                                  rwcount:
   193                                  	; 15/06/2022
   194 00000077 31D2                    	xor	edx, edx
   195 00000079 A0[5B020000]            	mov	al, [iobuf+stat.mode+1]
   196 0000007E A880                    	test	al, 80h
   197 00000080 7507                    	jnz	short rwc_1
   198 00000082 A840                    	test	al, 40h ; block device ?
   199 00000084 7503                    	jnz	short rwc_1 ; yes
   200                                  	; no, character device
   201                                  	; read/write count = 1
   202 00000086 FEC2                    	inc	dl
   203 00000088 C3                      	retn
   204                                  rwc_1:
   205                                  	; regular file or block device
   206                                  	; read/write count = 512
   207 00000089 B602                    	mov	dh, 2 ; edx = 512
   208 0000008B C3                      	retn
   209                                  	
   210                                  cat_f:
   211                                  	; 15/06/2022
   212 0000008C E8E6FFFFFF              	call	rwcount
   213 00000091 8915[54020000]          	mov	[filerwc], edx
   214                                  
   215                                  	; 18/06/2022
   216                                  	; (check if input file is a tty)
   217 00000097 C605[4C020000]00        	mov	byte [chrin], 0
   218 0000009E 20D2                    	and	dl, dl
   219 000000A0 742B                    	jz	short cat_3  ; not a character device
   220                                  	;mov	ax, [iobuf]
   221 000000A2 A0[58020000]            	mov	al, [iobuf] ; inode number
   222 000000A7 3C1A                    	cmp	al, 26 ; tty9
   223 000000A9 7722                    	ja	short cat_3
   224 000000AB 3C11                    	cmp	al, 17 ; tty0
   225 000000AD 7317                    	jnb	short cat_g
   226                                  
   227                                  	; [chrin] = 0
   228 000000AF 3C08                    	cmp	al, 8  ; /dev/tty
   229 000000B1 751A                    	jne	short cat_3
   230                                  
   231                                  	sys	_gtty, 0, 1  ; get status of console tty (w)
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000000B3 BB00000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 000000B8 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 000000BD B820000000          <1>  mov eax, %1
    86 000000C2 CD30                <1>  int 30h
   232 000000C4 FEC0                    	inc	al  ; console tty number + 1
   233                                  	;jmp	short cat_h
   234                                  cat_g:
   235                                  	;sub 	al, 16
   236 000000C6 240F                    	and	al, 0Fh ; 15
   237                                  ;cat_h:
   238 000000C8 A2[4C020000]            	mov	[chrin], al
   239                                  	; [chrin] = tty number + 1
   240                                  
   241                                  cat_3:	
   242                                  	; 05/03/2022 ; (+)
   243                                  	; get inode number of current tty (stdin)
   244                                  	; (get 34 byte inode details, inode num + inode)
   245                                  	; (get 66 byte inode details for runix 386 v1.2)
   246                                  	;sys	_fstat, 0, iobuf 
   247                                  	;;jc	short cat_n
   248                                  	;mov	ecx, iobuf	
   249                                  	sys	_fstat, 0  ; get stdin file inode details
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000000CD 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 000000D2 B81C000000          <1>  mov eax, %1
    86 000000D7 CD30                <1>  int 30h
   250                                  
   251                                  	; 15/06/2022
   252 000000D9 E899FFFFFF              	call	rwcount
   253 000000DE 8915[50020000]          	mov	[stdinrw], edx
   254                                  
   255                                  cat_n:	;;3:
   256                                  	; get keyboard status of console tty
   257 000000E4 31DB                    	xor	ebx, ebx  ; 0
   258 000000E6 31C9                    	xor	ecx, ecx ; 0
   259                                  	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 000000E8 B820000000          <1>  mov eax, %1
    86 000000ED CD30                <1>  int 30h
   260                                  	; 18/06/2022
   261 000000EF FEC0                    	inc	al ; tty number + 1
   262 000000F1 3A05[4C020000]          	cmp	al, [chrin]
   263 000000F7 7502                    	jne	short cat_b
   264                                  	; input (source) file and stdin (console tty) is same
   265 000000F9 29F6                    	sub	esi, esi ; 0
   266                                  cat_b:	
   267                                  	; 15/06/2022
   268 000000FB 21DB                    	and	ebx, ebx ; is there a waiting char ?
   269 000000FD 7524                    	jnz	short cat_x ; yes
   270                                  
   271 000000FF 08F6                    	or	dh, dh  ; is stdin a character device (tty) ?
   272 00000101 752A                    	jnz	short cat_p
   273                                  			; no, stdin is a file or block device
   274                                  
   275                                  	; is there a file to read ?
   276 00000103 09F6                    	or	esi, esi ; 05/03/2022
   277 00000105 7556                    	jnz	short cat_r ; yes
   278                                  
   279                                  	; edx = [stdinrw]
   280 00000107 B9[58020000]            	mov	ecx, iobuf
   281                                  	; ebx = 0
   282                                  	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 0000010C B803000000          <1>  mov eax, %1
    86 00000111 CD30                <1>  int 30h
   283                                  	;sys	_read, 0, iobuf
   284                                  		; read one char into [iobuf]
   285                                  	;jc	short cat_6 ; (ebx = 0)
   286                                  	; 18/06/2022
   287 00000113 722B                    	jc	short cat_d
   288                                  
   289 00000115 803D[58020000]1B        	cmp	byte [iobuf], 1Bh ; 27 ; ESCape key ?
   290 0000011C 7579                    	jne	short cat_c 
   291 0000011E E9F6000000              	jmp	cat_exit ; yes, exit
   292                                  
   293                                  cat_x:
   294 00000123 80FB1B                  	cmp	bl, 1Bh ; 27 ; ESCape key ?
   295                                  	;je	short cat_exit ; yes, exit
   296                                  	; 18/06/2022
   297 00000126 7505                    	jne	short cat_p
   298 00000128 E9C4000000              	jmp	cat_q
   299                                  cat_p:
   300                                  	; edx = [stdinrw]
   301                                  	sys	_read, 0, iobuf
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 0000012D BB00000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000132 B9[58020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000137 B803000000          <1>  mov eax, %1
    86 0000013C CD30                <1>  int 30h
   302                                  		; read one char into [iobuf]
   303                                  	;jc	short cat_6 ; (ebx = 0)
   304                                  	; 18/06/2022
   305 0000013E 7305                    	jnc	short cat_s
   306                                  cat_d:
   307 00000140 E997000000              	jmp	cat_6
   308                                  cat_s:	
   309                                  	; eax = read count
   310                                  	; read (redirected) stdin at first then other files 
   311 00000145 09C0                    	or	eax, eax
   312                                  	;jnz	short cat_w ; write bytes in buffer
   313                                  	; 18/06/2022
   314 00000147 7406                    	jz	short cat_u
   315 00000149 20F6                    	and	dh, dh
   316 0000014B 744A                    	jz	short cat_c ; console tty (crlf check)
   317 0000014D EB78                    	jmp	short cat_w
   318                                  cat_u:
   319                                  	; 16/06/2022
   320                                  	; trick to skip reading stdin file
   321                                  	;xor	edx, edx
   322                                  	; edx = 0
   323                                  	;mov	[stdinrw], edx  ; end of stdin file
   324                                  	; 18/06/2022
   325 0000014F A3[50020000]            	mov	[stdinrw], eax  ; 0 ; end of stdin file
   326                                  	;jmp	short cat_c ; read (input) file
   327                                  
   328                                  	; is there a file to read ?
   329 00000154 21F6                    	and	esi, esi
   330                                  	;jz	short cat_7 ; no
   331                                  	; 18/06/2022
   332 00000156 7505                    	jnz	short cat_r
   333 00000158 E98C000000              	jmp	cat_7 
   334                                  
   335                                  cat_r:
   336                                  	; 18/06/2022
   337 0000015D 8A2D[4C020000]          	mov	ch, [chrin]
   338 00000163 20ED                    	and	ch, ch
   339 00000165 7446                    	jz	short cat_m ; not a tty file
   340 00000167 30C9                    	xor	cl, cl ; 0
   341 00000169 29DB                    	sub	ebx, ebx ; 0	
   342                                  	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 0000016B B820000000          <1>  mov eax, %1
    86 00000170 CD30                <1>  int 30h
   343 00000172 09DB                    	or	ebx, ebx
   344                                  	;jz	short cat_n ; check for console keystroke
   345                                  	; 18/06/2022
   346 00000174 7505                    	jnz	short cat_e
   347 00000176 E969FFFFFF              	jmp	cat_n
   348                                  
   349                                  cat_e:
   350                                  	sys	_read, esi, iobuf, 1
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 0000017B 89F3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 0000017D B9[58020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 00000182 BA01000000          <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 00000187 B803000000          <1>  mov eax, %1
    86 0000018C CD30                <1>  int 30h
   351 0000018E 803D[58020000]1B        	cmp	byte [iobuf], 1Bh ; 27 ; ESCape key ?
   352 00000195 7449                    	je	short cat_k ; close file
   353                                  
   354                                  cat_c:
   355                                  	; is there a file to read ?
   356                                  	;or	esi, esi ; 05/03/2022
   357                                  	;jnz	short cat_r ; yes
   358                                  
   359                                  	; 16/06/2022
   360                                  	;and	edx, edx
   361                                  	;jz	short cat_exit ; end of stdin file
   362                                  
   363                                  	;xor	eax, eax
   364                                  	;inc	al
   365 00000197 B001                    	mov	al, 1
   366                                  	; eax = 1	
   367 00000199 803D[58020000]0D        	cmp	byte [iobuf], 0Dh ; carriage return ?
   368 000001A0 7525                    	jne	short cat_w
   369 000001A2 C605[59020000]0A        	mov	byte [iobuf+1], 0Ah ; line feed
   370 000001A9 FEC0                    	inc	al
   371                                  	; eax = 2
   372 000001AB EB1A                    	jmp	short cat_w
   373                                  
   374                                  cat_m:
   375                                  	; 05/03/2022
   376                                  	;mov	eax, [esi] ; file descriptor/number
   377                                  	;;mov	ax, [esi]
   378                                  	;
   379                                  	;sys	_read, eax, iobuf, 512 ; 16/07/2015
   380                                  	;;sys 	_read, eax, ibuf, 512 
   381                                  	;jc	short cat_6
   382                                  	; 15/06/2022
   383                                  	; 05/03/2022
   384                                  	; esi = file descriptor/number
   385                                  	sys	_read, esi, iobuf, [filerwc]
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000001AD 89F3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 000001AF B9[58020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 000001B4 8B15[54020000]      <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000001BA B803000000          <1>  mov eax, %1
    86 000001BF CD30                <1>  int 30h
   386                                  		; read 512 chars into [iobuf]
   387                                  	;jc	short cat_6
   388 000001C1 721D                    	jc	short cat_k ; 05/03/2022
   389                                  
   390                                  	; NOTE: If input file is a tty (keyboard)
   391                                  	;	only 1 byte will be read, by ignoring
   392                                  	;	byte count (512).
   393                                  	;	Retro UNIX 8086 v1 kernel ('rtty')
   394                                  	;	has been modified fot that.
   395                                  	;       Erdogan Tan (16/07/2015)
   396                                  	;
   397                                  
   398                                  	; 15/06/2022
   399 000001C3 21C0                    	and	eax, eax ; EAX = 1 for tty (keyboard)
   400                                  	;jz	short cat_6
   401 000001C5 7419                    	jz	short cat_k ; 05/03/2022
   402                                  
   403                                  ;	push	esi
   404                                  	;mov	esi, ibuf
   405                                  ;	mov	esi, ecx ; offset ibuf
   406                                  ;	mov	ecx, eax
   407                                  		;;mov	fin,r0
   408                                  		;;sys	read; ibuf; 512.
   409                                  		;;bes	3f
   410                                  		;;mov	r0,r4
   411                                  		;;beq	3f
   412                                  		;;mov	$ibuf,r3
   413                                  	 ; 16/07/2015
   414                                  ;	mov	edx, eax
   415                                  ;	;add	edx, obuf
   416                                  ;cat_4:
   417                                  ;	;;4:
   418                                  ;	lodsb
   419                                  	;call	putc
   420                                  cat_w:
   421                                  	; 16/07/2015
   422                                  	; write to console tty (stdout)
   423                                  	sys 	_write, 1, iobuf, eax
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000001C7 BB01000000          <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 000001CC B9[58020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81 000001D1 89C2                <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 000001D3 B804000000          <1>  mov eax, %1
    86 000001D8 CD30                <1>  int 30h
   424                                  	;jc	short cat_6
   425                                  	; 05/03/2022
   426                                  	;jnc	short cat_3
   427                                  	; 15/06/2022
   428 000001DA 7347                    	jnc	short cat_8
   429                                  
   430                                  ;	loop	cat_4
   431                                  ;	pop	esi
   432                                  cat_5:
   433                                  	;mov	eax, [esi] ; 05/03/2022
   434                                  	;;mov	ax, [esi]
   435                                  	;jmp	short cat_3
   436                                  		;;movb	(r3)+,r0
   437                                  		;;jsr	pc,putc
   438                                  		;;dec	r4
   439                                  		;;bne	4b
   440                                  		;;br	3b
   441                                  	;; 05/03/2022
   442                                  	;; ebx = file descriptor/input
   443                                  	;;and	ebx, ebx ; console input ?
   444                                  	;;jnz	short cat_3 ; no, file input
   445                                  	;;cmp	byte [quit], al ; 0
   446                                  	;;ja	short cat_7 ; ebx = 0
   447                                  	;;jmp	short cat_z
   448                                  	; 05/03/2022
   449                                  	;jmp	short cat_3
   450                                  		
   451                                  cat_6:	;;3:
   452                                  	; 05/03/2022
   453                                  	; ebx = file descriptor/number
   454                                  	;movzx	ebx, word [esi]
   455                                  	;
   456 000001DC 09F6                    	or	esi, esi
   457 000001DE 7409                    	jz	short cat_7
   458                                  cat_k:
   459                                  	sys	_close, esi
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000001E0 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 000001E2 B806000000          <1>  mov eax, %1
    86 000001E7 CD30                <1>  int 30h
   460                                  	;
   461                                  	;or	ebx, ebx
   462                                  	;jz	short cat_7
   463                                  	;sys 	_close
   464                                  		;;mov	fin,r0
   465                                  		;;beq	loop
   466                                  		;;sys	close
   467                                  		;;br	loop
   468                                  cat_7:	
   469                                  	;;loop:
   470 000001E9 4D                      	dec	ebp
   471                                  	;;jz	short cat_8
   472                                  	; 28/12/2015
   473                                  	;jg	short cat_0
   474                                  	; 05/03/2022
   475 000001EA 7E2D                    	jng	short cat_exit
   476 000001EC E959FEFFFF              	jmp	cat_0
   477                                  
   478                                  	; 18/06/2022 (ESCape)
   479                                  cat_q:
   480                                  	; open console tty for read
   481                                  	sys	_open, consoletty, 0
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 000001F1 BB[42020000]        <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 000001F6 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 000001FB B805000000          <1>  mov eax, %1
    86 00000200 CD30                <1>  int 30h
   482 00000202 7215                    	jc	short cat_exit
   483                                  	sys	_read, eax, iobuf
    73                              <1> 
    74                              <1> 
    75                              <1> 
    76                              <1>  %if %0 >= 2
    77 00000204 89C3                <1>  mov ebx, %2
    78                              <1>  %if %0 >= 3
    79 00000206 B9[58020000]        <1>  mov ecx, %3
    80                              <1>  %if %0 = 4
    81                              <1>  mov edx, %4
    82                              <1>  %endif
    83                              <1>  %endif
    84                              <1>  %endif
    85 0000020B B803000000          <1>  mov eax, %1
    86 00000210 CD30                <1>  int 30h
   484                                  	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 00000212 B806000000          <1>  mov eax, %1
    86 00000217 CD30                <1>  int 30h
   485                                  
   486                                  	;jmp	short cat_exit	
   487                                  
   488                                  cat_exit:
   489                                  	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 00000219 B801000000          <1>  mov eax, %1
    86 0000021E CD30                <1>  int 30h
   490                                  here:
   491 00000220 90                      	nop
   492 00000221 EBFD                    	jmp	short here
   493                                  
   494                                  cat_8:
   495                                  	; 15/06/2022
   496 00000223 39D0                    	cmp	eax, edx
   497 00000225 72B5                    	jb	short cat_6
   498 00000227 8B15[50020000]          	mov	edx, [stdinrw]
   499 0000022D 09D2                    	or 	edx, edx  ; end of stdin file
   500 0000022F 7405                    	jz	short cat_9
   501 00000231 E9AEFEFFFF              	jmp	cat_n
   502                                  
   503                                  cat_9:
   504                                  	; 16/06/2022
   505 00000236 21F6                    	and	esi, esi
   506 00000238 74A6                    	jz	short cat_k
   507 0000023A E91EFFFFFF              	jmp	cat_r ; 18/06/2022
   508                                  
   509                                  ;cat_8:
   510                                  ;	;;done:
   511                                  ;	sub	di, obuf
   512                                  ;	jz	short cat_9
   513                                  ;	sys	_write, 1, obuf, di 
   514                                  		;;sub	$obuf,r2
   515                                  		;;beq	1f
   516                                  		;;mov	r2,0f
   517                                  		;;mov	$1,r0
   518                                  		;;sys	write; obuf; 0:..
   519                                  ;cat_9:	
   520                                  ;	;;1:
   521                                  ;	sys	_exit
   522                                  		;;sys	exit
   523                                  	;;
   524                                  ;putc:	
   525                                  ;	;;putc:
   526                                  ;	stosb
   527                                  ;	cmp	di, dx ; 16/07/2015
   528                                  	;cmp	di, obuf + 512
   529                                  ;	jb	short cat_10
   530                                  ;	push	cx
   531                                  	 ; 16/07/2015
   532                                  ;	mov	di, obuf
   533                                  ;	sub	dx, di ; byte (char) count
   534                                  	; 
   535                                  ;	sys 	_write, 1, obuf
   536                                  	;sys 	_write, 1, obuf, 512
   537                                  	;mov	di, obuf
   538                                  		;;movb	r0,(r2)+
   539                                  		;;cmp	r2,$obuf+512.
   540                                  		;;blo	1f
   541                                  		;;mov	$1,r0
   542                                  		;;sys	write; obuf; 512.
   543                                  		;;mov	$obuf,r2
   544                                  ;	pop	cx
   545                                  ;cat_10:	
   546                                  	;;1:
   547                                  ;	retn
   548                                  		;;rts	pc
   549                                  
   550 0000023F 0D0A00                  nl:	db 0Dh, 0Ah, 0
   551                                  
   552                                  ;; 05/03/2022
   553                                  ;quit:	db 0
   554                                  ; 18/06/2022
   555                                  consoletty:
   556 00000242 2F6465762F74747900      	db '/dev/tty', 0 ; (+)
   557                                  
   558 0000024B 90                      align 4
   559                                  
   560                                  bss_start:
   561                                  
   562                                  ABSOLUTE bss_start
   563                                  
   564                                  ; 18/06/2022
   565 0000024C ??                      chrin:	resb 1
   566 0000024D ??????                  	resb 3
   567                                  
   568                                  ; 15/06/2022
   569 00000250 ????????                stdinrw: resd 1
   570 00000254 ????????                filerwc: resd 1
   571                                  
   572 00000258 <res 200h>              iobuf:  resb 512
   573                                  ;ibuf:	resb 512
   574                                  ;obuf:	resb 512
   575                                  ;;fin:	resw 1
   576                                  ;fin:	resd 1 ; 05/03/2022	
   577                                  		;;.bss
   578                                  		;;ibuf:	.=.+512.
   579                                  		;;obuf:	.=.+512.
   580                                  		;;fin:	.=.+2
   581                                  		;;.text
   582                                  ;bss_end:
