6 IO redirects for 5 DOS-only files (implementing piping Principles)

Source: Internet
Author: User
Tags exit reset stdin

1. redirect

The following a.asm, with the ' t ' and carriage return 13 txt, imitation command.com, according to the MASM command line arg symbol value N, (such as masm/darg=0 A;), redirect 5 dos open Files (std_):

N=0: Is stdin handle, first bin mode read 1 characters, and then from TXT to "T" to Char
N=1: is stdout handle, output char ' 1 ' to txt
n=2: is stderr handle, Output char ' 2 ' to txt

N=30,31:
3 is Stdaux handle, n=30, from txt to ' t ' to char; n=31, Output char ' 3 ' to txt

N=41:
4 is STDPRN handle, Output char ' 4 ' to txt

Call Function:

When N=0, 44, read/write equipment info, keystroke value into 100h, its ascii/scan to, into 106h, when the expansion code, the first pair, into 104h, secondary value, into the 101h. As 1, get (31,31,2), Ctl_c, Get (3,3,2e), F1, Get ( 0,0,3BH), (3BH,0,3BH)

3d, according to read and write, open txt, save handle to Si
45, save the copy value of the Std_ handle to Di
46, future access (CX) handle, redirect to access (BX) = (SI) handle
3F/40, with std_ input/out, character number 1, bias Char
3e, turn off txt

Debug finish a.com, for input, see 103h char, change ' t ', on output, type TXT, see 1~4.

act=2
Ifdef ARG
IF Arg GT 2
IF Arg EQ 30
Act=30
ENDIF

IF Arg EQ 31
Act=31
ENDIF

IF Arg EQ 41
Act=41
ENDIF

Std_=arg/10
ELSE
IF Arg GE 0
Act=1
Std_=arg

IF Arg EQ 0
Act=0
ENDIF
ENDIF
ENDIF
ENDIF

IF ACT EQ 2 is invalid
%out/darg=0|1|2|30|31|41
. Err
ENDIF

Just macro
Local J

MOV di,es:[26] hit the key, I9 write low ASC, high scan to [1Ch tail text ++],i16 read [1Ah primer + +] to 3f function ax
Sub di,2

CMP di,1ch not reset key area, now turn around
JA j

MOV di,3ch just entered the tail

J:mov Ax,es:[di]
Endm

C segment
Assume Cs:c,ds:c

Org 100h

@: jmp @1

Char db ' 0 ' +std_
TXT db ' txt ', 0

@1:mov AH,3DH Open
mov al,act and 1
Lea Dx,txt
int 21h

mov Si,ax Han

MOV bx,act stdin
Test bx,63 >41
JNZ @4

mov ax,4400h I/O, directed from/to Fil\nul, DX=42H\80C4, otherwise 80d3
int 21h

XOR DH,DH
Push DX

MOV ax,4401h
or dl,32 Bin
int 21h

MOV ax,64 BIOS data area seg,1e~3d is a key zone
MOV Es,ax

MOV AH,3FH unequal return read
MOV cx,1 1 characters
Lea dx,@ into 256
int 21h

CMP byte ptr cs:[256],224 measurement extension
JZ @2

Test byte ptr cs:[256],255 re-test
JNZ @3

@2:just
mov word ptr txt,ax

MOV ah,3fh take True
INC DX
int 21h

@3:just
mov word ptr txt[2],ax

Pop DX

MOV ax,4401h recovery
int 21h

@4:mov ah,45h Duplica
MOV bx,std_
int 21h

mov Di,ax Han

mov ah,46h Force Han
MOV Bx,si
MOV cx,std_
int 21h

MOV ah,3fh+act R/W=3F/40
MOV bx,std_
MOV cx,1
Lea Dx,char
int 21h

MOV ah,46h
MOV Bx,di
MOV cx,std_
int 21h

MOV Ah,3eh close
MOV Bx,si
int 21h

Ret

C Ends
End @

2. Pipe and File reference count

