     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: 28/12/2015 ]
    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                                  ;
    19                                  ; cat386.s (17/10/2015, Retro UNIX 386 v1, NASM 2.11, 32 bit version)
    20                                  ; CAT2.ASM, 02/12/2013 - 16/07/2015 (Retro UNIX 8086 v1, MASM 6.11) 
    21                                  
    22                                  ; 17/10/2015
    23                                  
    24                                  ; UNIX v1 system calls
    25                                  _rele 	equ 0
    26                                  _exit 	equ 1
    27                                  _fork 	equ 2
    28                                  _read 	equ 3
    29                                  _write	equ 4
    30                                  _open	equ 5
    31                                  _close 	equ 6
    32                                  _wait 	equ 7
    33                                  _creat 	equ 8
    34                                  _link 	equ 9
    35                                  _unlink	equ 10
    36                                  _exec	equ 11
    37                                  _chdir	equ 12
    38                                  _time 	equ 13
    39                                  _mkdir 	equ 14
    40                                  _chmod	equ 15
    41                                  _chown	equ 16
    42                                  _break	equ 17
    43                                  _stat	equ 18
    44                                  _seek	equ 19
    45                                  _tell 	equ 20
    46                                  _mount	equ 21
    47                                  _umount	equ 22
    48                                  _setuid	equ 23
    49                                  _getuid	equ 24
    50                                  _stime	equ 25
    51                                  _quit	equ 26	
    52                                  _intr	equ 27
    53                                  _fstat	equ 28
    54                                  _emt 	equ 29
    55                                  _mdate 	equ 30
    56                                  _stty 	equ 31
    57                                  _gtty	equ 32
    58                                  _ilgins	equ 33
    59                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    60                                  _msg    equ 35 ; Retro UNIX 386 v1 feature only !
    61                                  
    62                                  %macro sys 1-4
    63                                      ; 03/09/2015	
    64                                      ; 13/04/2015
    65                                      ; Retro UNIX 386 v1 system call.		
    66                                      %if %0 >= 2   
    67                                          mov ebx, %2
    68                                          %if %0 >= 3    
    69                                              mov ecx, %3
    70                                              %if %0 = 4
    71                                                 mov edx, %4   
    72                                              %endif
    73                                          %endif
    74                                      %endif
    75                                      mov eax, %1
    76                                      int 30h	   
    77                                  %endmacro
    78                                  
    79                                  ; Retro UNIX 386 v1 system call format:
    80                                  ; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
    81                                  
    82                                  [BITS 32] ; We need 32-bit intructions for protected mode
    83                                  
    84                                  [ORG 0] 
    85                                  
    86                                  START_CODE:
    87                                  	;; / cat -- concatenate files
    88                                  
    89                                  	sys	_write, 1, nl, 2
    90                              <1> 
    91                              <1> 
    92                              <1> 
    93                              <1>  %if %0 >= 2
    94 00000000 BB01000000          <1>  mov ebx, %2
    95                              <1>  %if %0 >= 3
    96 00000005 B9[84000000]        <1>  mov ecx, %3
    97                              <1>  %if %0 = 4
    98 0000000A BA02000000          <1>  mov edx, %4
    99                              <1>  %endif
   100                              <1>  %endif
   101                              <1>  %endif
   102 0000000F B804000000          <1>  mov eax, %1
   103 00000014 CD30                <1>  int 30h
   104                                  
   105 00000016 5D                      	pop	ebp
   106 00000017 5A                      	pop	edx
   107 00000018 BE[88020000]            	mov	esi, fin
   108                                          ;mov    edi, obuf
   109                                  	;EAX = 2 (written byte count)
   110 0000001D 30C0                    	xor	al, al ; 0
   111 0000001F 4D                              dec     ebp
   112 00000020 7414                    	jz	short cat_3
   113                                  		;;mov	(sp)+,r5
   114                                  		;;tst	(sp)+
   115                                  		;;mov	$obuf,r2
   116                                  		;;cmp	r5,$1
   117                                  		;;beq	3f
   118                                  cat_0:	
   119 00000022 5B                      	pop	ebx
   120 00000023 803B2D                  	cmp	byte [ebx], '-'
   121 00000026 740B                    	je	short cat_2
   122                                  		;;dec	r5
   123                                  		;;ble	done
   124                                  		;;mov	(sp)+,r0
   125                                  		;;cmpb	(r0),$'-
   126                                  		;;bne	2f
   127                                  		;;clr	fin
   128                                  		;;br	3f
   129                                  cat_1:
   130                                  	;;2:
   131                                  	; ebx = file name offset
   132 00000028 31C9                    	xor 	ecx, ecx ; 0
   133                                  	sys 	_open
   134                              <1> 
   135                              <1> 
   136                              <1> 
   137                              <1>  %if %0 >= 2
   138                              <1>  mov ebx, %2
   139                              <1>  %if %0 >= 3
   140                              <1>  mov ecx, %3
   141                              <1>  %if %0 = 4
   142                              <1>  mov edx, %4
   143                              <1>  %endif
   144                              <1>  %endif
   145                              <1>  %endif
   146 0000002A B805000000          <1>  mov eax, %1
   147 0000002F CD30                <1>  int 30h
   148 00000031 7244                    	jc	short cat_7
   149                                  cat_2:
   150 00000033 668906                  	mov	[esi], ax
   151                                  		;;mov	r0,0f
   152                                  		;;sys	open; 0:..; 0
   153                                  		;;bes	loop
   154                                  		;;mov	r0,fin
   155                                  cat_3:
   156                                  	;;3:
   157                                          sys     _read, eax, iobuf, 512 ; 16/07/2015
   158                              <1> 
   159                              <1> 
   160                              <1> 
   161                              <1>  %if %0 >= 2
   162 00000036 89C3                <1>  mov ebx, %2
   163                              <1>  %if %0 >= 3
   164 00000038 B9[88000000]        <1>  mov ecx, %3
   165                              <1>  %if %0 = 4
   166 0000003D BA00020000          <1>  mov edx, %4
   167                              <1>  %endif
   168                              <1>  %endif
   169                              <1>  %endif
   170 00000042 B803000000          <1>  mov eax, %1
   171 00000047 CD30                <1>  int 30h
   172                                  	;sys 	_read, eax, ibuf, 512 
   173 00000049 721E                    	jc	short cat_6
   174                                  	; NOTE: If input file is a tty (keyboard)
   175                                  	;	only 1 byte will be read, by ignoring
   176                                  	;	byte count (512).
   177                                  	;	Retro UNIX 8086 v1 kernel ('rtty')
   178                                  	;	has been modified fot that.
   179                                  	;       Erdogan Tan (16/07/2015)
   180                                  	;
   181 0000004B 21C0                    	and	eax, eax ; EAX = 1 for tty (keyboard)
   182 0000004D 741A                    	jz	short cat_6
   183                                  ;	push	esi
   184                                  	;mov	esi, ibuf
   185                                  ;	mov	esi, ecx ; offset ibuf
   186                                  ;	mov	ecx, eax
   187                                  		;;mov	fin,r0
   188                                  		;;sys	read; ibuf; 512.
   189                                  		;;bes	3f
   190                                  		;;mov	r0,r4
   191                                  		;;beq	3f
   192                                  		;;mov	$ibuf,r3
   193                                  	 ; 16/07/2015
   194                                  ;	mov	edx, eax
   195                                  ;	;add	edx, obuf
   196                                  ;cat_4:
   197                                  ;	;;4:
   198                                  ;	lodsb
   199                                  	;call	putc
   200                                  	; 16/07/2015
   201                                  	sys 	_write, 1, iobuf, eax
   202                              <1> 
   203                              <1> 
   204                              <1> 
   205                              <1>  %if %0 >= 2
   206 0000004F BB01000000          <1>  mov ebx, %2
   207                              <1>  %if %0 >= 3
   208 00000054 B9[88000000]        <1>  mov ecx, %3
   209                              <1>  %if %0 = 4
   210 00000059 89C2                <1>  mov edx, %4
   211                              <1>  %endif
   212                              <1>  %endif
   213                              <1>  %endif
   214 0000005B B804000000          <1>  mov eax, %1
   215 00000060 CD30                <1>  int 30h
   216 00000062 7205                    	jc	short cat_6
   217                                  	;
   218                                  ;	loop	cat_4
   219                                  ;	pop	esi
   220                                  cat_5:
   221 00000064 668B06                  	mov	ax, [esi]
   222 00000067 EBCD                    	jmp	short cat_3
   223                                  		;;movb	(r3)+,r0
   224                                  		;;jsr	pc,putc
   225                                  		;;dec	r4
   226                                  		;;bne	4b
   227                                  		;;br	3b
   228                                  cat_6:
   229                                  	;;3:
   230 00000069 0FB71E                  	movzx	ebx, word [esi]
   231 0000006C 09DB                    	or	ebx, ebx
   232 0000006E 7407                    	jz	short cat_7
   233                                  	sys 	_close
   234                              <1> 
   235                              <1> 
   236                              <1> 
   237                              <1>  %if %0 >= 2
   238                              <1>  mov ebx, %2
   239                              <1>  %if %0 >= 3
   240                              <1>  mov ecx, %3
   241                              <1>  %if %0 = 4
   242                              <1>  mov edx, %4
   243                              <1>  %endif
   244                              <1>  %endif
   245                              <1>  %endif
   246 00000070 B806000000          <1>  mov eax, %1
   247 00000075 CD30                <1>  int 30h
   248                                  		;;mov	fin,r0
   249                                  		;;beq	loop
   250                                  		;;sys	close
   251                                  		;;br	loop
   252                                  cat_7:	
   253                                  	;;loop:
   254 00000077 4D                      	dec	ebp
   255                                  	;jz	short cat_8
   256                                  	; 28/12/2015
   257 00000078 7FA8                    	jg	short cat_0
   258                                  	sys	_exit
   259                              <1> 
   260                              <1> 
   261                              <1> 
   262                              <1>  %if %0 >= 2
   263                              <1>  mov ebx, %2
   264                              <1>  %if %0 >= 3
   265                              <1>  mov ecx, %3
   266                              <1>  %if %0 = 4
   267                              <1>  mov edx, %4
   268                              <1>  %endif
   269                              <1>  %endif
   270                              <1>  %endif
   271 0000007A B801000000          <1>  mov eax, %1
   272 0000007F CD30                <1>  int 30h
   273                                  here:
   274 00000081 90                      	nop
   275 00000082 EBFD                    	jmp short here
   276                                  	;;
   277                                  ;cat_8:
   278                                  ;	;;done:
   279                                  ;	sub	di, obuf
   280                                  ;	jz	short cat_9
   281                                  ;	sys	_write, 1, obuf, di 
   282                                  		;;sub	$obuf,r2
   283                                  		;;beq	1f
   284                                  		;;mov	r2,0f
   285                                  		;;mov	$1,r0
   286                                  		;;sys	write; obuf; 0:..
   287                                  ;cat_9:	
   288                                  ;	;;1:
   289                                  ;	sys	_exit
   290                                  		;;sys	exit
   291                                  	;;
   292                                  ;putc:	
   293                                  ;	;;putc:
   294                                  ;	stosb
   295                                  ;	cmp	di, dx ; 16/07/2015
   296                                  	;cmp	di, obuf + 512
   297                                  ;	jb	short cat_10
   298                                  ;	push	cx
   299                                  	 ; 16/07/2015
   300                                  ;	mov	di, obuf
   301                                  ;	sub	dx, di ; byte (char) count
   302                                  	; 
   303                                  ;	sys 	_write, 1, obuf
   304                                  	;sys 	_write, 1, obuf, 512
   305                                  	;mov	di, obuf
   306                                  		;;movb	r0,(r2)+
   307                                  		;;cmp	r2,$obuf+512.
   308                                  		;;blo	1f
   309                                  		;;mov	$1,r0
   310                                  		;;sys	write; obuf; 512.
   311                                  		;;mov	$obuf,r2
   312                                  ;	pop	cx
   313                                  ;cat_10:	
   314                                  	;;1:
   315                                  ;	retn
   316                                  		;;rts	pc
   317                                  
   318 00000084 0D0A00                  nl:	db 0Dh, 0Ah, 0
   319                                  
   320 00000087 90                      align 4
   321                                  
   322                                  bss_start:
   323                                  
   324                                  ABSOLUTE bss_start
   325                                  
   326 00000088 <res 00000200>          iobuf:  resb 512
   327                                  ;ibuf:	resb 512
   328                                  ;obuf:	resb 512
   329 00000288 <res 00000002>          fin:	resw 1
   330                                  		;;.bss
   331                                  		;;ibuf:	.=.+512.
   332                                  		;;obuf:	.=.+512.
   333                                  		;;fin:	.=.+2
   334                                  		;;.text
   335                                  ;bss_end:
