home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Game Programming Gurus / Tricks_of_the_Game_Programming_Gurus_SAMS_Publishing_1994.iso / digipak / theaudio / real / doscalls.asm < prev    next >
Assembly Source File  |  1994-01-03  |  25KB  |  978 lines

  1.  
  2.     LOCALS            ;; Enable local labels
  3.  
  4.         IDEAL                   ;; Use Turbo Assembler's IDEAL mode
  5.     JUMPS
  6.  
  7. SMALL_MODEL    equ    0       ;: true only if trying to generate near
  8.                     ;; procedure calls, note that all C function
  9.                     ;; prototypes are currently set as far so
  10.                     ;; that the C compiler will link correctly
  11.                     ;; regardless of memory model, so there is
  12.                     ;; no need to generate near calls unless you
  13.                     ;; are trying to write an executable without
  14.                     ;; any segment relocatable information such as
  15.                     ;; a COM file.
  16.  
  17.  
  18.     INCLUDE "PROLOGUE.MAC"  ;; common assembly language prologue
  19.  
  20. SEGMENT  _TEXT BYTE PUBLIC 'CODE'    ;; Set up _TEXT segment
  21.         ENDS
  22.  
  23.     ASSUME    CS: _TEXT, DS: _TEXT, SS: NOTHING, ES: NOTHING
  24.  
  25.     extrn    _memfree:far    ; Application controlled memory allocation.
  26.     extrn    _memalloc:far    ; Application controlled memory allocation.
  27.  
  28.  
  29. SEGMENT _TEXT
  30.  
  31. Macro    CPROC    name
  32.     public    _&name
  33. IF    SMALL_MODEL
  34. Proc    _&name    near
  35. ELSE
  36. Proc    _&name    far
  37. ENDIF
  38.     endm
  39.  
  40.  
  41.  
  42.     CPROC    mfopen
  43.     ARG    FNAME:DWORD,RSIZE:DWORD,FTYPE:WORD
  44. ; usage: int mfopen(char *filename,    Name of the file to open. +4
  45. ;                   long int *size,    Returned size of file     +6
  46. ;                    int type);        Open type.                +8
  47. ; type --- 0 - open, create new.
  48. ; type --- 1 - open, without creating.
  49. ; returns file handle.  A file handle of zero is an error condition.
  50.         push    bp
  51.         mov     bp,sp
  52.     push    ds
  53.         push    si
  54.         push    di
  55.  
  56.     lds    dx,[FNAME]
  57.     mov    ax,[FTYPE]    ; get open type requested.
  58.         or      al,al
  59.         jnz     opold
  60. ; Here we create the file as requested.
  61.         xor     cx,cx
  62.         mov     ah,03Ch         ; create handle function.
  63.         int     21h             ; perform function
  64.         jc      ferr            ; if carry clear set then return error.
  65.         jnc     fop             ; ALWAYS-> branch to handle return.
  66. opold:  mov     al,2            ; open with read and write access.
  67.         mov     ah,03Dh         ; open handle for read and write access.
  68.         int     21h             ; do file open.
  69.         jc      ferr            ; file not found, return error.
  70. fop:    mov     bx,ax           ; put file handle in BX.
  71.         mov     ah,42h          ; function 42h move file pointer
  72.         mov     al,2            ; offset from end of file
  73.         xor     cx,cx           ; zero bytes from
  74.         xor     dx,dx           ; end of the file
  75.         int     21h             ; perform move file pointer
  76.         jc      ferr            ; file error with a close
  77.     lds    di,[RSIZE]    ; pointer to long int
  78.     or    di,di        ; Valid pointer?
  79.     jnz    @@OK1
  80.     mov    cx,ds        ; Get DS register.
  81.     jcxz    @@SKIP        ; if null pointer, skip
  82. @@OK1:    mov    [di],ax     ; save low word of file size
  83.         mov     [di+2],dx       ; save high word of file size
  84. @@SKIP: mov    ah,42h        ; function 42h move file pointer
  85.     xor    al,al        ; offset from beginning.
  86.         xor     cx,cx           ; zero bytes from beginning
  87.         xor     dx,dx           ; of the file
  88.         int     21h             ; perform move file pointer
  89.         jc      ferr            ; file error with a close
  90.         mov     ax,bx           ; return file handle.
  91.         pop     di
  92.         pop     si
  93.     pop    ds
  94.         pop     bp              ; restore base pointer.
  95.         ret
  96. ferr:   xor     ax,ax           ; handle of zero is an error condition.
  97.         pop     di
  98.         pop     si
  99.     pop    ds
  100.         pop     bp
  101.         ret
  102.     endp
  103.  
  104.     CPROC    mfclose
  105.     ARG    FHAND:WORD
  106. ; usage: int mfclose(int fhand)  Closes this files.
  107.          push   bp
  108.          mov    bp,sp
  109.          mov    ah,3Eh          ; function for close handle.
  110.      mov    bx,[FHAND]    ; get handle into BX
  111.          int    21h             ; perform file close.
  112.          jc     clerr           ; if carry set then a close error.
  113.          mov    ax,1            ; return code of one is success code.
  114.          pop    bp
  115.          ret
  116. clerr:  xor    ax,ax            ; return code of zero is an error on close.
  117.         pop     bp
  118.         ret
  119.     endp
  120.  
  121.     CPROC    mfpos
  122.     ARG    FHAND:WORD
  123. ; Usage: long int mfpos(int fhand)
  124. ; Reports current file position.
  125.         push    bp
  126.         mov     bp,sp
  127.  
  128.     mov    bx,[FHAND]    ; get file handle.
  129.         mov     ah,42h          ; move file pointer.
  130.         mov     al,1            ; from current pointer position.
  131.         xor     cx,cx           ; offset of zero from current location.
  132.         xor     dx,dx           ; high word.
  133.         int     21h             ; do dos
  134.  
  135.         pop     bp
  136.         ret
  137.     endp
  138.  
  139.     CPROC    mfseek
  140.     ARG    FHAND:WORD,FPOSL:WORD,FPOSH:WORD
  141. ; usage: mfseek(int fhand,long int fpos)
  142. ; sets current file position, returns, in the same location, the actual
  143. ; file position.
  144.         push    bp
  145.         mov     bp,sp
  146.  
  147.     mov    bx,[FHAND]    ; get file handle.
  148.         mov     ah,42h          ; move file pointer.
  149.         mov     al,0            ; from beginning of the file.
  150.  
  151.     mov    dx,[FPOSL]    ; get low word of file pos.
  152.     mov    cx,[FPOSH]    ; get high word of file pos.
  153.         int     21h             ; do dos
  154.         jc      mfserr          ; move file position error.
  155.  
  156.         pop     bp
  157.         ret
  158. mfserr:
  159.     xor    ax,ax        ; zero out return code.
  160.     xor    dx,dx
  161.  
  162.         pop     bp
  163.         ret
  164.     endp
  165.  
  166.     CPROC    mfread
  167.     ARG    READADR:DWORD,SIZEL:WORD,SIZEH:WORD,FHAND:WORD
  168. ; usage: int mfread(char far *read,long int size,int fhand);
  169.         push    bp
  170.         mov     bp,sp
  171.  
  172.         push    si
  173.         push    di
  174.     mov    bx,[FHAND]     ; get file handle.
  175.         push    ds               ; save data segment
  176.  
  177.     lds    dx,[READADR]
  178.  
  179. doit2:  mov    cx,32768          ; block size for reads.
  180.     cmp    [SIZEH],0     ; is high word non-zero?
  181.         jne    doread            ; yes->do read
  182.     cmp    cx,[SIZEL]     ; more than block bytes to read?
  183.         jbe    doread            ; go do read
  184.     mov    cx,[SIZEL]     ; read last portion
  185. doread: mov   ah,3Fh             ; read command
  186.         int    21h               ; do read
  187.         jc     ferr2             ; file error
  188.         cmp    cx,ax             ; read all the bytes in?
  189.         jne    ferr2             ; file error
  190.     sub    [SIZEL],cx     ; subtract
  191.     sbb    [SIZEH],0     ; high word
  192.         mov    ax,cx             ; save count bytes written
  193.         and    ax,0Fh            ; leave low nibble
  194.         add    dx,ax             ; add to offset
  195.         shr    cx,1              ; /2
  196.         shr    cx,1              ; /4
  197.         shr    cx,1              ; /8
  198.         shr    cx,1              ; /16
  199.         mov    ax,ds             ; get segment
  200.         add    ax,cx             ; add paragraphs along
  201.         mov    ds,ax             ; place in data segment
  202.     cmp    [SIZEL],0     ; done all?
  203.         jne    doit2             ; non-zero more to do
  204.     cmp    [SIZEH],0     ; done all?
  205.     jne    doit2         ; non-zero more to do
  206.     mov    ax,1         ; return success code.
  207.  
  208.         pop     ds               ; get back data segment
  209.         pop     di
  210.         pop     si
  211.         pop     bp
  212.         ret
  213. ferr2:  xor     ax,ax            ; return error code on read
  214.         pop     ds
  215.         pop     di
  216.         pop     si
  217.         pop     bp
  218.         ret
  219.     endp
  220.  
  221.     CPROC    mfwrite
  222.     ARG    WADR:DWORD,SIZEL:WORD,SIZEH:WORD,FHAND:WORD
  223. ; usage: int mfwrite(int seg,int offset,long int size,int fhand)
  224.         push    bp
  225.         mov     bp,sp
  226.  
  227.         push    si
  228.         push    di
  229.     mov    bx,[FHAND]    ; get file handle.
  230.         push    ds              ; save data segment
  231.  
  232.     lds    dx,[WADR]    ; write address.
  233.  
  234. doit:   mov    cx,32768         ; block size for writes
  235.     cmp    [SIZEH],0    ; is high word non-zero?
  236.         jne    dowrite          ; yes->do write
  237.     cmp    cx,[SIZEL]    ; more than block bytes to write?
  238.         jbe    dowrite          ; go do write
  239.     mov    cx,[SIZEL]    ; write last portion
  240. dowrite: mov   ah,40h           ; write command
  241.         int    21h              ; do write
  242.         jc     ferr1            ; file error
  243.         cmp    cx,ax            ; write all the bytes out?
  244.         jne    ferr1            ; file error
  245.     sub    [SIZEL],cx    ; subtract
  246.     sbb    [SIZEH],0    ; high word
  247.         mov    ax,cx            ; save count bytes written
  248.         and    ax,0Fh           ; leave low nibble
  249.         add    dx,ax            ; add to offset
  250.         shr    cx,1             ; /2
  251.         shr    cx,1             ; /4
  252.         shr    cx,1             ; /8
  253.         shr    cx,1             ; /16
  254.         mov    ax,ds            ; get segment
  255.         add    ax,cx            ; add paragraphs along
  256.         mov    ds,ax            ; place in data segment
  257.     cmp    [SIZEL],0    ; done all?
  258.         jne    doit             ; non-zero more to do
  259.     cmp    [SIZEH],0    ; done all?
  260.         jne    doit             ; non-zero more to do
  261.         mov     ax,1            ; return success code.
  262.         pop     ds              ; get back data segment
  263.         pop     di
  264.         pop     si
  265.         pop     bp
  266.         ret
  267. ferr1:  xor     ax,ax           ; return error code on write.
  268.         pop     ds
  269.         pop     di
  270.         pop     si
  271.         pop     bp
  272.         ret
  273.     endp
  274.  
  275.  
  276.     CPROC    fload
  277.     ARG    FNAME:DWORD,RSIZE:DWORD
  278. ; This routine is called as:
  279. ; char far *fload(char *filename,long int *size)
  280. ; where filename is the name of the file you wish to load
  281. ; and size will contain the size of the file in bytes.
  282. ; This routine returns the segment in memory of where this file
  283. ; was read into.  It uses a dos memory allocate to decide where to
  284. ; read the file in.
  285.         push    bp
  286.         mov     bp,sp
  287.         push    di
  288.         push    si
  289.     push    ds
  290.     mov    si,ds         ; Save DS passed.
  291.  
  292.     lds    dx,[FNAME]
  293.         xor     al,al            ; access code of zero
  294.         mov     ah,03Dh          ; function 3D file open
  295.         int     21h              ; perform function
  296.     jc    @@ferr0      ; if carry set then an error
  297.         mov     bx,ax            ; place file handle in bx
  298.         mov     ah,42h           ; function 42h move file pointer
  299.         mov     al,2             ; offset from end of file
  300.         xor     cx,cx            ; zero bytes from
  301.         xor     dx,dx            ; end of the file
  302.         int     21h              ; perform move file pointer
  303.     jc    @@ferr1      ; file error with a close
  304.     lds    di,[RSIZE]
  305.     or    di,di
  306.     jz    @@SKIP
  307.     mov    [di],ax      ; save low word of file size
  308.     mov    [di+2],dx     ; save high word of file size
  309. @@SKIP: mov    ds,si         ; DS=segment on entry.
  310.     mov    si,bx         ; Save file handle
  311.     push    dx
  312.     push    ax
  313.     call    _memalloc     ; Allocate memory for it.
  314.     add    sp,4
  315.     mov    bx,si         ; get back file handle
  316.     or    ax,ax         ; Offset portion needs to be zero!
  317.     jnz    @@ok2         ; Exit if offset portion is non-zero.
  318.     or    dx,dx         ; Failure to allocate memory?
  319.     jz    @@ferr1      ; file error with close
  320. @@ok2:    mov    di,ax         ; Save offset portion of address.
  321.     mov    si,dx         ; place segment in si
  322.         mov     ah,42h           ; move file pointer
  323.         xor     al,al            ; to beginning of file
  324.         xor     cx,cx            ; zero begin
  325.         xor     dx,dx            ; zero begin
  326.         int     21h              ; move file pointer to begining
  327.         mov     ds,si            ; get segment of memory allocate
  328.         mov     cx,32768         ; read in 32768 bytes
  329.     mov    dx,di         ; byte offset zero from allocated segment
  330. readin: mov     ah,3Fh           ; file read handle
  331.         int     21h              ; perform the read
  332.         cmp     ax,cx            ; read in whole amount?
  333.         jne     readon           ; done entire read
  334.         mov     ax,ds            ; get buffer segment
  335.         add     ax,2048          ; add 32768 bytes
  336.         mov     ds,ax            ; place back in data segment
  337.         jmp short readin         ; read in next block
  338. readon: mov    ah,3eh         ; close handle
  339.         int     21h              ; perform close
  340.     mov    dx,si         ; get back segment
  341.     mov    ax,di         ; offset portion
  342.     pop    ds
  343.         pop     si
  344.         pop     di
  345.         pop     bp               ; restore base pointer
  346.         ret
  347. @@ferr1:  mov      ah,3eh     ; close handle
  348.         int     21h              ; perform close
  349. @@ferr0:  xor      ax,ax
  350.     xor    dx,dx        
  351.     pop    ds
  352.         pop     si
  353.         pop     di
  354.         pop     bp
  355.         ret
  356.     endp
  357.  
  358.  
  359. ;; Load a file into allocated memory, but force it to be on a paragraph
  360. ;; boundary.
  361.     CPROC    floadpara
  362.     ARG    FNAME:DWORD,RSIZE:DWORD,SEGRET:DWORD
  363. ; This routine is called as:
  364. ; char far *fload(char *filename,long int *size)
  365. ; where filename is the name of the file you wish to load
  366. ; and size will contain the size of the file in bytes.
  367. ; This routine returns the segment in memory of where this file
  368. ; was read into.  It uses a dos memory allocate to decide where to
  369. ; read the file in.
  370.         push    bp
  371.         mov     bp,sp
  372.         push    di
  373.         push    si
  374.     push    ds
  375.     mov    si,ds         ; Save DS passed.
  376.  
  377.     lds    dx,[FNAME]
  378.         xor     al,al            ; access code of zero
  379.         mov     ah,03Dh          ; function 3D file open
  380.         int     21h              ; perform function
  381.     jc    @@ferr0        ; if carry set then an error
  382.         mov     bx,ax            ; place file handle in bx
  383.         mov     ah,42h           ; function 42h move file pointer
  384.         mov     al,2             ; offset from end of file
  385.         xor     cx,cx            ; zero bytes from
  386.         xor     dx,dx            ; end of the file
  387.         int     21h              ; perform move file pointer
  388.     jc    @@ferr1        ; file error with a close
  389.     lds    di,[RSIZE]
  390.     or    di,di
  391.     jz    @@SKIP
  392.     mov    [di],ax     ; save low word of file size
  393.     mov    [di+2],dx    ; save high word of file size
  394. @@SKIP: mov    ds,si        ; DS=segment on entry.
  395.     mov    si,bx        ; Save file handle
  396.     add    ax,16
  397.     adc    dx,0        ; Extra 16 bytes.
  398.     push    dx
  399.     push    ax
  400.     call    _memalloc    ; Allocate memory for it.
  401.     add    sp,4
  402.     mov    bx,si        ; get back file handle
  403.     or    ax,ax        ; Offset portion needs to be zero!
  404.     jnz    @@ok2        ; Exit if offset portion is non-zero.
  405.     or    dx,dx        ; Failure to allocate memory?
  406.     jz    @@ferr1     ; file error with close
  407. @@ok2:    mov    di,ax        ; Save offset portion of address.
  408.     mov    si,dx         ; place segment in si
  409.         mov     ah,42h           ; move file pointer
  410.         xor     al,al            ; to beginning of file
  411.         xor     cx,cx            ; zero begin
  412.         xor     dx,dx            ; zero begin
  413.         int     21h              ; move file pointer to begining
  414.         mov     ds,si            ; get segment of memory allocate
  415.         mov     cx,32768         ; read in 32768 bytes
  416.     mov    dx,di         ; byte offset zero from allocated segment
  417. ;; Get byte offset, but now round it up to the next paragraph boundary.
  418.     add    dx,15        ; Round up.
  419.     and    dx,0FFF0h    ; Strip off bottom 4 bits.
  420. @@r1:    mov    ah,3Fh         ; file read handle
  421.         int     21h              ; perform the read
  422.         cmp     ax,cx            ; read in whole amount?
  423.     jne    @@r2         ; done entire read
  424.         mov     ax,ds            ; get buffer segment
  425.         add     ax,2048          ; add 32768 bytes
  426.         mov     ds,ax            ; place back in data segment
  427.     jmp short @@r1         ; read in next block
  428. @@r2:    mov    ah,3eh         ; close handle
  429.         int     21h              ; perform close
  430.     mov    dx,si         ; get back segment
  431.     mov    ax,di         ; offset portion
  432.     add    ax,15        ; Round up to closest paragraph.
  433.     ShiftR    ax,4        ; compute segment portion.
  434.     add    ax,dx        ; Plus segment portion of far address.
  435.     lds    bx,[SEGRET]    ; Get address to return segment portion of address.
  436.     mov    [ds:bx],ax    ; Return segment load.
  437.     mov    dx,si         ; get back segment
  438.     mov    ax,di         ; offset portion
  439.  
  440.     pop    ds
  441.         pop     si
  442.         pop     di
  443.         pop     bp               ; restore base pointer
  444.         ret
  445. @@ferr1:  mov      ah,3eh       ; close handle
  446.         int     21h              ; perform close
  447. @@ferr0:  xor      ax,ax
  448.     xor    dx,dx        ;
  449.     pop    ds
  450.         pop     si
  451.         pop     di
  452.         pop     bp
  453.         ret
  454.     endp
  455.  
  456.  
  457.  
  458.     CPROC    keystat
  459.         mov     ah,01h
  460.         int     16h
  461.         jnz     IsKey
  462.         xor     ax,ax
  463.         ret
  464. IsKey:  mov     ax,1
  465.         ret
  466.     endp
  467.  
  468.  
  469.     CPROC    getkey
  470.         mov     ah,07h
  471.         int     21h
  472.         xor     ah,ah
  473.     or    al,al
  474.     jnz    @@RET
  475.     mov    ah,07h
  476.     int    21h
  477.     xor    ah,ah
  478.     add    ax,256
  479. @@RET:
  480.         ret
  481.     endp
  482.  
  483.  
  484.     CPROC    farcop
  485.     ARG    DEST:DWORD,FROM:DWORD
  486.     push    bp
  487.     mov    bp,sp
  488.     push    es
  489.     push    ds
  490.     push    si
  491.     push    di
  492.  
  493.     les    di,[DEST]
  494.     lds    si,[FROM]
  495. @@MOV:    lodsb
  496.     stosb
  497.     or    al,al
  498.     jnz    @@MOV
  499.  
  500.     pop    di
  501.     pop    si
  502.     pop    ds
  503.     pop    es
  504.     pop    bp
  505.     ret
  506.     endp
  507.  
  508.  
  509.     CPROC    farcat
  510.     ARG    DEST:DWORD,FROM:DWORD
  511.     push    bp
  512.     mov    bp,sp
  513.     push    es
  514.     push    ds
  515.     push    si
  516.     push    di
  517.  
  518.     les    di,[DEST]
  519.     xor    ax,ax
  520.     mov    cx,-1
  521.     repnz    scasb
  522.     dec    di
  523.     lds    si,[FROM]
  524. @@MOV2:  lodsb
  525.     stosb
  526.     or    al,al
  527.     jnz    @@MOV2
  528.  
  529.     pop    di
  530.     pop    si
  531.     pop    ds
  532.     pop    es
  533.     pop    bp
  534.     ret
  535.     endp
  536.  
  537.  
  538.     CPROC    fmalloc
  539.     ARG    SIZEL:WORD,SIZEH:WORD
  540. ; MEMALLOC(SIZE IN PARAGRAPHS)
  541. ; This routine allocates the amount of memory requested in the first
  542. ; argument.  If the memory could not be allocated then MEMALLOC returns
  543. ; a -1 else it returns the segment of the allocated memory
  544.         push    bp
  545.         mov     bp,sp
  546.  
  547.     mov    ax,[SIZEL]    ; Get low word.
  548.     mov    dx,[SIZEH]    ; Get high word, get size, long word.
  549.  
  550.     sar    dx,1
  551.     rcr    ax,1        ; /2
  552.  
  553.     sar    dx,1
  554.     rcr    ax,1        ; /4
  555.  
  556.     sar    dx,1
  557.     rcr    ax,1        ; /8
  558.  
  559.     sar    dx,1
  560.     rcr    ax,1        ; /16
  561.  
  562.     or    dx,dx
  563.     jnz    @@ERR
  564.  
  565.     mov    bx,ax        ; Paragraphs.
  566.     inc    bx        ; Plus 1 to correct for roundoff
  567.         mov     ah,48h          ; request for memory allocate
  568.         int     21h
  569.     jnc    @@mall1       ; memory was allocated
  570. @@ERR:    xor    ax,ax        ; error return code!
  571. @@mall1:  mov      dx,ax
  572.     xor    ax,ax        ; Far ptr format
  573.     pop    bp
  574.         ret
  575.     endp
  576.  
  577.  
  578.  
  579.     CPROC    ffree
  580.     ARG    FREEL:WORD,FREEH:WORD
  581. ; FREEMEM(Segement to free)
  582. ; This routine frees previously allocated memory block
  583. ; -1 is an error code
  584.         push    bp
  585.         mov     bp,sp
  586.         push    es
  587.     mov    bx,[FREEH]    ; Get segment portion of address
  588.         mov     es,bx           ; segment to free
  589.         mov     ah,49h
  590.         int     21h
  591.         jnc     fmem1
  592.         mov     ax,-1
  593. fmem1:  pop     es
  594.         pop     bp
  595.         ret
  596.     ENDP
  597.  
  598.  
  599.     CPROC    writeln
  600.     ARG    STRING:DWORD
  601. ; called as writeln(char *string)
  602. ; prints the string specifed to the standard output device
  603.         push    bp         ; save base pointer
  604.         mov     bp,sp      ; place bp in sp
  605.     push    ds
  606.         push    si
  607.     lds    si,[STRING]
  608.         mov     bl,15      ; print in White
  609. write:  lodsb              ; get character
  610.     or    al,al       ; EOS?
  611.     jz    writdon    ; done with write
  612.         cmp     al,10      ; is it a line feed?
  613.         jne     nex1       ; go compare next item
  614.         mov     ah,0Eh     ; write out the line feed
  615.         int     10h        ; now write out
  616.         mov     al,13      ; a carriage return
  617. nex1:   mov     ah,0Eh     ; write a character function
  618.         int     10h        ; write the character
  619.         jmp short write    ; keep on writing
  620. writdon: pop    si
  621.     pop    ds
  622.         pop    bp         ; restore base pointer
  623.         ret
  624.     endp
  625.  
  626.     CPROC    GetTimerInterruptVector
  627.     push    ds
  628.     xor    ax,ax
  629.     mov    ds,ax
  630.     mov    ax,[ds:8*4]
  631.     mov    dx,[ds:8*4+2]
  632.     pop    ds
  633.     ret
  634.     endp
  635.  
  636. command_reg     equ 43h
  637. channel_0       equ 40h
  638. channel_2       equ 42h         ; speaker's frequency oscillator.
  639.  
  640.     CPROC    SetTimerInterruptVector
  641.     ARG    ADDRESSL:WORD,ADDRESSH:WORD,DIVISOR:WORD
  642.     PENTER    0
  643.  
  644.     cli        ; Turn off hardware Interrupts while modifiying vectory.
  645.     push    ds
  646.     xor    ax,ax
  647.     mov    ds,ax
  648.     mov    ax,[ADDRESSL]
  649.     mov    [ds:8*4],ax
  650.     mov    ax,[ADDRESSH]
  651.     mov    [ds:8*4+2],ax
  652.     mov    dx,[DIVISOR]    ; Get divisor rate.
  653.  
  654.         mov     al,00110110b
  655.         out     command_reg,al
  656.         jmp     $+2
  657.     mov    ax,dx        ; Get rate into AX
  658.         out     channel_0,al
  659.         jmp     $+2
  660.         mov     al,ah
  661.         out     channel_0,al
  662.     sti            ; Restart hardware Interrupts
  663.  
  664.     pop    ds
  665.     PLEAVE
  666.     ret
  667.     endp
  668.  
  669.  
  670. CPROC    mdelete
  671.     ARG    FNAME:DWORD
  672. ; usage: int mdelete(char far *fname)
  673. ; return code 1, deleted.
  674. ;          0, error occured.
  675.     PENTER    0
  676.     push    ds
  677.  
  678.     lds    dx,[FNAME]
  679.     mov    ah,41h
  680.     int    21h
  681.     jnc    mdok
  682.     xor    ax,ax
  683.     jmp short @@EXT
  684. mdok:    mov    ax,1
  685. @@EXT:
  686.     pop    ds
  687.     PLEAVE
  688.     ret
  689.     endp
  690.  
  691.  
  692.     CPROC    farlen
  693.     ARG    STRING:DWORD
  694.     PENTER    0
  695.     push    es
  696.     push    di
  697.  
  698.     les    di,[STRING]
  699.     xor    ax,ax
  700.     mov    cx,-1
  701.     repnz    scasb
  702.     not    cx
  703.     mov    ax,cx
  704.     dec    ax        ; Less one
  705.  
  706.     pop    di
  707.     pop    es
  708.     PLEAVE
  709.     ret
  710.     endp
  711.  
  712.     CPROC    ucase
  713.     ARG    STRING:DWORD
  714.     PENTER    0
  715.     PushCREGS
  716.  
  717.     xor    ax,ax
  718.     lds    si,[STRING]
  719.     push    ds
  720.     pop    es
  721.     mov    di,si            ; Dest address is the same.
  722.     mov    cx,-1
  723.     xor    ax,ax
  724.     repnz    scasb
  725.     not    cx
  726.     mov    di,si
  727. @@LOD:  lodsb                           ; Get byte from source.
  728.         cmp     al,'a'                  ;
  729.         jl      @@STO                   ; char < 'A' ... continue to next char
  730.         cmp     al,'z'                  ;
  731.         jg      @@STO                   ; char > 'Z' ... continue to next char
  732.         sub     al,'a'-'A'              ; upper case it
  733. @@STO:  stosb                           ; Store it
  734.     loop    @@LOD
  735.  
  736.     PopCREGS
  737.     PLEAVE
  738.     ret
  739.     endp
  740.  
  741.     CPROC    farcompare
  742.     ARG    STR1:DWORD,STR2:DWORD
  743.     PENTER    0
  744.     PushCREGS
  745.  
  746.  
  747.     lds    si,[STR1]
  748.     les    di,[STR2]
  749.     push    di
  750.     mov    cx,-1
  751.     xor    ax,ax        ; zero byte
  752.     repnz    scasb        ; Search for EOS string 2
  753.     not    cx        ; Get length.
  754.     pop    di
  755.     rep    cmpsb        ; compare strings.
  756.     xor    bx,bx        ; Zero bx.
  757.     mov    al,[ds:si-1]    ; Get ending byte.
  758.     mov    bl,[es:di-1]    ; Ending byte.
  759.     sub    ax,bx        ; Return code.
  760.  
  761.     PopCREGS
  762.     PLEAVE
  763.     ret
  764.     endp
  765.  
  766.     CPROC    ifexists
  767.     ARG    FNAME:DWORD
  768.     PENTER    0
  769.     push    ds
  770.  
  771.     lds    dx,[FNAME]
  772.     xor    al,al
  773.     mov    ah,03Dh
  774.     int    21h
  775.     jc    @@NOP
  776.     mov    bx,ax
  777.     mov    ah,3Eh
  778.     int    21h
  779.     mov    ax,1        ; File found
  780. @@LEAVE:
  781.     pop    ds
  782.     PLEAVE
  783.     ret
  784. @@NOP:    xor    ax,ax        ; File not found.
  785.     jmp short @@LEAVE
  786.     endp
  787.  
  788. CPROC    tprint
  789.     ARG    XLOC:WORD,YLOC:WORD,LN:WORD,STRING:DWORD,COLOR:WORD
  790.     PENTER    0
  791.     PushCREGS
  792.  
  793.     lds    si,[STRING]       ;; Get base address of text to print.
  794.     cmp    [LN],0        ;; If length passed is zero, then use string length.
  795.     jne    @@LNPASS    ;; use length passed.
  796.     or    si,si        ;; Null string?
  797.     jz    @@EXIT        ;; yes, exit.
  798.     mov    di,si
  799.     push    ds
  800.     pop    es
  801.     xor    ax,ax
  802.     mov    cx,-1
  803.     repnz    scasb
  804.     neg    cx
  805.     dec    cx        ; Less one.
  806.     mov    [LN],cx     ; Length of string.
  807. @@LNPASS:
  808. ;; If ylocation above are below clip window, exit.
  809.     mov    ax,[YLOC]        ;; Get destination ylocation.
  810.     or    ax,ax            ;; < top margin?
  811.     js    @@EXIT             ;; yes, doesn't fit.
  812.     cmp    ax,25            ;; > bottom margin?
  813.     jg    @@EXIT             ;; yes, doesn't fit.
  814.  
  815. ;; If xlocation > right margin? yes->exit.
  816.     mov    ax,[XLOC]        ;; Get xlocation passed?
  817.     cmp    ax,79            ;; > right margin?
  818.     jg    @@EXIT            ;; doesn't fit, exit.
  819.     or    ax,ax
  820.     jns    @@SKP1            ;; no, start postion is ok.
  821.     neg    ax            ;; Make it positive.
  822.     or    si,si            ;; Null string?
  823.     jz    @@REN
  824.     add    si,ax            ;; Number of pixels to skip.
  825. @@REN:    sub    [LN],ax         ;; Differnece, from total length.
  826.     js    @@EXIT
  827.     jz    @@EXIT             ;; If length zerod out, get out.
  828.     mov    [XLOC],0        ;; Set it.
  829. @@SKP1:
  830.     add    ax,[LN]         ;; Add total length.
  831.     dec    ax            ;; Less one to ending pixel position.
  832.     cmp    ax,79            ;; Past right margin?
  833.     jle    @@SKIP            ;; no->it fits fine.
  834.     sub    ax,79            ;; Amount past right margin.
  835.     sub    [LN],ax         ;; Lower total length.
  836.     js    @@EXIT            ;; if negitze, no go.
  837. @@SKIP:
  838.     mov    ax,0B800h
  839.     mov    es,ax
  840.     mov    ax,160
  841.     mul    [YLOC]
  842.     mov    di,ax
  843.     mov    ax,[XLOC]
  844.     shl    ax,1
  845.     add    di,ax
  846.  
  847.     mov    ax,[COLOR]
  848.     mov    ah,al            ;; Into AH.
  849.     mov    cx,[LN]         ;; Length.
  850.     or    si,si
  851.     jz    @@SPACE         ;; Fill rest with spaces
  852. @@SND:    lodsb                ;; Get character to send.
  853.     or    al,al            ;; Is it eos?
  854.     jz    @@SPACE         ;; fill rest with spaces.
  855.     stosw
  856.     loop    @@SND
  857.     jmp    @@EXIT
  858. @@SPACE:mov    al,20h            ;; Space character.
  859. @@FILL: rep    stosw            ;; Fill rest with spaces.
  860.  
  861.  
  862. @@EXIT:
  863.     PopCREGS
  864.     PLEAVE
  865.     ret
  866.     endp
  867.  
  868. CPROC    tcolor
  869.     ARG    XLOC:WORD,YLOC:WORD,LEN:WORD,COLOR:WORD
  870.     PENTER    0
  871.     PushCREGS
  872.  
  873.     mov    ax,160
  874.     mul    [YLOC]
  875.     mov    di,ax
  876.     mov    ax,[XLOC]
  877.     shl    ax,1
  878.     add    di,ax
  879.     mov    cx,[LEN]
  880.     mov    ax,0B800h
  881.     mov    es,ax
  882.     mov    ax,[COLOR]
  883.     inc    di        ; Up to attribute byte
  884. @@SND:    stosb
  885.     inc    di
  886.     loop    @@SND
  887.  
  888.     PopCREGS
  889.     PLEAVE
  890.     ret
  891.     endp
  892.  
  893. CPROC    TextCursor
  894.     ARG    XLOC:WORD,YLOC:WORD
  895.     PENTER    0
  896.  
  897.     mov    ax,[YLOC]
  898.     mov    dx,[XLOC]
  899.     mov    dh,al
  900.     mov    ah,2
  901.     mov    bh,0
  902.     int    10h
  903.  
  904.     PLEAVE
  905.     ret
  906.     endp
  907.  
  908.  
  909.     CPROC    farmov
  910.     ARG    DEST:DWORD,FROM:DWORD,MLEN:WORD
  911.     push    bp
  912.     mov    bp,sp
  913.     push    es
  914.     push    ds
  915.     push    si
  916.     push    di
  917.  
  918.     les    di,[DEST]
  919.     lds    si,[FROM]
  920.     mov    cx,[MLEN]
  921.     rep    movsb
  922.  
  923.     pop    di
  924.     pop    si
  925.     pop    ds
  926.     pop    es
  927.     pop    bp
  928.     ret
  929.     endp
  930.  
  931. CPROC    AddFar
  932.     ARG    BASE:DWORD,LOCL:WORD,LOCH:WORD
  933.     PENTER    0
  934.  
  935.     mov    ax,[word BASE+2]    ; Get segment portion of address.
  936.     xor    dx,dx
  937.  
  938.     LongShiftL    4    ; One nibble up.
  939.     add    ax,[word BASE]
  940.     adc    dx,0        ; Now have 20 bit result.
  941.  
  942.     add    ax,[LOCL]
  943.     adc    dx,[LOCH]
  944.     push    ax
  945.     LongShiftR    4    ; Into segment notation.
  946.     mov    dx,ax        ; Segment portion in DX
  947.     pop    ax
  948.     and    ax,0Fh        ; Offset portion, normalized.
  949.  
  950.     PLEAVE
  951.     ret
  952.     endp
  953.  
  954. CPROC    MakeFP
  955.     ARG    OFFS:WORD,SEGM:WORD
  956.     PENTER    0
  957.  
  958.     mov    ax,[OFFS]
  959.     mov    dx,[SEGM]
  960.  
  961.     PLEAVE
  962.     ret
  963.     endp
  964.  
  965. CPROC    MakeSeg
  966.     ARG    ADR:DWORD
  967.     PENTER    0
  968.  
  969.     mov    ax,[word ADR+2]
  970.  
  971.     PLEAVE
  972.     ret
  973.     endp
  974.  
  975.  
  976.     ENDS
  977.     END
  978.