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