     1                                  ; ****************************************************************************
     2                                  ; chmod8086.s (chmod0.s) - by Erdogan Tan - 29/04/2022
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; Retro UNIX 8086 v1 - chmod - change (file) mode
     5                                  ;
     6                                  ; [ Last Modification: 29/04/2022 ]
     7                                  ;
     8                                  ; Derived from (original) UNIX v2 (v1) 'chmod.s' source Code
     9                                  ; Ref:
    10                                  ; www.tuhs.org (https://minnie.tuhs.org)
    11                                  ; svntree-20081216.tar.gz
    12                                  ; ****************************************************************************
    13                                  ; [ unix72/src/cmd/chmod.s (archive date: 16-12-2008) ]
    14                                  
    15                                  ; Modified from:
    16                                  ; 	chmod1.s - 29/04/2022 - (Retro UNIX 386 v1 & v1.1 % v1.2)
    17                                  
    18                                  ; UNIX v1 system calls
    19                                  _rele 	equ 0
    20                                  _exit 	equ 1
    21                                  _fork 	equ 2
    22                                  _read 	equ 3
    23                                  _write	equ 4
    24                                  _open	equ 5
    25                                  _close 	equ 6
    26                                  _wait 	equ 7
    27                                  _creat 	equ 8
    28                                  _link 	equ 9
    29                                  _unlink	equ 10
    30                                  _exec	equ 11
    31                                  _chdir	equ 12
    32                                  _time 	equ 13
    33                                  _mkdir 	equ 14
    34                                  _chmod	equ 15
    35                                  _chown	equ 16
    36                                  _break	equ 17
    37                                  _stat	equ 18
    38                                  _seek	equ 19
    39                                  _tell 	equ 20
    40                                  _mount	equ 21
    41                                  _umount	equ 22
    42                                  _setuid	equ 23
    43                                  _getuid	equ 24
    44                                  _stime	equ 25
    45                                  _quit	equ 26	
    46                                  _intr	equ 27
    47                                  _fstat	equ 28
    48                                  _emt 	equ 29
    49                                  _mdate 	equ 30
    50                                  _stty 	equ 31
    51                                  _gtty	equ 32
    52                                  _ilgins	equ 33
    53                                  _sleep	equ 34 ; Retro UNIX 8086 v1 feature only !
    54                                  _msg    equ 35 ; Retro UNIX 386 v1 feature only !
    55                                  
    56                                  ;;;
    57                                  ESCKey equ 1Bh
    58                                  EnterKey equ 0Dh
    59                                  
    60                                  ;%macro sys 1-4
    61                                  ;   ; 03/09/2015	
    62                                  ;   ; 13/04/2015
    63                                  ;   ; Retro UNIX 386 v1 system call.		
    64                                  ;   %if %0 >= 2   
    65                                  ;       mov ebx, %2
    66                                  ;       %if %0 >= 3    
    67                                  ;           mov ecx, %3
    68                                  ;           ;%if %0 = 4
    69                                  ;           %if	%0 >= 4 ; 11/03/2022
    70                                  ;		mov edx, %4   
    71                                  ;           %endif
    72                                  ;       %endif
    73                                  ;   %endif
    74                                  ;   mov eax, %1
    75                                  ;   int 30h	   
    76                                  ;%endmacro
    77                                  
    78                                  %macro sys 1-4
    79                                      ; Retro UNIX 8086 v1 system call.
    80                                      %if %0 >= 2   
    81                                          mov bx, %2
    82                                          %if %0 >= 3
    83                                              mov cx, %3
    84                                              %if %0 >= 4
    85                                                 mov dx, %4
    86                                              %endif
    87                                          %endif
    88                                      %endif
    89                                      mov ax, %1
    90                                      int 20h
    91                                  %endmacro
    92                                  
    93                                  ;; Retro UNIX 386 v1 system call format:
    94                                  ;; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
    95                                  
    96                                  ;; 11/03/2022
    97                                  ;; Note: Above 'sys' macro has limitation about register positions;
    98                                  ;;	ebx, ecx, edx registers must not be used after their
    99                                  ;;	positions in sys macro.
   100                                  ;; for example:
   101                                  ;;	'sys _write, 1, msg, ecx' is defective, because
   102                                  ;;	 ecx will be used/assigned before edx in 'sys' macro.
   103                                  ;; correct order may be:
   104                                  ;;	'sys _write, 1, msg, eax ; (eax = byte count)
   105                                  
   106                                  ; Retro UNIX 8086 v1 system call format:
   107                                  ; sys systemcall (ax) <arg1 (bx)>, <arg2 (cx)>, <arg3 (dx)>
   108                                  
   109                                  ; ----------------------------------------------------------------------------
   110                                  
   111                                  [BITS 16] ; We need 16-bit intructions (8086/8088 - Real Mode)
   112                                  
   113                                  [ORG 0] 
   114                                  
   115                                  START_CODE:
   116                                  	; 29/04/2022 - Retro UNIX 8086 v1
   117                                  	;	32 bit to 16 bit conversion
   118                                  	;	-----
   119                                  	;	eax, edx, ecx, ebx -> ax, dx, cx, bx
   120                                  	;	esi, edi, ebp, esp -> si, di, bp, sp
   121                                  	;	register+4 -> register+2
   122                                  	;	dword values on stack -> word values on stack
   123                                  		
   124                                  	; 29/04/2022 - Retro UNIX 386 v1
   125                                  
   126                                  	;mov	sp,r5
   127                                  	;mov	(r5),r4
   128                                  	;cmp	r4,$3
   129                                  	;blt	chmerr
   130                                  	;add	$4,r5
   131                                  	;mov	(r5)+,r1
   132                                  	;clr	0f
   133                                  
   134 00000000 58                      	pop	ax ; ax = number of arguments
   135                                  
   136 00000001 89C2                    	mov	dx, ax ; *
   137                                  	
   138                                  	;cmp	ax, 3
   139 00000003 3C03                    	cmp	al, 3
   140 00000005 7233                    	jb	short chmerr
   141                                  
   142                                  	;xor	cx, cx
   143                                  
   144 00000007 58                      	pop	ax
   145                                  		; ax = argument 0 (file name: 'chmod')
   146 00000008 5E                      	pop	si
   147                                  		; si = argument 1 = mode (octal)
   148 00000009 B304                    	mov	bl, 4 ; '7','7','7',0
   149                                  ;1:
   150                                  chm_1:
   151                                  	;movb	(r1)+,r0
   152                                  	;beq	1f
   153                                  	;asl	0f
   154                                  	;asl	0f
   155                                  	;asl	0f
   156                                  	;bic	$!7,r0
   157                                  	;bis	r0,0f
   158                                  	;br	1b
   159                                  
   160 0000000B AC                      	lodsb
   161 0000000C 08C0                    	or	al, al
   162 0000000E 7415                    	jz	short chm_2
   163                                  	;;; 29/04/2022
   164 00000010 FECB                    	dec	bl
   165 00000012 7426                    	jz	short chmerr
   166                                  	;;; 29/04/2022
   167 00000014 3C37                    	cmp	al, '7'
   168 00000016 7722                    	ja	short chmerr	
   169 00000018 3C30                    	cmp	al, '0'
   170 0000001A 721E                    	jb	short chmerr
   171                                  	;;;
   172                                  	;shl	word [mode], 3 ; * 8
   173 0000001C C1E103                  	shl	cx, 3
   174 0000001F 2407                    	and	al, 7
   175                                  	;or	byte [mode], al
   176 00000021 08C1                    	or	cl, al
   177 00000023 EBE6                    	jmp	short chm_1
   178                                  ;1:
   179                                  chm_2:		
   180                                  	;mov	(r5)+,0f-2
   181                                  	;sys	chmod; ..; 0:..
   182                                  	;bes	chmerr
   183                                  	;dec	r4
   184                                  	;cmp	r4,$3
   185                                  	;bge	1b
   186                                  	;sys	exit
   187                                  
   188 00000025 5E                      	pop	si ; file name address
   189                                  	;sys	_chmod, si, [mode] 
   190                                  	sys	_chmod, si
    79                              <1> 
    80                              <1>  %if %0 >= 2
    81 00000026 89F3                <1>  mov bx, %2
    82                              <1>  %if %0 >= 3
    83                              <1>  mov cx, %3
    84                              <1>  %if %0 >= 4
    85                              <1>  mov dx, %4
    86                              <1>  %endif
    87                              <1>  %endif
    88                              <1>  %endif
    89 00000028 B80F00              <1>  mov ax, %1
    90 0000002B CD20                <1>  int 20h
   191 0000002D 720B                    	jc	short chmerr
   192                                  
   193 0000002F 4A                      	dec	dx ; *
   194                                  	;cmp	dx, 3
   195 00000030 80FA03                  	cmp	dl, 3
   196 00000033 73F0                    	jnb	short chm_2
   197                                  exit:
   198                                  	sys	_exit
    79                              <1> 
    80                              <1>  %if %0 >= 2
    81                              <1>  mov bx, %2
    82                              <1>  %if %0 >= 3
    83                              <1>  mov cx, %3
    84                              <1>  %if %0 >= 4
    85                              <1>  mov dx, %4
    86                              <1>  %endif
    87                              <1>  %endif
    88                              <1>  %endif
    89 00000035 B80100              <1>  mov ax, %1
    90 00000038 CD20                <1>  int 20h
   199                                  
   200                                  chmerr:
   201                                  	;mov	$1,r0
   202                                  	;sys	write; 1f; 2
   203                                  	;sys	exit
   204                                  
   205                                  	sys	_write, 1, _1f, 4  ; ' ?' + CRLF
    79                              <1> 
    80                              <1>  %if %0 >= 2
    81 0000003A BB0100              <1>  mov bx, %2
    82                              <1>  %if %0 >= 3
    83 0000003D B9[4A00]            <1>  mov cx, %3
    84                              <1>  %if %0 >= 4
    85 00000040 BA0400              <1>  mov dx, %4
    86                              <1>  %endif
    87                              <1>  %endif
    88                              <1>  %endif
    89 00000043 B80400              <1>  mov ax, %1
    90 00000046 CD20                <1>  int 20h
   206                                  	;sys	_exit
   207 00000048 EBEB                    	jmp	short exit 
   208                                  
   209                                  ;-----------------------------------------------------------------
   210                                  ;  data - initialized data
   211                                  ;-----------------------------------------------------------------
   212                                  
   213                                  ;align 4
   214                                  
   215                                  ;1:
   216                                  _1f:	; <?\n>
   217                                  
   218 0000004A 203F0D0A00              	db ' ?', 0Dh, 0Ah, 0	
   219                                  
   220                                  ;mode:	dw 0
   221                                  
   222                                  
   223                                  ; 29/04/2022
   224                                  
   225                                  ;-----------------------------------------------------------------
   226                                  ; Original UNIX v2 - /bin/chmod source code (chmod.s)
   227                                  ;		     in PDP-11 (unix) assembly language
   228                                  ;-----------------------------------------------------------------
   229                                  
   230                                  ;/ chmod - change mode
   231                                  ;
   232                                  ;chmode:
   233                                  ;	mov	sp,r5
   234                                  ;	mov	(r5),r4
   235                                  ;	cmp	r4,$3
   236                                  ;	blt	chmerr
   237                                  ;	add	$4,r5
   238                                  ;	mov	(r5)+,r1
   239                                  ;	clr	0f
   240                                  ;1:
   241                                  ;	movb	(r1)+,r0
   242                                  ;	beq	1f
   243                                  ;	asl	0f
   244                                  ;	asl	0f
   245                                  ;	asl	0f
   246                                  ;	bic	$!7,r0
   247                                  ;	bis	r0,0f
   248                                  ;	br	1b
   249                                  ;1:
   250                                  ;	mov	(r5)+,0f-2
   251                                  ;	sys	chmod; ..; 0:..
   252                                  ;	bes	chmerr
   253                                  ;	dec	r4
   254                                  ;	cmp	r4,$3
   255                                  ;	bge	1b
   256                                  ;	sys	exit
   257                                  ;
   258                                  ;chmerr:
   259                                  ;	mov	$1,r0
   260                                  ;	sys	write; 1f; 2
   261                                  ;	sys	exit
   262                                  ;
   263                                  ;1:	<?\n>
