home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / fido / pup_v2b.zip / MS-ASM.ASM < prev    next >
Assembly Source File  |  1988-01-28  |  11KB  |  561 lines

  1. title MSDOS dependent functions
  2. name msasm
  3.  
  4. comment %
  5.  
  6.     T. Jennings
  7.     13 Oct 87
  8.  
  9.     (k) all rights reversed
  10.  
  11.  
  12. These are support routines for Puppy that are
  13. currently implemented in MSDOS MASM.EXE assembler.
  14. Most of them are pretty simple; some are annoying.
  15.  
  16. Some, such as time & date, you can dummy for systems
  17. that dont support those functions, like CP/M; best to
  18. return a 0 or something rather than remove the code; 
  19. if you ever get a clock/calendar you can add it later.
  20.  
  21. Some are arbitrary 8086isms; byte order in longs. Motorola
  22. people will just have to shut up and implement! :-)
  23.  
  24. And some are just because when you invoke some supposedly
  25. simple thing like read() or open() the stupid library
  26. drags a whole bag of shit with it. There is a very simple
  27. but effective Level 1 File I/O set of calls here.
  28.  
  29.  
  30. handle= open(pathname,access);
  31. int handle,access;
  32. char *pathname;
  33.  
  34.     Open a file. handle returns either 
  35. the DOS handle, or -1 if error (file not 
  36. found). Access is: 0 == read only, 1 == write
  37. only, 2 == read/write.
  38.  
  39. handle= creat(pathname,access);
  40.  
  41.     Create a new file, delete any existing
  42. copy. Access is not used: use 0. Returns the
  43. handle or -1 if error.
  44.  
  45. v= read(handle,buffer,count);
  46. v= write(handle,buffer,count);
  47. int v,handle,count;
  48. char *buffer;
  49.  
  50.     File read or write to an opened or
  51. created file. reads or writes 'count' bytes
  52. to the file 'handle', to or from 'buffer'.
  53. Returns the number of bytes processed: equal
  54. to 'count' if sucessful.
  55.  
  56. error= close(handle)
  57. int error;
  58.  
  59.     Close an open file. Returns -1 if 
  60. error. Any buffers are flushed at this point.
  61.  
  62. pos= lseek(handle,distance,flag);
  63. long pos;    new position, else -1
  64. int handle;
  65. long distance;    displacement
  66. int flag;    0 == from BOF, 
  67.         1 == from current,
  68.         2 == from EOF,
  69.  
  70.     Seek to a position within a file.
  71.  
  72.  
  73. Get the time & date from the system, store 
  74. in the provided array. Unsupported items 
  75. should be returned as 0's.
  76.  
  77. See PUPPY.H for the definition of 'time' and
  78. date.
  79.  
  80. date= gdate();
  81. time= gtime();
  82.  
  83.  
  84. GET and SET MSDOS file creation times of the open
  85. file handle. If not supported, GET a 0 and
  86. make SET do nothing.
  87.  
  88. SET is done just before the file is closed.
  89.  
  90.     _ftime(1,handle,timedate) set time/date
  91.     _ftime(0,handle,timedate) get time/date
  92.     char *timedate;
  93.     int handle;
  94.  
  95.  
  96.  
  97.     val= _ioctl(func,handle,data,dummy)
  98.     val= _ioctl(func,handle,buff,count)
  99.     int val;    ret value or flag
  100.     int func,handle,dummy,count;
  101.     char *buff;
  102.  
  103. This is the MSDOS ioctl() function, used only
  104. to support the function badname(), in MS-C.C. The
  105. description below is only for informational 
  106. purposes; if not MSDOS, just dont implement any 
  107. of it. Refer to badname().
  108.  
  109.  
  110. RETURN:    See below
  111.     VAL returns as -1 if any error.
  112.  
  113. DESCRIPTION:
  114.     IOCTL get/set device information. The 
  115.     args and return values depend on the 
  116.     function:
  117.  
  118. func == 0    get
  119. func == 1    set
  120.     Get or set device info on (handle).
  121.     0x80 Is a device (else file)
  122.     0x01    Console input
  123.     0x02    Console out
  124.     0x04    Null device
  125.     0x08    Clock device
  126.     0x10    Special (fast) device
  127.     0x20    Raw mode
  128.     0x40    EOF on input
  129.     0x4000    Accepts control strings
  130.  
  131. func == 2    read
  132. func == 3    write
  133.     Read or write (count) bytes from
  134.     the control channel.
  135.  
  136. func == 4    read
  137. func == 5    write
  138.     Same as 2,3, except (handle) is drive
  139.     number; 0 == default, 1 == A:, etc.
  140.  
  141. func == 6    input status
  142. func == 7    output status
  143.     if (handle) is a device, returns 0 if
  144.     not ready, else non-zero if device is 
  145.     ready. For files, output always ready, 
  146.     input always ready until EOF hit.
  147.  
  148.  
  149.  
  150. Search for file first/next function. 
  151.  
  152. found= _find(pathname,n,&xfbuf);
  153. int n;
  154. char *pathname;
  155. struct _xfbuf *xfbuf;
  156.  
  157. The structure xfbuf holds search information 
  158. inbetween calls to _find(); MSDOS, unlike CP/M,
  159. allows other file operations between successive
  160. _find() calls. It is absolutely necessary to 
  161. support this. The CP/M implementation of a 
  162. Unix level 1 file system (CPMFILES.C) supports
  163. this.
  164.  
  165. This function returns 0 if there is no (more)
  166. matching filenames.
  167.  
  168. pathname is the pathname to search for; it may
  169. contain wildcards and drive specs. 
  170.  
  171. N indicates first or "next" searches; it is
  172. 0 for the first time. For systems like CP/M you
  173. should build a list of all matching files and
  174. return the first, if any. Each successive call 
  175. will return N + 1, ie. 1 2 3 ... N. You
  176. can use this to index the table built when N
  177. was 0.
  178.  
  179. xfbuf is the address of the structure used to
  180. hold information to perform the search. The
  181. contents are preserved in between calls, so
  182. you can store anything you want in it. It is 
  183. completely uninitialized space, so you should
  184. init it during the N == 0 pass.
  185.  
  186.  
  187. title XMODEM CRC Algorithm 8086 / Lattice C Compiler
  188.  
  189. I did not write this. I dont know who did. I munged an 8080
  190. M80 source into 8086 MASM.
  191.  
  192. clrcrc();
  193. CLRCRC - A call to this entry resets the CRC accumulator.
  194.  It must be called at the start of each message.
  195.  
  196.  Entry Parameters:    None.
  197.  Exit Conditions:     CRC accumulator cleared.
  198.  
  199. udcrc(c);
  200. char c;
  201. UPDCRC - A call to this entry updates the CRC accumulator.
  202.  It must be called once for each byte in the
  203.  message for which the CRC is being calculated.
  204.  
  205.  Entry Parameters:    a byte to be included
  206.              in the CRC calculation.
  207.  Exit Conditions:    CRC accumulator updated.
  208.  
  209. crc= fincrc();
  210. unsigned crc;
  211. FINCRC - A call to this entry finishes the CRC calculation
  212.  for a message which is to be TRANSMITTED. It must
  213.  be called after the last byte of the message has
  214.  been passed thru UPDCRC. It returns the calculated
  215.  CRC bytes, which must be transmitted as the final
  216.  two bytes of the message.
  217.  
  218. Note that this returns a single 16 bit value; 
  219. if transmitting bytes, it must be transmitted 
  220. upper half first then lower half:
  221.  
  222.     output (crc >> 8);
  223.     output (crc);
  224.  
  225.  Entry Parameters: None.
  226.  Exit Conditions:  calculated CRC bytes.
  227.  
  228.  
  229. error= chkcrc();
  230. int error;
  231. CHKCRC - A call to this routine checks the CRC bytes of
  232.  a RECEIVED message and returns a code to indicate
  233.  whether the message was received correctly. It must
  234.  be called after the message AND the two CRC bytes
  235.  have been received AND passed thru UPDCRC.
  236.  
  237.  Entry Parameters: None.
  238.  Exit Conditions:  0 if message ok.
  239.            non-zero if message garbled.
  240.  
  241. end of comment ------> %
  242.  
  243. ;
  244. ;Define a (paranoid) near function.
  245. ;
  246.  func macro procname
  247.   public procname
  248.   procname proc near
  249.     push    bp
  250.     mov    bp,sp
  251.     push    ds
  252.     push    es
  253.     mov    ax,ds
  254.     mov    es,ax
  255.  endm
  256. ;
  257. ;Close a function declaration.
  258. ;
  259.  endf macro procname
  260.     pop    es
  261.     pop    ds
  262.     pop    bp
  263.     ret
  264.   procname endp
  265.  endm
  266. ;
  267. ;Define the args on the stack.
  268. ;
  269. arg0    equ    [bp+4]
  270. arg1    equ    [bp+6]
  271. arg2    equ    [bp+8]
  272. arg3    equ    [bp+10]
  273. arg4    equ    [bp+12]
  274. arg5    equ    [bp+14]
  275. arg6    equ    [bp+16]
  276. arg7    equ    [bp+18]
  277. arg8    equ    [bp+20]
  278. ;
  279. ;dgroup just generates a runtime accessible 
  280. ;constant 'dataseg' so that drivers and other
  281. ;such oddities can set DS: to access data 
  282. ;during interrupt service routines.
  283. ;
  284. dgroup group data
  285. data segment byte public 'data'
  286. assume ds:dgroup
  287.  
  288. public dataseg
  289. dataseg dw dgroup    ;so drivers can find it
  290.  
  291. data ends
  292.  
  293. pgroup group prog        ;MASM mumbo jumbo
  294. prog segment byte public 'prog'
  295. assume cs:pgroup
  296. page
  297. ;
  298. ;Return the time or date components.
  299. ;
  300. func gdate
  301.     mov    ah,2ah
  302.     int    21h        ;get date
  303.     mov    bx,cx        ;build it here
  304.     sub    bx,1980        ;CX = year
  305.     mov    cl,9
  306.     shl    bx,cl        ;shift it in,
  307.  
  308.     mov    al,dh        ;DH = month
  309.     and    ax,0fh
  310.     mov    cl,5
  311.     shl    ax,cl
  312.     or    bx,ax
  313.  
  314.     mov    al,dl        ;DL = day
  315.     and    ax,1fh
  316.     or    ax,bx
  317. endf gdate
  318.  
  319. func gtime
  320.     mov    ah,2ch
  321.     int    21h        ;get time
  322.  
  323.     mov    bl,cl        ;CL = mins
  324.     and    bx,3fh
  325.     mov    cl,5
  326.     shl    bx,cl
  327.  
  328.     mov    al,ch        ;CH = hour
  329.     xor    ah,ah
  330.     mov    cl,11
  331.     shl    ax,cl
  332.     or    bx,ax
  333.  
  334.     mov    al,dh        ;DH = secs
  335.     and    ax,1fh        ;only 2 sec
  336.     add    ax,ax        ;resolution
  337.     or    ax,bx
  338. endf gtime
  339. page
  340. ;
  341. ;Convert a long to a character array.
  342. ;
  343. func _ltoc 
  344.     cld
  345.     mov    di,arg2
  346.     mov    ax,arg0        ;LSW,
  347.     stosw
  348.     mov    ax,arg1        ;MSW
  349.     stosw
  350. endf _ltoc 
  351. ;
  352. ;Convert a character array to a long.
  353. ;
  354. func _ctol 
  355.     cld
  356.     mov    si,arg0
  357.     lodsw        ;get LSW,
  358.     mov    bx,ax    ;MSW to AX,
  359.     lodsw        ;return AX:BX.
  360. endf _ctol 
  361. page
  362. ;
  363. ;Get and set File times.
  364. ;
  365. func _ftime 
  366.     mov    al,arg0        ;1=set, 0=get,
  367.     and    al,1
  368.     mov    bx,arg1        ;file handle,
  369.     mov    si,arg2        ;ptr to array,
  370.     mov    cx,[si]        ;CX= time,
  371.     mov    dx,[si+2]    ;DX= date,
  372.     push    si
  373.     mov    ah,87
  374.     int    33
  375.     pop    si
  376.     test byte ptr arg0,1    ;if get time,
  377.     jnz    _ft1
  378.     mov    [si],cx        ;return time,
  379.     mov    [si+2],dx    ;return date,
  380. _ft1:
  381. endf _ftime 
  382. page
  383. ;
  384. ;Perform IOCTL() functions.
  385. ;
  386. func _ioctl 
  387.     mov    al,arg0        ;func
  388.     mov    bx,arg1        ;handle
  389.     mov    dx,arg2        ;data/buff
  390.     mov    cx,arg3        ;dummy/count
  391.     mov    ah,68
  392.     int    33
  393.  
  394.     jnc    _io1        ;if error,
  395.     mov    ax,-1        ;return -1
  396.     jmp    short _ioz
  397.  
  398. _io1:    cmp    word ptr arg0,2    ;functions 0,1
  399.     jae    _io2        ;return from DX
  400.     mov    ax,dx
  401.     jmp    short _ioz
  402.  
  403. _io2:    cmp    word ptr arg0,6    ;functions 2-5
  404.     jb    _ioz        ;return from AX
  405.     mov    ah,0        ;6,7 from AL
  406. _ioz:
  407. endf _ioctl 
  408. page
  409. ;
  410. ;Do search first/next.
  411. ;
  412. func _find 
  413.     mov    dx,arg2        ;XFBUF ptr,
  414.     mov    bx,dx
  415.     mov    ah,26        ;set DMA addr
  416.     int    21h        ;to buffer,
  417.     mov    cx,0        ;CX == attrib
  418.     mov    dx,arg0        ;pathname
  419.     mov    ax,arg1        ;iteration flag
  420.     or    ax,ax
  421.     jz    f0
  422.     mov    al,1        ;make 0,1
  423.  
  424. f0:    mov    ah,78        ;search first
  425.     cmp    al,0        ;if AL == 0,
  426.     je    f1
  427.     mov    ah,79        ;else search next
  428. f1:    int    21h        ;DOS call,
  429.     mov    ax,-1        ;make 0 == not found
  430.     adc    ax,0
  431. endf _find 
  432. page
  433. ;
  434. ;Our very own little bit of data space.
  435. ;
  436. crc    dw    0    ;in CS space
  437.  
  438. ;
  439. ;Initialize the CRC.
  440. ;
  441. func clrcrc
  442.     mov    cs:crc,0
  443. endf clrcrc
  444.  
  445. ;
  446. ;Update the CRC with the new byte. The method used is 
  447. ;the CCITT polynomial:
  448. ;
  449. ;    x^16 + x^12 + x^5 + 1
  450. ;
  451. ;An alternate method often used in synchronous
  452. ;protocols is:
  453. ;
  454. ;    x^16 + x^15 + x^2 + 1
  455. ;
  456. ;Which can be generated by changing the XOR pattern
  457. ;from 1021 hex to 8005 hex.
  458. ;
  459. func updcrc
  460.     mov    bx,cs:crc    ;BX == CRC a reg for speed,
  461.     mov    cx,8        ;CX == bits in a byte,
  462.     mov    ax,arg0        ;AL == msg byte,
  463.  
  464. u1:    rcl    al,1        ;MSB -> carry,
  465.     rcl    bx,1        ;    -> CRC LSB,
  466.     jnc    u2
  467.     xor    bx,1021h
  468. u2:    loop    u1
  469.  
  470.     mov    cs:crc,bx
  471. endf updcrc
  472.  
  473. ;
  474. ;Finish off the CRC.
  475. ;
  476. func fincrc
  477.     xor    ax,ax        ;do two zeros
  478.     push    ax
  479.     call    updcrc
  480.     call    updcrc
  481.     pop    ax
  482.     mov    ax,cs:crc    ;return finished CRC
  483. endf fincrc
  484.  
  485. ;
  486. ;Check the calculated CRC. Return 0 if OK.
  487. ;
  488. func chkcrc
  489.     mov    ax,cs:crc
  490. endf chkcrc
  491. page
  492. ;
  493. ;File functions
  494. ;
  495. func open 
  496.     mov    ah,61
  497.     call    opncrt
  498. endf open
  499.  
  500. func creat 
  501.     mov    ah,60
  502.     call    opncrt
  503. endf creat
  504.  
  505. opncrt:
  506.     mov    dx,arg0        ;pathname,
  507.     mov    al,arg1        ;access,
  508.     xor    bx,bx
  509.     xor    cx,cx
  510.     int    21h        ;do it,
  511.     jnc    opncrt1
  512.     mov    ax,-1        ;error!
  513. opncrt1:ret
  514. ;
  515. ;Close a file
  516. ;
  517. func close 
  518.     mov    ah,62
  519.     mov    bx,arg0        ;handle,
  520.     int    21h
  521. endf close 
  522. ;
  523. ;File read & write
  524. ;
  525. func read 
  526.     mov    ah,63
  527.     call    rdwrt
  528. endf read
  529.  
  530. func write 
  531.     mov    ah,64
  532.     call    rdwrt
  533. endf write
  534.  
  535. rdwrt:    mov    bx,arg0        ;handle,
  536.     mov    dx,arg1        ;buffer,
  537.     mov    cx,arg2        ;count,
  538.     int    21h
  539.     ret
  540. ;
  541. ;File seek
  542. ;
  543. func lseek 
  544.     mov    bx,arg0        ;BX == handle,
  545.     mov    dx,arg1        ;CX:DX == disp,
  546.     mov    cx,arg2
  547.     mov    ah,42h
  548.     mov    al,arg3        ;unless error,
  549.     int    21h        ;DX:AX == pos,
  550.     mov    bx,ax
  551.     mov    ax,dx
  552.     jnc    _x1
  553.     mov    ax,-1
  554.     mov    bx,-1
  555. _x1:
  556. endf lseek 
  557.  
  558. prog ends
  559.  
  560. end
  561.