Disassembly DOS version 3.31, get [100,1e9]/[1ea,219] is code/data, initial value is 18/50 1ea/leb, record screen configuration row/column number, initial value 1/1 1ec/1ed, record processed row/column number.

Process:

(2.1) 100, with function 30, take the primary/secondary version number Al/ah, if not 3/1f, on the 1FC "More:incorrect DOS version", and then 110, with int 20, exit

(2.2) 1ea=19, with int 10,ah=0f, the screen number of columns to 1EB, such as 50

(2.3) With function 45, copy stdin (bx=0) handle to BP, such as 5, with function 3e, close stdin handle

(2.4) With function 45, copy stderr (bx=2) handle (also can copy the operation opened Si), must have closed handle 0,
Read the keyboard 1 characters from stderr or Si when reading the full screen, without moving the pipe stdin pointer.

(2.5) 139, with function 3f, open BP handle, byte number cx=1000, Destination 21a, read stdin

When you use the | access pipeline, more does not create a new process.

When not connected, will read the keyboard line, such as read "135", this string and carriage return 0d, line 0a, the saved destination, the deposit length 5 to the AX, the number of characters more than CX, the front CX is saved to the destination. Ax returns 0 for the ^z line.

If the ax=0, use the 14a at the Int 20 exit, otherwise

(2.6) 150, read destination per character to Al

Read to ^z, then turn 14a to exit

Read 0d, set number of columns 1ed=1, turn to 1AC

Read 0a, line number 1EC plus 1, 1ac

Read back 08, when 1ed=1, turn 1ac, otherwise column number minus 1, turn 1ac

Read tab 09, then 1ed to next tab position, turn 1ac

Read the bell 07, it does not occupy the output column, turn 1AC

Read other, 1ed plus 1, compare screen configuration column number 1eb, not greater than 1AC, otherwise

1EC plus 1, reset 1ed=1

(2.7) 1ac,

With function 2, display the current character in stdout to connect the pipe

If 1EC is less than the screen configuration line 1ea, then turn 1e1, otherwise with function 09, show 1f0 "-More-", with the function of 0c, and al=08, clear keyboard input + modulation function 08, no echo, from stderr read keyboard 1 characters

Output 2 empty lines, then reset 1ed=1,1ec=1

(2.8) 1E1,

CX minus 1, the result is 0, turn 139, otherwise turn 150

(2.9) implemented with A.com:

Type A.asm | A.com, at machine, bell per second, or
A.com a.com, Fcb,han each open a.com,1 dup,1 times force_dup, the implementation of the child process, the Count 11231123

C segment
Assume Cs:c;ds:c

Org 100h
@: JMP Init

Cfg_r DB 25
Cfg_c DB 80
Cur_r DB 1
Cur_c DB 1

BUF db "A.com"
DB 1000h-($-BUF) DUP (0)
buf_=$

Key db '-key-$ '
CR DB 13,10,36

OLD4A DD V6

PARABLK DB Parasz
Para db-1
Parasz=$-para
DB 13

Env DW 0
Paraoff DW Parablk
Paraseg DW?
FCB0 DW 92,?
Dd-1

ADJ Macro R,u
Local A
MOV al,r
Inc AL
Daa
CMP Al,u
Stc
Jne A
XOR Al,al
A:mov R,al
Endm

HARP macro
Local H

MOV ah,2
int 26

Adj dh,96 sec BCD
JC H

Adj cl,96
JC H

Adj ch,36

H:mov ah,7 CLR
int 26

MOV ah,6 set
int 26

Endm

Alarm:push Ax
Push CX
Push DX

MOV al,7
int 29h
Harp

Pop DX
Pop CX
Pop ax

JMP cs:old4a

J2S macro H
MOV di,h
MOV Al,cs:24[di] Original jft
Push SI
Push ES
Call ref
Pop es
Pop si
Endm

ref proc
Cbw
GRP:CMP Ax,es:4[si]; CNT
JL SFT

Sub Ax,es:4[si]

