<--- Turn the page     (contents page)     Turn the page --->


Assembler

The Smallest Send To...




In this months column, I have decided to take the C column from Issue #2, December 1998, and modify it a little to make a good optimizing column.

We will take a file from stdin and send it to the Windoze clipboard. This stdin can be from a file through redirection, or from the CON, ie. keyboard with a ^Z as the ending code.

Our point of this column will be to make this program as small as we can, optimize it. Hence, the assembler column.

So let's begin. DOS includes a great service for getting input from the stdin. Service 3Fh of Interrupt 21h. We just include the file handle of the stdin device, a value of 00h in BX. When DOS reach's the end of the file or the ^Z, it returns a value of 00h in AX, meaning, no more read.
Note: We must remember that if we enter the text at the prompt, and give a ^Z to end, this ^Z must be the first char in the line.

Once we have gotten all of the file, we can set up the required parameters to pass to the Windoze clipboard and we are done.
.model tiny
.code

; we need di to point to a buffer passed the end of our code
;  we will have dx point to a byte just before this buffer
           mov  dx,offset Buffer
           mov  di,dx
           inc  di

; we need to read in one (1) byte at a time.
;   on start up, cx = 00FFh
           mov  cl,1

; we need to use handle 00h.
;   on start up, bx = 0000h
           ;xor  bx,bx

Loop1:     mov  ah,3Fh                  ; read from file or device
           int  21h                     ;
           push ax                      ;
           mov  al,[buffer]             ;
           stosb                        ;
           pop  ax                      ;
           dec  ax                      ; if ax = 1 then we got a char
           jz   short Loop1             ;

           mov  ax,1701h                ; open the clipboard
           int  2Fh                     ;

           mov  ax,1702h                ; clear the clipboard
           int  2Fh                     ;

           mov  ax,1703h                ; send the data
           mov  bx,offset Buffer        ; point to the start of our buffer
           inc  bx
           xor  si,si                   ; si:cx = size
           mov  cx,di                   ;
           sub  cx,bx                   ;
           dec  cx
           mov  dx,01                   ; dx = 01 = TEXT
           int  2Fh                     ;

           mov  ax,1708h                ; close the clipboard
           int  2Fh                     ;

           ret                          ; return to DOS

Buffer:

.end
Please note. I am sure that this program can be smaller if a guy/gal would put a little more effort in to it. This is not the point. The point is that sometimes there is no need to put all of the garbage in to your code. However, if you can make this code smaller, please send me a copy of the source and I will post the smallest code I receive in the next issue.

Some simple rules:
- Your code must do exactly as mine. i.e.: the clipboard should have the same contents.
- You must open, clear, and close the clipboard.
- Your code must use the 80x86 instructions set only.
- You can assume the input will have no errors.
- Common sense.

Send your code here.

For an optimizing trick, I tried to inc DX each time I got a char instead of using DI. This saved above 10 bytes. However, service 3Fh (and 40h for that matter) is a little screwing when modifying the registers. ¥


<--- Turn the page     (contents page)     Turn the page --->

Page 9