Les Si,es:[si]; next_addr
JMP GRP

Sft:mul Word ptr old4a
Add Si,ax

mov al,byte ptr es:6[si]
Add al,48
INT 41
Ret
Ref ENDP

Open proc
MOV ah,82
int 21h
Les Si,es:[bx+di] Vector sft

CMP di,4
JZ Unix

MOV ah,15
MOV dx,92
int 21h
or Al,al
JNZ open1
Open0:ret

Unix:mov ax,3d00h
Lea Dx,buf
int 21h
Jnc Open0
Open1:int 20h
Open ENDP

INIT:CMP byte ptr cs:[129],-1 parameter, ALT + number, cannot be typed
Je lst_lst

MOV ax,4406h
XOR BX,BX
int 21h
or Al,al
JZ Heir
JMP Bell has been redirected

Heir:lea sp,buf_ Wide Stack

mov ah,4ah new mem, beginning ES Festival, long BX Festival
Lea Bx,bell
Add bx,15

Rept 4
SHR bx,1 byte Turn festival
Endm

int 21h

Lst_lst:mov di,26
Call Open

MOV al,cs:[116]
Call ref

MOV bp,sp for off

MOV di,4
Call Open
Push AX

MOV Bx,ax
J2S BX

MOV ah,45h DUP
int 21h
Push AX
J2s AX

MOV al,cs:[24] stdin
mov byte ptr @,al

MOV ah,46h force
XOR cx,cx
int 21h

Push CX
J2S CX

mov ah,16 for exec, off FCB
MOV dx,92
int 21h

CMP byte ptr cs:[129],-1
Je Close

MOV Ax,cs
MOV Es,ax

MOV Paraseg,ax
MOV Fcb0[2],ax

Lea bx,env; es:bx Vector parameter block
Lea Dx,buf DS:DX Vector Executive name

MOV ax,4b00h
int 21h

Close:mov Ah,3eh

CLOSE0:CMP BP,SP
JZ close1
Pop bx
int 21h Free 24[BX]
JMP CLOSE0

Close1:mov al,byte ptr @
MOV Cs:[24],al

int 20h

Bell:mov Ax,354ah
int 21h

mov word ptr old4a,bx
mov word ptr [old4a+2],es

MOV ah,37
Lea Dx,alarm
int 21h

Harp

XOR BX,BX
MOV ah,45h
int 21h

MOV Bp,ax

MOV Ah,3eh
int 21h

MOV bx,2
MOV ah,45h
int 21h

Read:lea Dx,buf
MOV cx,size buf
MOV bx,bp
MOV ah,3fh
int 21h

or Ax,ax
JNZ read1

Quit:lds dx,cs:old4a
MOV Ax,254ah
int 21h

int 20h

Read1:mov Cx,ax
MOV si,dx

Next:lodsb

CMP Al,1ah
JZ quit

CMP al,13
JNZ CTL_CR

MOV cur_c,1
JMP Show

CTL_CR:CMP al,10
Jnz?

Inc Cur_r
JMP Show

? back:cmp al,8
Jnz? tab

CMP cur_c,1
JZ Show

Dec cur_c
JMP Show

? tab:cmp al,9
Jnz Bell?

MOV ah,cur_c
Add ah,7
and ah,248
Inc AH
MOV Cur_c,ah
JMP Show

? bell:cmp al,7
JZ Show

Inc Cur_c

MOV ah,cur_c
CMP Ah,cfg_c
Jbe Show

Inc Cur_r
MOV cur_c,1

Show:mov Dl,al
MOV ah,2
int 21h

MOV Ah,cur_r
CMP Ah,cfg_r
JB ENDBLK

Lea Dx,key
MOV ah,9
int 21h

MOV ax,0c08h
int 21h

Lea DX,CR
MOV ah,9
int 21h

MOV cur_c,1
MOV cur_r,1

Endblk:dec CX
JZ read@
JMP Next

read@: jmp Read

C Ends
End @

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.