DOS 3.30 source code leak
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3710 lines
67 KiB

5 years ago
  1. _ _ | | _ _
  2. _ ______________
  3. Write Handle (Function 40H)
  4. Call:
  5. AH = 40H
  6. BX
  7. Handle
  8. CX
  9. Bytes to write
  10. DS:DX
  11. Pointer to buffer
  12. Return:
  13. Carry set:
  14. AX
  15. 5 = Access denied
  16. 6 = Invalid handle
  17. Carry not set:
  18. AX
  19. Bytes written
  20. Comments:
  21. Function 40H writes to the file or device associated with the specified han-
  22. dle. BX must contain the handle. CX must contain the number of bytes to
  23. be written. DX must contain the offset (to the segment address in DS) of
  24. the data to be written.
  25. If you set CX to zero, the file will be truncated at the current position of
  26. the file pointer. MS-DOS will not perform the write if the handle is read-
  27. only.
  28. If there is no error, AX returns the number of bytes written. Be sure to
  29. check AX after performing a write. If its value is less than the number in
  30. CX when the call was made, it indicates an error, even though the carry
  31. flag isn't set. If AX contains 0, and if the target is a disk file, the disk is
  32. full.
  33. If you use this function request to write to standard output, you can
  34. redirect the output. If you call this request with CX=0, the file size is set
  35. to the value of the read/write pointer. To satisfy the new file size, alloca-
  36. tion units are allocated or released, as required.
  37. If there is an error, the carry flag (CF) is set and the error code returns in
  38. AX:
  39. Code
  40. 1
  41. _ _ | | _ _
  42. _ _ | | _ _
  43. _ ______________
  44. Meaning
  45. _ ________________________________________________________________
  46. 5 Handle not open for writing
  47. 6 Handle not open or invalid
  48. Macro Definition:
  49. write_handle macro handle,buffer,bytes
  50. mov bx,handle
  51. mov dx,offset buffer
  52. mov cx,bytes
  53. mov ah,40H
  54. int 21H
  55. endm
  56. Example:
  57. The following program creates a file named dir.tmp, containing the
  58. filename and extension of each file in the current directory, in the current
  59. directory on the disk in drive B.
  60. srch_file db "b:*.*",0
  61. tmp_file db "b:dir.tmp",0
  62. buffer db 43 dup (?)
  63. handle dw ?
  64. ;
  65. begin: set_dta buffer ;See Function 1AH
  66. find_first_file srch_file,16H ;Check directory
  67. cmp ax,12H ;Directory empty?
  68. je return ;Yes, go home
  69. create_handle tmp_file,0 ;See Function 3CH
  70. jc error_create ;Routine not shown
  71. mov handle,ax ;Save handle
  72. write_it: write_handle handle,buffer[1EH],12 ;THIS FUNCTION
  73. jc error_write ;Routine not shown
  74. find_next_file ;Check directory
  75. cmp ax,12H ;Another entry?
  76. je all_done ;No, go home
  77. jmp write_it ;Yes, write record
  78. all_done: close_handle handle ;See Function 3EH
  79. jc error_close ;Routine not shown
  80. 2
  81. _ _ | | _ _
  82. _ _ | | _ _
  83. _ ______________
  84. Delete Directory Entry [Unlink] (Function 41H)
  85. Call:
  86. AH = 41H
  87. DS:DX
  88. Pointer to pathname
  89. Return:
  90. Carry set:
  91. AX
  92. 2 = File not found
  93. 3 = Path not found
  94. 5 = Access denied
  95. Carry not set:
  96. No error
  97. Comments:
  98. Function 41H erases a file by deleting its directory entry. DX must con-
  99. tain the offset (from the segment address in DS) of an ASCIZ string that
  100. specifies the pathname of the file that you want to delete. You cannot use
  101. wildcard characters.
  102. If the file exists and is not read-only, the call deletes it. If there is an error,
  103. the call sets the carry flag (CF) and the error code returns in AX:
  104. Code
  105. Meaning
  106. _ ________________________________________________________________
  107. 2 File doesn't exist, or specifies a directory
  108. 3 Path is invalid
  109. 5 File is read-only
  110. To delete a file with the read-only attribute, first change its attribute to 0
  111. with Function 43H (Get/Set File Attribute).
  112. 3
  113. _ _ | | _ _
  114. _ _ | | _ _
  115. _ ______________
  116. Macro Definition:
  117. delete_entry macro path
  118. mov dx,offset path
  119. mov ah,41H
  120. int 21H
  121. endm
  122. Example:
  123. The following program deletes all files, dated before December 31, 1986,
  124. from the disk in drive B.
  125. year db 1986
  126. month db 12
  127. day db 31
  128. files db ?
  129. message db "NO FILES DELETED.",0DH,0AH,"$"
  130. path db "b:*.*", 0
  131. buffer db 43 dup (?)
  132. ;
  133. begin: set_dta buffer ;See Function 1AH
  134. select_disk "B" ;See Function 0EH
  135. find_first_file path,0 ;See Function 4EH
  136. jnc compare ;got one
  137. jmp all_done ;no match, go home
  138. compare: convert_date buffer[-1] ;See end of chapter
  139. cmp cx,year ;After 1986?
  140. jg next ;Yes, don't delete
  141. cmp dl,month ;After December?
  142. jg next ;Yes, don't delete
  143. cmp dh,day ;31st or after?
  144. jge next ;Yes, don't delete
  145. delete_entry buffer[1EH] ;THIS FUNCTION
  146. jc error_delete ;Routine not shown
  147. inc files ;Bump file counter
  148. next: find_next_file ;Check directory
  149. jnc compare ;Go home if done
  150. how_many: cmp files,0 ;Was directory empty?
  151. je all_done ;Yes, go home
  152. convert files,10,message ;See end of chapter
  153. all_done: display message ;See Function 09H
  154. select_disk "A" ;See Function 0EH
  155. 4
  156. _ _ | | _ _
  157. _ _ | | _ _
  158. _ ______________
  159. Move File Pointer (Function 42H)
  160. Call:
  161. AH = 42H
  162. AL
  163. Method of moving
  164. BX
  165. Handle
  166. CX:DX
  167. Distance in bytes (offset)
  168. Return:
  169. Carry set:
  170. AX
  171. 1 = Invalid function
  172. 6 = Invalid handle
  173. Carry not set:
  174. DX:AX
  175. New read/write pointer location
  176. Comments:
  177. Function 42H moves the read/write pointer of the file associated with the
  178. specified handle. BX must contain the handle. CX and DX must contain a
  179. 32-bit offset (CX contains the most significant byte). AL must contain a
  180. code that specifies how to move the pointer:
  181. Code
  182. Cursor Moved to
  183. _ ________________________________________________________________
  184. 0 Beginning of file plus the offset
  185. 1 Current pointer location plus the offset
  186. 2 End of file plus the offset
  187. DX and AX return the new location of the read/write pointer (a 32-bit
  188. integer; DX contains the most significant byte). You can determine the
  189. length of a file by setting CX:DX to 0, AL to 2, and calling this function.
  190. DX:AX returns the offset of the byte following the last byte in the file (size
  191. of the file in bytes).
  192. If there is an error, the carry flag (CF) is set and the error code returns in
  193. AX:
  194. Code
  195. Meaning
  196. _ ________________________________________________________________
  197. 5
  198. _ _ | | _ _
  199. _ _ | | _ _
  200. _ ______________
  201. 1 AL not 0, 1, or 2
  202. 6 Handle not open
  203. Macro Definition:
  204. move_ptr macro handle,high,low,method
  205. mov bx,handle
  206. mov cx,high
  207. mov dx,low
  208. mov al,method
  209. mov ah,42H
  210. int 21H
  211. endm
  212. Example:
  213. The following program prompts for a letter, converts it to its alphabetic
  214. sequence (A=1, B=2, etc.), then reads and displays the corresponding
  215. record from the file named alphabet.dat that is in the current directory on
  216. the disk in drive B. The file contains 26 records, each 28 bytes long.
  217. file db "b:alphabet.dat",0
  218. buffer db 28 dup (?),"$"
  219. prompt db "Enter letter: $"
  220. crlf db 0DH,0AH,"$"
  221. handle db ?
  222. record_length dw 28
  223. ;
  224. begin: open_handle file,0 ;See Function 3DH
  225. jc error_open ;Routine not shown
  226. mov handle,ax ;Save handle
  227. get_char: display prompt ;See Function 09H
  228. read_kbd_and_echo ;See Function 01H
  229. sub al,41h ;Convert to sequence
  230. mul byte ptr record_length ;Calculate offset
  231. move_ptr handle,0,ax,0 ;THIS FUNCTION
  232. jc error_move ;Routine not shown
  233. read_handle handle,buffer,record_length
  234. jc error_read ;Routine not shown
  235. cmp ax,0 ;End of file?
  236. je return ;Yes, go home
  237. display crlf ;See Function 09H
  238. display buffer ;See Function 09H
  239. display crlf ;See Function 09H
  240. jmp get_char ;Get another character
  241. 6
  242. _ _ | | _ _
  243. _ _ | | _ _
  244. _ ______________
  245. Get/Set File Attributes (Function 43H)
  246. Call:
  247. AH = 43H
  248. AL
  249. 0 = Get attributes
  250. 1 = Set attributes
  251. CX (if AL=1)
  252. Attributes to be set
  253. DS:DX
  254. Pointer to pathname
  255. Return:
  256. Carry set:
  257. AX
  258. 1 = Invalid function
  259. 2 = File not found
  260. 3 = Path not found
  261. 5 = Access denied
  262. Carry not set:
  263. CX
  264. Attribute byte (if AL=0)
  265. Comments:
  266. Function 43H gets or sets the attributes of a file. DX must contain the
  267. offset (from the segment address in DS) of an ASCIZ string that specifies the
  268. pathname of a file. AL must specify whether to get or set the attribute
  269. (0=get, 1=set).
  270. If AL is 0 (get the attribute), the attribute byte returns in CX. If AL is 1
  271. (set the attribute), CX must contain the attributes to be set. The attri-
  272. butes are described under "File Attributes" earlier in this chapter.
  273. You cannot change the VolumeID bit (08H) or the Subdirectory bit (10H)
  274. of the attribute byte with this function.
  275. 7
  276. _ _ | | _ _
  277. _ _ | | _ _
  278. _ ______________
  279. If there is an error, the carry flag (CF) is set and the error code returns in
  280. AX:
  281. Code
  282. Meaning
  283. _ ________________________________________________________________
  284. 1 AL not 0 or 1
  285. 2 File doesn't exist
  286. 3 Path invalid
  287. 5 Attribute in CX cannot be changed (Subdirectory or VolumeID).
  288. Macro Definition:
  289. change_attr macro path,action,attrib
  290. mov dx,offset path
  291. mov al,action
  292. mov cx,attrib
  293. mov ah,43H
  294. int 21H
  295. endm
  296. Example:
  297. The following program displays the attributes assigned to the file named
  298. report.asm that is in the current directory on the disk in drive B.
  299. header db 15 dup (20h),"Read-",0DH,0AH
  300. db "Filename Only Hidden "
  301. db "System Volume Sub-Dir Archive"
  302. db 0DH,0AH,0DH,0AH,"$"
  303. path db "b:report.asm",3 dup (0),"$"
  304. attribute dw ?
  305. blanks db 9 dup (20h),"$"
  306. ;
  307. begin: change_attr path,0,0 ;THIS FUNCTION
  308. jc error_mode ;Routine not shown
  309. mov attribute,cx ;Save attribute byte
  310. display header ;See Function 09H
  311. display path ;See Function 09H
  312. mov cx,6 ;Check 6 bits (0-5)
  313. mov bx,1 ;Start with bit 0
  314. chk_bit: test attribute,bx ;Is the bit set?
  315. jz no_attr ;No
  316. display_char "X" ;See Function 02H
  317. jmp short next_bit ;Done with this bit
  318. no_attr: display_char 20h ;See Function 02H
  319. next_bit: display blanks ;See Function 09H
  320. shl bx,1 ;Move to next bit
  321. loop chk_bit ;Check it
  322. 8
  323. _ _ | | _ _
  324. _ _ | | _ _
  325. _ ______________
  326. IOCtl Data (Function 44H, Codes 0 and 1)
  327. Call:
  328. AH = 44H
  329. AL
  330. 0 = Get device data
  331. 1 = Set device data
  332. BX
  333. Handle
  334. DX
  335. Device data (see text)
  336. Return:
  337. Carry set:
  338. AX
  339. 1 = Invalid function
  340. 6 = Invalid handle
  341. Carry not set:
  342. DX
  343. Device data
  344. Comments:
  345. Function 44H, Codes 0 and 1, either gets or sets the data MS-DOS uses to
  346. control the device. AL must contain 0 to get the data or 1 to set it. BX
  347. must contain the handle. If AL is 1, DH must contain 0.
  348. The device-data word is specified or returned in DX. If bit 7 of the data is
  349. 1, the handle refers to a device and the other bits have the following mean-
  350. ings:
  351. 9
  352. _ _ | | _ _
  353. _ _ | | _ _
  354. _ ______________
  355. Table 0.1
  356. MS-DOS Data Bit Values
  357. _ _________________________________________________________________________
  358. Bit Value Meaning
  359. _ _________________________________________________________________________
  360. 0 1 Console input device
  361. 1 1 Console output device
  362. 2 1 Null device
  363. 3 1 Clock device
  364. 4 1 Reserved
  365. 5 1 Don't check for control characters
  366. 0 Check for control characters
  367. 6 0 End of file on input
  368. 8-10 Reserved
  369. 11 1 Device understands open/close
  370. 12 Reserved
  371. 13 1 Device supports output until busy
  372. 14 1
  373. Device can process control strings sent with Function 44H, Codes
  374. 2 and 3 (IOCtl character); bit can be read only, but not set
  375. 15 Reserved
  376. _ _________________________________________________________________________
  377. You must set the reserved bits to zero.
  378. The control characters referred to in the description of bit 5 are
  379. CONTROL-C, CONTROL-P, CONTROL-S, and CONTROL-Z. To read these charac-
  380. ters as data, instead of as control characters, you must set bit 5 and use
  381. either Function 33H, CONTROL-C Check, or the MS-DOS break command
  382. to turn off CONTROL-C checking.
  383. If bit 7 of DX is 0, the handle refers to a file and the other bits have the
  384. following meanings:
  385. Bit Value Meaning
  386. _ ________________________________________________________________
  387. 0-5 Drive number (0=A, 1=B, etc.)
  388. 6 0 The file has been written
  389. 8-15 Reserved
  390. _ ________________________________________________________________
  391. 10
  392. _ _ | | _ _
  393. _ _ | | _ _
  394. _ ______________
  395. If there is an error, the carry flag (CF) is set and the error code returns in
  396. AX:
  397. Code
  398. Meaning
  399. _ ________________________________________________________________
  400. 1 AL not 0 or 1, or AL is 1 but DH is not 0
  401. 6 Handle in BX not open or is invalid
  402. Macro Definition:
  403. ioctl_data macro code,handle
  404. mov bx,handle
  405. mov al,code
  406. mov ah,44H
  407. int 21H
  408. endm
  409. Example:
  410. The following program gets the device data for standard output, sets the
  411. bit that specifies not to check for control characters (bit 5), and then
  412. clears the bit.
  413. get equ 0
  414. set equ 1
  415. stdout equ 1
  416. ;
  417. begin: ioctl_data get,stdout ;THIS FUNCTION
  418. jc error ;routine not shown
  419. mov dh,0 ;clear DH
  420. or dl,20H ;set bit 5
  421. ioctl_data set,stdout ;THIS FUNCTION
  422. jc error ;routine not shown
  423. ;
  424. ; <control characters now treated as data, or "raw mode">
  425. ;
  426. ioctl_data get,stdout ;THIS FUNCTION
  427. jc error ;routine not shown
  428. mov dh,0 ;clear DH
  429. and dl,0DFH ;clear bit 5
  430. ioctl_data set,stdout ;THIS FUNCTION
  431. ;
  432. ; <control characters now interpreted, or "cooked mode">
  433. ;
  434. 11
  435. _ _ | | _ _
  436. _ _ | | _ _
  437. _ ______________
  438. IOCtl Character (Function 44H, Codes 2 and 3)
  439. Call:
  440. AH = 44H
  441. AL
  442. 2 = Send control data
  443. 3 = Receive control data
  444. BX
  445. Handle
  446. CX
  447. Bytes to read or write
  448. DS:DX
  449. Pointer to buffer
  450. Return:
  451. Carry set:
  452. AX
  453. 1 = Invalid function
  454. 6 = Invalid handle
  455. Carry not set:
  456. AX
  457. Bytes transferred
  458. Comments:
  459. Function 44H, Codes 2 and 3, sends or receives control data to or from a
  460. character device. AL must contain 2 to send data or 3 to receive. BX must
  461. contain the handle of a character device, such as a printer or serial port.
  462. CX must contain the number of bytes to be read or written. DX must con-
  463. tain the offset (to the segment address in DS) of the data buffer.
  464. AX returns the number of bytes transferred. The device driver must sup-
  465. port the IOCtl interface.
  466. If there is an error, the carry flag (CF) is set and the error code returns in
  467. AX:
  468. Code
  469. Meaning
  470. _ ________________________________________________________________
  471. 1 AL not 2 or 3, or device cannot perform the specified function
  472. 6 Handle in BX not open or doesn't exist
  473. 12
  474. _ _ | | _ _
  475. _ _ | | _ _
  476. _ ______________
  477. Macro Definition:
  478. ioctl_char macro code,handle,buffer
  479. mov bx,handle
  480. mov dx,offset buffer
  481. mov al,code
  482. mov ah,44H
  483. int 21H
  484. endm
  485. Example:
  486. No general example is applicable, since processing of IOCtl control data
  487. depends on the device being used, as well as the device driver.
  488. 13
  489. _ _ | | _ _
  490. _ _ | | _ _
  491. _ ______________
  492. IOCtl Block (Function 44H, Codes 4 and 5)
  493. Call:
  494. AH = 44H
  495. AL
  496. 4 = Send control data
  497. 5 = Receive control data
  498. BL
  499. Drive number (0=default, 1=A, etc.)
  500. CX
  501. Bytes to read or write
  502. DS:DX
  503. Pointer to buffer
  504. Return:
  505. Carry set:
  506. AX
  507. 1 = Invalid function
  508. 5 = Invalid drive
  509. Carry not set:
  510. AX
  511. Bytes transferred
  512. Comments:
  513. Function 44H, Codes 4 and 5, sends or receives control data to or from a
  514. block device. AL must contain 4 to send data or 5 to receive. BL must con-
  515. tain the drive number (0=default, 1=A, etc.). CX must contain the
  516. number of bytes to be read or written. DX must contain the offset (to the
  517. segment address in DS) of the data buffer.
  518. AX returns the number of bytes transferred. The device driver must sup-
  519. port the IOCtl interface. To determine whether it does, use Function 44H,
  520. Code 0, to get the device data, and test bit 14; if the bit is set, the driver
  521. supports IOCtl.
  522. 14
  523. _ _ | | _ _
  524. _ _ | | _ _
  525. _ ______________
  526. If there is an error, the carry flag (CF) is set and the error code returns in
  527. AX:
  528. Code
  529. Meaning
  530. _ ________________________________________________________________
  531. 1 AL not 4 or 5, or device cannot perform the specified function
  532. 5 Number in BL not a valid drive number
  533. Macro Definition:
  534. ioctl_status macro code,drive,buffer
  535. mov bl,drive
  536. mov dx,offset buffer
  537. mov al,code
  538. mov ah,44H
  539. int 21H
  540. endm
  541. Example:
  542. No general example is applicable, since processing of IOCtl control data
  543. depends on the device being used, as well as the device driver.
  544. 15
  545. _ _ | | _ _
  546. _ _ | | _ _
  547. _ ______________
  548. IOCtl Status (Function 44H, Codes 6 and 7)
  549. Call:
  550. AH = 44H
  551. AL
  552. 6 = Check input status
  553. 7 = Check output status
  554. BX
  555. Handle
  556. Return:
  557. Carry set:
  558. AX
  559. 1 = Invalid function
  560. 5 = Access denied
  561. 6 = Invalid handle
  562. 13 = Invalid data
  563. Carry not set:
  564. AL
  565. 00H = Not ready
  566. 0FFH= Ready
  567. Comments:
  568. Function 44H, Codes 6 and 7, checks whether the file or device associated
  569. with a handle is ready. AL must contain 6 to check whether the handle is
  570. ready for input or 7 to check whether the handle is ready for output. BX
  571. must contain the handle.
  572. AL returns the status:
  573. Meaning for Meaning for Meaning for
  574. Value Device Input File Output File
  575. _ ________________________________________________________________
  576. 00H Not ready Pointer is at EOF Ready
  577. 0FFH Ready Ready Ready
  578. _ ________________________________________________________________
  579. An output file always returns ready, even if the disk is full.
  580. If there is an error, the carry flag (CF) is set and the error code returns in
  581. AX:
  582. Code
  583. Meaning
  584. _ ________________________________________________________________
  585. 16
  586. _ _ | | _ _
  587. _ _ | | _ _
  588. _ ______________
  589. 1 AL not 6 or 7
  590. 5 Access denied
  591. 6 Number in BX not a valid, open handle
  592. 13 Invalid data
  593. Macro Definition:
  594. ioctl_status macro code,handle
  595. mov bx,handle
  596. mov al,code
  597. mov ah,44H
  598. int 21H
  599. endm
  600. Example:
  601. The following program displays a message that tells whether the file asso-
  602. ciated with handle 6 is ready for input or whether it is at end-of-file.
  603. stdout equ 1
  604. ;
  605. message db "File is "
  606. ready db "ready."
  607. at_eof db "at EOF."
  608. crlf db ODH,OAH
  609. ;
  610. begin: write_handle stdout,message,8 ;display message
  611. jc write_error ;routine not shown
  612. ioctl_status 6 ;THIS FUNCTION
  613. jc ioctl_error ;routine not shown
  614. cmp al,0 ;check status code
  615. jne not_eof ;file is ready
  616. write_handle stdout,at_eof,7 ;see Function 40H
  617. jc write_error ;routine not shown
  618. jmp all_done ;clean up & go home
  619. not_eof: write_handle stdout,ready,6 ;see Function 40H
  620. all_done: write_handle stdout,crlf,2 ;see Function 40H
  621. jc write_error ;routine not shown
  622. 17
  623. _ _ | | _ _
  624. _ _ | | _ _
  625. _ ______________
  626. IOCtl Is Changeable (Function 44H, Code 08H)
  627. Call:
  628. AH = 44H
  629. AL = 08H
  630. BL
  631. Drive number (0=default, 1=A, etc.)
  632. Return:
  633. Carry set:
  634. AX
  635. 1 = Invalid function
  636. 15 = Invalid drive
  637. Carry not set:
  638. AX
  639. 0 = Changeable
  640. 1 = Not changeable
  641. Comments:
  642. Function 44H, Code 08H, checks whether a drive contains a removable or
  643. nonremovable disk. BL must contain the drive number (0=default, 1=A,
  644. etc.). AX returns 0 if the disk can be changed, 1 if it cannot.
  645. This call lets a program determine whether to issue a message to change
  646. disks.
  647. If there is an error, the carry flag (CF) is set and the error code returns in
  648. AX.
  649. Code
  650. Meaning
  651. _ ________________________________________________________________
  652. 1 Device does not support this call
  653. 15 Number in BL not a valid drive number
  654. When the call returns error 1 (because the driver doesn't support it), the
  655. caller asssumes that the driver cannot be changed.
  656. 18
  657. _ _ | | _ _
  658. _ _ | | _ _
  659. _ ______________
  660. Macro Definition:
  661. ioctl_change macro drive
  662. mov bl, drive
  663. mov al, 08H
  664. mov ah, 44H
  665. int 21H
  666. endm
  667. Example:
  668. The following program checks whether the current drive contains a remov-
  669. able disk. If not, processing continues; if so, the program prompts the user
  670. to replace the disk in the current drive.
  671. stdout equ 1
  672. ;
  673. message db "Please replace disk in drive "
  674. drives db "ABCD"
  675. crlf db 0DH,0AH
  676. ;
  677. begin: ioctl_change 0 ;THIS FUNCTION
  678. jc ioctl_error ;routine not shown
  679. cmp ax,0 ;current drive changeable?
  680. jne continue ;no, continue processing
  681. write_handle stdout,message,29 ;see Function 40H
  682. jc write_error ;routine not shown
  683. current_disk ;see Function 19H
  684. xor bx,bx ;clear index
  685. mov bl,al ;get current drive
  686. display_char drives[bx] ;see Function 02H
  687. write_handle stdout,crlf,2 ;see Function 40H
  688. jc write_error ;routine not shown
  689. continue:
  690. ; (Further processing here)
  691. 19
  692. _ _ | | _ _
  693. _ _ | | _ _
  694. _ ______________
  695. IOCtl Is Redirected Block (Function 44H, Code
  696. 09H)
  697. Call:
  698. AH = 44H
  699. AL = 09H
  700. BL
  701. Drive number (0=default, 1=A, etc.)
  702. Return:
  703. Carry set:
  704. AX
  705. 1 = Invalid function code
  706. 15 = Invalid drive number
  707. Carry not set:
  708. DX
  709. Device-attribute bits
  710. Comments:
  711. Function 44H, Code 09H, checks whether a drive letter refers to a drive on
  712. a Microsoft Networks workstation (local) or is redirected to a server
  713. (remote). BL must contain the drive number (0=default, 1=A, etc.).
  714. If the block device is local, DX returns the attribute word from the device
  715. header. If the block device is remote, only bit 12 (1000H) is set; the other
  716. bits are 0 (reserved).
  717. An application program should not test bit 12, because applications
  718. should not make distinctions between local and remote files (or devices).
  719. Programs should be written so that they will be independent of the loca-
  720. tion of a device that has been removed.
  721. If there is an error, the carry flag (CF) is set and the error code returns in
  722. AX:
  723. Code
  724. Meaning
  725. _ ________________________________________________________________
  726. 1 File sharing must be loaded to use this system call
  727. 15 Number in BL not a valid drive number
  728. 20
  729. _ _ | | _ _
  730. _ _ | | _ _
  731. _ ______________
  732. Macro Definition:
  733. ioctl_rblock macro drive
  734. mov bl, drive
  735. mov al, 09H
  736. mov ah, 44H
  737. int 21H
  738. endm
  739. Example:
  740. The following program checks whether drive B is local or remote and
  741. displays the appropriate message.
  742. stdout equ 1
  743. ;
  744. message db "Drive B: is "
  745. loc db "local."
  746. rem db "remote."
  747. crlf db 0DH,0AH
  748. ;
  749. begin: write_handle stdout,message,12 ;display message
  750. jc write_error ;routine not shown
  751. ioctl_rblock 2 ;THIS FUNCTION
  752. jc ioctl_error ;routine not shown
  753. test dx,1000h ;bit 12 set?
  754. jnz not_loc ;yes, it's remote
  755. write_handle stdout,loc,6 ;see Function 40H
  756. jc write_error ;routine not shown
  757. jmp done
  758. not_loc: write_handle stdout,rem,7 ;see Function 40H
  759. jc write_error ;routine not shown
  760. done: write_handle stdout,crlf,2 ;see Function 40H
  761. jc write_error ;routine not shown
  762. 21
  763. _ _ | | _ _
  764. _ _ | | _ _
  765. _ ______________
  766. IOCtl Is Redirected Handle (Function 44H,
  767. Code 0AH)
  768. Call:
  769. AH = 44H
  770. AL = 0AH
  771. BX
  772. Handle
  773. Return:
  774. Carry set:
  775. AX
  776. 1 = Invalid function code
  777. 6 = Invalid handle
  778. Carry not set:
  779. DX
  780. IOCtl bit field
  781. Comments:
  782. Function 44H, Code 0AH, checks whether a handle refers to a file or a dev-
  783. ice on a Microsoft Networks workstation (local) or is redirected to a server
  784. (remote). BX must contain the file handle. DX returns the IOCtl bit field;
  785. bit 15 is set if the handle refers to a remote file or device.
  786. An application program should not test bit 15, because applications
  787. should not make distinctions between local and remote files (or devices).
  788. Programs should be written so that they will be independent of the loca-
  789. tion of a device that has been removed.
  790. If there is an error, the carry flag (CF) is set and the error code returns in
  791. AX:
  792. Code
  793. Meaning
  794. _ ________________________________________________________________
  795. 1 Network must be loaded to use this system call
  796. 6 Handle in BX not a valid, open handle
  797. 22
  798. _ _ | | _ _
  799. _ _ | | _ _
  800. _ ______________
  801. Macro Definition:
  802. ioctl_rhandle macro handle
  803. mov bx, handle
  804. mov al, 0AH
  805. mov ah, 44H
  806. int 21H
  807. endm
  808. Example:
  809. The following program checks whether handle 5 refers to a local or remote
  810. file or a device and displays the appropriate message.
  811. stdout equ 1
  812. ;
  813. message db "Handle 5 is "
  814. loc db "local."
  815. rem db "remote."
  816. crlf db 0DH,0AH
  817. ;
  818. begin: write_handle stdout,message,12;display message
  819. jc write_error ;routine not shown
  820. ioctl_rhandle 5 ;THIS FUNCTION
  821. jc ioctl_error ;routine not shown
  822. test dx,8000h ;bit 15 set?
  823. jnz not_loc ;yes, it's remote
  824. write_handle stdout,loc,6 ;see Function 40H
  825. jc write_error ;routine not shown
  826. jmp done
  827. not_loc: write_handle stdout,rem,7 ;see Function 40H
  828. jc write_error ;routine not shown
  829. done: write_handle stdout,crlf,2 ;see Function 40H
  830. jc write_error ;routine not shown
  831. 23
  832. _ _ | | _ _
  833. _ _ | | _ _
  834. _ ______________
  835. IOCtl Retry (Function 44H, Code 0BH)
  836. Call:
  837. AH = 44H
  838. AL = 0BH
  839. DX
  840. Number of retries
  841. CX
  842. Wait time
  843. Return:
  844. Carry set:
  845. AX
  846. 1 = Invalid function code
  847. Carry not set:
  848. No error
  849. Comments:
  850. Function 44H, Code 0BH, specifies how many times MS-DOS should retry
  851. a disk operation that fails because of a file-sharing violation. DX must
  852. contain the number of retries. CX controls the pause between retries.
  853. MS-DOS retries this type of disk operation three times, unless you use this
  854. system call to specify a different number. After the specified number of
  855. retries, MS-DOS issues Interrupt 24H (Critical-Error-Handler Address) for
  856. the requesting process.
  857. The effect of the delay parameter in CX is machine-dependent because it
  858. specifies how many times MS-DOS should execute an empty loop. The
  859. actual time varies, depending on the processor and clock speed. You can
  860. determine the effect on your machine by using debug. Set the number of
  861. retries to 1 and then time several values of CX.
  862. 24
  863. _ _ | | _ _
  864. _ _ | | _ _
  865. _ ______________
  866. If there is an error, the carry flag (CF) is set and the error code returns in
  867. AX:
  868. Code
  869. Meaning
  870. _ ________________________________________________________________
  871. 1 File sharing must be loaded to use this system call
  872. Macro Definition:
  873. ioctl_retry macro retries, wait
  874. mov dx, retries
  875. mov cx, wait
  876. mov al, 0BH
  877. mov ah, 44H
  878. int 21H
  879. endm
  880. Example:
  881. The following program sets the number of sharing retries to 10 and
  882. specifies a delay of 1000 between retries.
  883. begin: ioctl_retry 10,1000 ;THIS FUNCTION
  884. jc error ;routine not shown
  885. 25
  886. _ _ | | _ _
  887. _ _ | | _ _
  888. _ ______________
  889. Generic IOCtl (for Handles) (Function 44H,
  890. Code 0CH)
  891. Call:
  892. AH = 44H
  893. AL = 0CH
  894. BX
  895. Handle
  896. CH = 05H
  897. Category code (printer device)
  898. CL
  899. Function (minor) code
  900. DS:DX
  901. Pointer to data buffer
  902. Return:
  903. Carry set:
  904. AX
  905. 1 = Invalid function code
  906. Carry not set:
  907. No error
  908. Comments:
  909. This call loads and selects code pages for devices on a per-device basis. It
  910. also sets or gets the output iteration count for a printer that supports
  911. "PRINT 'TIL BUSY."
  912. The category code may be one of the following:
  913. Code
  914. Meaning
  915. _ ________________________________________________________________
  916. 00 Unknown device
  917. 01 Serial printer
  918. 03 Console device
  919. 05 Parallel printer
  920. The function code may be one of the following:
  921. Code
  922. Meaning
  923. _ ________________________________________________________________
  924. 26
  925. _ _ | | _ _
  926. _ _ | | _ _
  927. _ ______________
  928. 45H Sets iteration count for printer
  929. 4AH Select code page
  930. 4CH Start prepare list
  931. 4DH End prepare list
  932. 65H Gets iteration count for printer
  933. 6AH Query code page selected
  934. 6BH Query code page prepare list
  935. _ ________________________________________________________________
  936. Note
  937. DS:DX points to a word that contains the new value for the total
  938. number of output iterations performed before proceeding. Thus,
  939. DS:DX points to a word that contains the character iteration count for
  940. the "PRINT 'TIL BUSY" loop. This is the number of times the device
  941. driver will wait for the device to signal "ready" before acknowledging
  942. "Device busy."
  943. _ ________________________________________________________________
  944. Macro Definition:
  945. ioctl_handles macro handle,function,category,buffer
  946. mov ch,05H
  947. mov cl,function
  948. mov dx,offset buffer
  949. mov bx,handle
  950. mov ah,44H
  951. mov al,0CH
  952. int 21H
  953. endm
  954. 27
  955. _ _ | | _ _
  956. _ _ | | _ _
  957. _ ______________
  958. Generic IOCtl (for Devices) (Function 44H,
  959. Code 0DH)
  960. Call:
  961. AH = 44H
  962. AL = 0DH
  963. BL
  964. Drive number
  965. (0 = default, 1 = A, etc.)
  966. CH = 08H
  967. Category (major) code
  968. CL
  969. Function (minor) code
  970. DS:DX
  971. Pointer to parameter block -1
  972. Return:
  973. Carry set:
  974. AX
  975. 1 = Invalid function code
  976. 2 = Invalid drive
  977. Carry not set:
  978. No error
  979. Comments:
  980. The function code may be one of the following:
  981. Code
  982. Meaning
  983. _ ________________________________________________________________
  984. 40 Set device parameters
  985. 41 Write track on logical device
  986. 42 Format track on logical device
  987. 60 Get device parameters
  988. 61 Read track on logical device
  989. 62 Verify track on logical device
  990. _ ________________________________________________________________
  991. Note
  992. You must issue "Set Device Parameters" before you can read, write,
  993. format, or verify a logical drive.
  994. 28
  995. _ _ | | _ _
  996. _ _ | | _ _
  997. _ ______________
  998. _ ________________________________________________________________
  999. You should use the following procedure when you want to read, write, for-
  1000. mat, or verify a logical drive:
  1001. o Save drive parameters using "Get Device Parameters;"
  1002. o Set desired drive parameters using "Set Device Parameters;"
  1003. o Perform the I/O operation;
  1004. o Restore the original drive parameters using "Set Device Parame-
  1005. ters."
  1006. Set Device Parameters (Function 44 0DH, CL=40H)
  1007. When CL=40H, the parameter block has the following field format:
  1008. --------------------------------------
  1009. | BYTE Special Functions |
  1010. |------------------------------------|
  1011. | BYTE Device Type |
  1012. |------------------------------------|
  1013. | WORD Device Attributes |
  1014. |------------------------------------|
  1015. | WORD Number of Cylinders |
  1016. |------------------------------------|
  1017. | BYTE Media Type |
  1018. |------------------------------------|
  1019. | Device BPB |
  1020. |------------------------------------|
  1021. | Track Layout |
  1022. --------------------------------------
  1023. These fields have the following meanings:
  1024. Special Functions
  1025. Bit Value Meaning
  1026. _ ________________________________________________________________
  1027. 0 0
  1028. The Device BPB (BIOS Parameter Block) field contains
  1029. the new default BPB for this device. If a previous "Set
  1030. Parameter Device" call set this bit, Build BPB returns
  1031. the actual media BPB; otherwise, it returns the default
  1032. BPB for the device.
  1033. 1
  1034. All subsequent Build BPB requests return the device
  1035. BPB.
  1036. 1 0 Read all fields of the parameter block.
  1037. 29
  1038. _ _ | | _ _
  1039. _ _ | | _ _
  1040. _ ______________
  1041. 1
  1042. Ignore all fields of the parameter block except for the
  1043. Track Layout field.
  1044. 2 0
  1045. The sectors in the track may not all be the same size.
  1046. (You should not use this setting.)
  1047. 1
  1048. The sectors in the track are all the same size and the
  1049. sector numbers range between 1 and the total number of
  1050. sectors actually in the track. You should always set this
  1051. bit.
  1052. 3-7 0 These bits must be zero.
  1053. _ ________________________________________________________________
  1054. Device Type
  1055. This byte describes the physical device and is set by the device. When set,
  1056. it has the following meanings:
  1057. Value
  1058. Meaning
  1059. _ ________________________________________________________________
  1060. 0 320/360 KB
  1061. 1 1.2 MB
  1062. 2 720 KB
  1063. 3 8-inch, single-density
  1064. 4 8-inch, double-density
  1065. 5 Hard disk
  1066. 6 Tape drive
  1067. 7 Other
  1068. Device Attributes
  1069. Bit Value Meaning
  1070. _ ________________________________________________________________
  1071. 0 The media is removable.
  1072. 0 1 The media is not removable.
  1073. 1 0
  1074. Disk change-line is not
  1075. supported; (no door lock
  1076. support).
  1077. 1
  1078. Disk change-line is
  1079. supported; (door lock
  1080. support).
  1081. 2-7 0 These bits must be zero.
  1082. _ ________________________________________________________________
  1083. 30
  1084. _ _ | | _ _
  1085. _ _ | | _ _
  1086. _ ______________
  1087. Number of Cylinders
  1088. This field indicates the maximum number of cylinders that the physical
  1089. device can support. This information is set by the device.
  1090. Media Type
  1091. For drives that may contain different media, this field (which is device-
  1092. dependent) indicates which media the drive expects.
  1093. For a 1.2 MB disk, bit zero has the following meaning:
  1094. Bit Value Meaning
  1095. _ ________________________________________________________________
  1096. 0 0 Quad-density, 1.2 MB disk
  1097. 1 Double-density, 320/360 KB disk
  1098. _ ________________________________________________________________
  1099. The default media type is a quad-density 1.2 MB disk.
  1100. Device BPB
  1101. If bit 0 of the Special Functions field is clear, the BPB in this field is the
  1102. new default BPB for the device.
  1103. If bit 0 of the Special Functions field is set, the device driver returns the
  1104. BPB from this field for subsequent Build BPB requests.
  1105. Track Layout
  1106. This field contains a table of variable length for each logical device and
  1107. indicates the expected layout of the sectors on the media track. The field
  1108. has the following format:
  1109. ------------------------------------------------
  1110. | WORD Sector Count -- total # of sectors |
  1111. |----------------------------------------------|
  1112. | WORD Sector Number -- sector #1 |
  1113. |----------------------------------------------|
  1114. | WORD Sector Size -- sector #1 |
  1115. |----------------------------------------------|
  1116. | WORD Sector Number -- sector #2 |
  1117. |----------------------------------------------|
  1118. | WORD Sector Size -- sector #2 |
  1119. ------------------------------------------------
  1120. |
  1121. |
  1122. |
  1123. 31
  1124. _ _ | | _ _
  1125. _ _ | | _ _
  1126. _ ______________
  1127. ------------------------------------------------
  1128. | WORD Sector Number -- sector #n |
  1129. |----------------------------------------------|
  1130. | WORD Sector Size -- sector #n |
  1131. ------------------------------------------------
  1132. The Sector Count field indicates the total number of sectors. Each sector
  1133. number must be unique and in the range of 1 to sector count (n).
  1134. If bit 2 of the Special Functions field is set, all sector sizes must be the
  1135. same.
  1136. Get Device Parameters (Function 440DH, CL=60H)
  1137. When CL=60H, the parameter block has the same field layout as for
  1138. CL=40H. However, some of the fields have different meanings. These are
  1139. described as follows:
  1140. Special Functions
  1141. Bit Value Meaning
  1142. _ ________________________________________________________________
  1143. 0 0 Returns the default BPB for the device.
  1144. 1
  1145. Returns the BPB that Build BPB
  1146. would return.
  1147. 1-7 0 These bits must be zero.
  1148. _ ________________________________________________________________
  1149. Track Layout
  1150. The "Get Device Parameters" call does not use this field.
  1151. Read/Write Track on Logical Drive
  1152. (Function 440D, CL=61H/CL=41H)
  1153. To write to a track on a logical drive, set CL=41H. To read a track on a
  1154. logical drive, set CL=61H.
  1155. When CL=41H or 61H, the parameter block has the following format:
  1156. --------------------------------
  1157. | BYTE Special Functions |
  1158. |------------------------------|
  1159. | WORD Head |
  1160. |------------------------------|
  1161. | WORD Cylinder |
  1162. |------------------------------|
  1163. 32
  1164. _ _ | | _ _
  1165. _ _ | | _ _
  1166. _ ______________
  1167. | WORD First Sector |
  1168. |------------------------------|
  1169. | WORD Number of Sectors |
  1170. |------------------------------|
  1171. | DWORD Transfer Address |
  1172. --------------------------------
  1173. These fields are described as follows:
  1174. Special Functions This byte must be zero.
  1175. Head
  1176. This field contains the number of the head on which you perform the write
  1177. or read.
  1178. Cylinder
  1179. This field contains the number of the cylinder on which you perform the
  1180. write or read.
  1181. First Sector
  1182. This field contains the number of the first sector on which you perform the
  1183. write or read. Sectors are numbered starting with zero, so the fourth sec-
  1184. tor is numbered 3.
  1185. Number of Sectors
  1186. This field contains the total number of sectors.
  1187. Transfer Address
  1188. This field contains the address for storing the data to be written or the
  1189. data just read.
  1190. Format/Verify Track on Logical Drive
  1191. (Function 440DH, CL=42/CL=62)
  1192. To format and verify a track on a logical drive, set CL=42H. To verify a
  1193. track on a logical drive, set CL=62H.
  1194. 33
  1195. _ _ | | _ _
  1196. _ _ | | _ _
  1197. _ ______________
  1198. When CL=42H or 62H, the parameter block has the following format:
  1199. --------------------------------
  1200. | BYTE Special Functions |
  1201. |------------------------------|
  1202. | WORD Head |
  1203. |------------------------------|
  1204. | WORD Cylinder |
  1205. --------------------------------
  1206. These fields are described as follows:
  1207. Special Functions
  1208. This byte must be zero.
  1209. Head
  1210. This field contains the number of the head on which you perform the for-
  1211. mat or verify.
  1212. Cylinder
  1213. This field contains the number of the cylinder on which you perform the
  1214. format or verify.
  1215. 34
  1216. _ _ | | _ _
  1217. _ _ | | _ _
  1218. _ ______________
  1219. Get/Set IOCtl Drive Map (Function 44H,
  1220. Codes 0EH and 0FH)
  1221. Call:
  1222. AH = 44H
  1223. AL
  1224. OEH = Get logical drive map
  1225. OFH = Set logical drive map
  1226. BX
  1227. Drive number
  1228. (0 = default, 1 = A, etc.)
  1229. Return:
  1230. Carry set:
  1231. AX
  1232. 1 = Invalid function code
  1233. 5 = Invalid drive
  1234. Carry not set:
  1235. AL = Logical drive mapped onto physical drive
  1236. (= 0 if only one drive is
  1237. assigned to this physical drive)
  1238. Comments:
  1239. MS-DOS 3.3 supports the mapping of multiple logical drives onto a single
  1240. physical block device. Get IOCtl Drive Map lets you query the DOS about
  1241. which logical drive is currently mapped onto the corresponding physical
  1242. device. Set IOCtl Drive Map alters the device that is currently mapped
  1243. onto the physical device. These functions are only useful if there is more
  1244. than one logical block device mapped onto a single physical device.
  1245. A possible use for these functions is with applications that want to disable
  1246. the DOS prompt in order to place the correct floppy disk in the drive when
  1247. accessing the other logical drive.
  1248. To detect whether a logical device currently owns the physical device it is
  1249. mapped to, a program needs to check the value in AL after calling Func-
  1250. tion 440EH or 440FH (Get/Set IOCtl Drive Map).
  1251. 35
  1252. _ _ | | _ _
  1253. _ _ | | _ _
  1254. _ ______________
  1255. Duplicate File Handle (Function 45H)
  1256. Call:
  1257. AH = 45H
  1258. BX
  1259. Handle
  1260. Return:
  1261. Carry set:
  1262. AX
  1263. 4 = Too many open files
  1264. 6 = Invalid handle
  1265. Carry not set:
  1266. AX
  1267. New handle
  1268. Comments:
  1269. Function 45H creates an additional handle for a file. BX must contain the
  1270. handle of an open file.
  1271. MS-DOS returns the new handle in AX. The new handle refers to the same
  1272. file as the handle in BX, with the file pointer at the same position.
  1273. After you use this function request, moving the read/write pointer of
  1274. either handle also moves the pointer for the other handle. You usually use
  1275. this function request to redirect standard input (handle 0) and standard
  1276. output (handle 1). For a description of standard input, standard output,
  1277. and the advantages and techniques of manipulating them, see Software
  1278. Tools by Brian W. Kernighan and P.J. Plauger (Addison-Wesley Publish-
  1279. ing Co., 1976).
  1280. If there is an error, the carry flag (CF) is set and the error code returns in
  1281. AX:
  1282. Code
  1283. Meaning
  1284. _ ________________________________________________________________
  1285. 4 Too many open files (no handle available)
  1286. 6 Handle not open or is invalid
  1287. 36
  1288. _ _ | | _ _
  1289. _ _ | | _ _
  1290. _ ______________
  1291. Macro Definition:
  1292. xdup macro handle
  1293. mov bx,handle
  1294. mov ah,45H
  1295. int 21H
  1296. endm
  1297. Example:
  1298. The following program redirects standard output (handle 1) to a file
  1299. named dirfile, invokes a second copy of command.com to list the directory
  1300. (which writes the directory to dirfile), and then restores standard input to
  1301. handle 1.
  1302. pgm_file db "command.com",0
  1303. cmd_line db 9,"/c dir /w",0dH
  1304. parm_blk db 14 dup (0)
  1305. path db "dirfile",0
  1306. dir_file dw ? ; For handle
  1307. sav_stdout dw ? ; For handle
  1308. ;
  1309. begin: set_block last_inst ; See Function 4AH
  1310. jc error_setblk ; Routine not shown
  1311. create_handle path,0 ; See Function 3CH
  1312. jc error_create ; Routine not shown
  1313. mov dir_file,ax ; Save handle
  1314. xdup 1 ; THIS FUNCTION
  1315. jc error_xdup ; Routine not shown
  1316. mov sav_stdout,ax ; Save handle
  1317. xdup2 dir_file,1 ; See Function 46H
  1318. jc error_xdup2 ; Routine not shown
  1319. exec pgm_file,cmd_line,parm_blk ; See Function
  1320. 4BH
  1321. jc error_exec ; Routine not shown
  1322. xdup2 sav_stdout,1 ; See Function 46H
  1323. jc error_xdup2 ; Routine not shown
  1324. close_handle sav_stdout ; See Function 3EH
  1325. jc error_close ; Routine not shown
  1326. close_handle dir_file ; See Function 3EH
  1327. jc error_close ; Routine not shown
  1328. 37
  1329. _ _ | | _ _
  1330. _ _ | | _ _
  1331. _ ______________
  1332. Force Duplicate File Handle (Function 46H)
  1333. Call:
  1334. AH = 46H
  1335. BX
  1336. Handle
  1337. CX
  1338. Second handle
  1339. Return:
  1340. Carry set:
  1341. AX
  1342. 4 = Too many open files
  1343. 6 = Invalid handle
  1344. Carry not set:
  1345. No error
  1346. Comments:
  1347. Function 46H forces a specified handle to refer to the same file as a second
  1348. handle already associated with an open file. BX must contain the handle
  1349. of the open file; CX must contain the second handle.
  1350. On return, the handle in CX now refers to the same file at the same posi-
  1351. tion as the handle in BX. If the file referred to by the handle in CX was
  1352. open at the time of the call, this function closes it.
  1353. After you use this call, moving the read/write pointer of either handle also
  1354. moves the pointer for the other handle. Normally, you would use this
  1355. function request to redirect standard input (handle 0) and standard out-
  1356. put (handle 1). For a description of standard input, standard output, and
  1357. the advantages and techniques of manipulating them, see Software Tools
  1358. by Brian W. Kernighan and P.J. Plauger (Addison-Wesley Publishing Co.,
  1359. 1976).
  1360. 38
  1361. _ _ | | _ _
  1362. _ _ | | _ _
  1363. _ ______________
  1364. If there is an error, the carry flag (CF) is set and the error code returns in
  1365. AX:
  1366. Code
  1367. Meaning
  1368. _ ________________________________________________________________
  1369. 4 Too many open files (no handle available)
  1370. 6 Handle not open or is invalid
  1371. Macro Definition:
  1372. xdup2 macro handle1,handle2
  1373. mov bx,handle1
  1374. mov cx,handle2
  1375. mov ah,46H
  1376. int 21H
  1377. endm
  1378. Example:
  1379. The following program redirects standard output (handle 1) to a file
  1380. named dirfile, invokes a second copy of command.com to list the directory
  1381. (which writes the directory to dirfile), and then restores standard input to
  1382. handle 1.
  1383. pgm_file db "command.com",0
  1384. cmd_line db 9,"/c dir /w",0dH
  1385. parm_blk db 14 dup (0)
  1386. path db "dirfile",0
  1387. dir_file dw ? ; For handle
  1388. sav_stdout dw ? ; For handle
  1389. ;
  1390. begin: set_block last_inst ; See Function 4AH
  1391. jc error_setblk ; Routine not shown
  1392. create_handle path,0 ; See Function 3CH
  1393. jc error_create ; Routine not shown
  1394. mov dir_file,ax ; Save handle
  1395. xdup 1 ; See Function 45H
  1396. jc error_xdup ; Routine not shown
  1397. mov sav_stdout,ax ; Save handle
  1398. xdup2 dir_file,1 ;
  1399. jc error_xdup2 ; Routine not shown
  1400. exec pgm_file,cmd_line,parm_blk ; See Function
  1401. 4BH
  1402. jc error_exec ; Routine not shown
  1403. xdup2 sav_stdout,1 ; THIS FUNCTION
  1404. jc error_xdup2 ; Routine not shown
  1405. close_handle sav_stdout ; See Function 3EH
  1406. jc error_close ; Routine not shown
  1407. close_handle dir_file ; See Function 3EH
  1408. jc error_close ; Routine not shown
  1409. 39
  1410. _ _ | | _ _
  1411. _ _ | | _ _
  1412. _ ______________
  1413. Get Current Directory (Function 47H)
  1414. Call:
  1415. AH = 47H
  1416. DS:SI
  1417. Pointer to 64-byte memory area
  1418. DL
  1419. Drive number
  1420. (0 = default, 1 = A)
  1421. Return:
  1422. Carry set:
  1423. AX
  1424. 15 = Invalid drive number
  1425. Carry not set:
  1426. No error
  1427. Comments:
  1428. Function 47H returns the pathname of the current directory on a specified
  1429. drive. DL must contain a drive number (0=default, 1=A, etc.). SI must
  1430. contain the offset (from the segment address in DS) of a 64-byte memory
  1431. area.
  1432. MS-DOS places an ASCIZ string in the memory area, consisting of the path
  1433. (starting from the root directory) of the current directory for the drive
  1434. specified in DL. The string does not begin with a backslash and does not
  1435. include the drive letter.
  1436. If there is an error, the carry flag (CF) is set and the error code returns in
  1437. AX:
  1438. Code
  1439. Meaning
  1440. _ ________________________________________________________________
  1441. 15 Number in DL not a valid drive number
  1442. 40
  1443. _ _ | | _ _
  1444. _ _ | | _ _
  1445. _ ______________
  1446. Macro Definition:
  1447. get_dir macro drive,buffer
  1448. mov dl,drive
  1449. mov si,offset buffer
  1450. mov ah,47H
  1451. int 21H
  1452. endm
  1453. Example:
  1454. The following program displays the current directory that is on the disk in
  1455. drive B.
  1456. disk db "b:
  1457. buffer db 64 dup (?)
  1458. ;
  1459. begin: get_dir 2,buffer ;THIS FUNCTION
  1460. jc error_dir ;Routine not shown
  1461. display disk ;See Function 09H
  1462. display_asciz buffer ;See end of chapter
  1463. 41
  1464. _ _ | | _ _
  1465. _ _ | | _ _
  1466. _ ______________
  1467. Allocate Memory (Function 48H)
  1468. Call:
  1469. AH = 48H
  1470. BX
  1471. Paragraphs of memory requested
  1472. Return:
  1473. Carry set:
  1474. AX
  1475. 7 = Memory-control blocks damaged
  1476. 8 = Insufficient memory
  1477. BX
  1478. Paragraphs of memory available
  1479. Carry not set:
  1480. AX
  1481. Segment address of allocated memory
  1482. Comments:
  1483. Function 48H tries to allocate the specified amount of memory to the
  1484. current process. BX must contain the number of paragraphs of memory
  1485. (one paragraph is 16 bytes).
  1486. If sufficient memory is available to satisfy the request, AX returns the seg-
  1487. ment address of the allocated memory (the offset is 0). If sufficient memory
  1488. is not available, BX returns the number of paragraphs of memory in the
  1489. largest available block.
  1490. If there is an error, the carry flag (CF) is set and the error code returns in
  1491. AX:
  1492. Code
  1493. Meaning
  1494. _ ________________________________________________________________
  1495. 7 Memory-control blocks damaged (a user program changed memory
  1496. that doesn't belong to it)
  1497. 8 Not enough free memory to satisfy the request
  1498. 42
  1499. _ _ | | _ _
  1500. _ _ | | _ _
  1501. _ ______________
  1502. Macro Definition:
  1503. allocate_memory macro bytes
  1504. mov bx,bytes
  1505. mov cl,4
  1506. shr bx,cl
  1507. inc bx
  1508. mov ah,48H
  1509. int 21H
  1510. endm
  1511. Example:
  1512. The following program opens the file named textfile.asc, calculates its size
  1513. with Function 42H (Move File Pointer), allocates a block of memory the
  1514. size of the file, reads the file into the allocated memory block, and then
  1515. frees the allocated memory.
  1516. path db "textfile.asc",0
  1517. msg1 db "File loaded into allocated memory block.",
  1518. 0DH,0AH
  1519. msg2 db "Allocated memory now being freed
  1520. (deallocated).",0DH,0AH
  1521. handle dw ?
  1522. mem_seg dw ?
  1523. file_len dw ?
  1524. ;
  1525. begin: open_handle path,0
  1526. jc error_open ;Routine not shown
  1527. mov handle,ax ;Save handle
  1528. move_ptr handle,0,0,2 ;See Function 42H
  1529. jc error_move ;Routine not shown
  1530. mov file_len,ax ;Save file length
  1531. set_block last_inst ;See Function 4AH
  1532. jc error_setblk ;Routine not shown
  1533. allocate_memory file_len ;THIS FUNCTION
  1534. jc error_alloc ;Routine not shown
  1535. mov mem_seg,ax ;Save address of new memory
  1536. move_ptr handle,0,0,0 ;See Function 42H
  1537. jc error_move ;Routine not shown
  1538. push ds ;Save DS
  1539. mov ax,mem_seg ;Get segment of new memory
  1540. mov ds,ax ;Point DS at new memory
  1541. read_handle cs:handle,0,cs:file_len ;Read file into
  1542. ; new memory
  1543. pop ds ;Restore DS
  1544. jc error_read ;Routine not shown
  1545. ; (CODE TO PROCESS FILE GOES HERE)
  1546. write_handle stdout,msg1,42 ;See Function 40H
  1547. jc write_error ;Routine not shown
  1548. free_memory mem_seg ;See Function 49H
  1549. jc error_freemem ;Routine not shown
  1550. write_handle stdout,msg2,49 ;See Function 40H
  1551. 43
  1552. _ _ | | _ _
  1553. _ _ | | _ _
  1554. _ ______________
  1555. jc write_error ;Routine not shown
  1556. 44
  1557. _ _ | | _ _
  1558. _ _ | | _ _
  1559. _ ______________
  1560. Free Allocated Memory (Function 49H)
  1561. Call:
  1562. AH = 49H
  1563. ES
  1564. Segment address of memory to be
  1565. freed
  1566. Return:
  1567. Carry set:
  1568. AX
  1569. 7 = Memory-control blocks damaged
  1570. 9 = Incorrect segment
  1571. Carry not set:
  1572. No error
  1573. Comments:
  1574. Function 49H frees (makes available) a block of memory previously allo-
  1575. cated with Function 48H (Allocate Memory). ES must contain the segment
  1576. address of the memory block to be freed.
  1577. If there is an error, the carry flag (CF) is set and the error code returns in
  1578. AX:
  1579. Code
  1580. Meaning
  1581. _ ________________________________________________________________
  1582. 7 Memory-control blocks damaged (a user program changed memory
  1583. that didn't belong to it)
  1584. 9 Memory pointed to by ES was not allocated with Function 48H
  1585. Macro Definition:
  1586. free_memory macro seg_addr
  1587. mov ax,seg_addr
  1588. mov es,ax
  1589. mov ah,49H
  1590. int 21H
  1591. endm
  1592. 45
  1593. _ _ | | _ _
  1594. _ _ | | _ _
  1595. _ ______________
  1596. Example:
  1597. The following program opens a file named textfile.asc, calculates its size
  1598. with Function 42H (Move File Pointer), allocates a block of memory the
  1599. size of the file, reads the file into the allocated memory block, and then
  1600. frees the allocated memory.
  1601. path db "textfile.asc",0
  1602. msg1 db "File loaded into allocated memory block.",
  1603. 0DH,0AH
  1604. msg2 db "Allocated memory now being freed
  1605. (deallocated).",0DH,0AH
  1606. handle dw ?
  1607. mem_seg dw ?
  1608. file_len dw ?
  1609. ;
  1610. begin: open_handle path,0
  1611. jc error_open ;Routine not shown
  1612. mov handle,ax ;Save handle
  1613. move_ptr handle,0,0,2 ;See Function 42H
  1614. jc error_move ;Routine not shown
  1615. mov file_len,ax ;Save file length
  1616. set_block last_inst ;See Function 4AH
  1617. jc error_setblk ;Routine not shown
  1618. allocate_memory file_len ;See Function 48H
  1619. jc error_alloc ;Routine not shown
  1620. mov mem_seg,ax ;Save address of new memory
  1621. mov_ptr handle,0,0,0 ;See Function 42H
  1622. jc error_move ;Routine not shown
  1623. push ds ;Save DS
  1624. mov ax,mem_seg ;Get segment of new memory
  1625. mov ds,ax ;Point DS at new memory
  1626. read_handle handle,code,file_len ;Read file into
  1627. ; new memory
  1628. pop ds ;Restore DS
  1629. jc error_read ;Routine not shown
  1630. ; (CODE TO PROCESS FILE GOES HERE)
  1631. write_handle stdout,msg1,42 ;See Function 40H
  1632. jc write_error ;Routine not shown
  1633. free_memory mem_seg ;THIS FUNCTION
  1634. jc error_freemem ;Routine not shown
  1635. write_handle stdout,msg2,49 ;See Function 40H
  1636. jc write_error ;Routine not shown
  1637. 46
  1638. _ _ | | _ _
  1639. _ _ | | _ _
  1640. _ ______________
  1641. Set Block (Function 4AH)
  1642. Call:
  1643. AH = 4AH
  1644. BX
  1645. Paragraphs of memory
  1646. ES
  1647. Segment address of memory area
  1648. Return:
  1649. Carry set:
  1650. AX
  1651. 7 = Memory-control blocks damaged
  1652. 8 = Insufficient memory
  1653. 9 = Incorrect segment
  1654. BX
  1655. Paragraphs of memory available
  1656. Carry not set:
  1657. No error
  1658. Comments:
  1659. Function 4AH changes the size of a memory-allocation block. ES must
  1660. contain the segment address of the memory block. BX must contain the
  1661. new size of the memory block, in paragraphs (one paragraph is 16 bytes).
  1662. MS-DOS attempts to change the size of the memory block. If the call fails
  1663. on a request to increase memory, BX returns the maximum size (in para-
  1664. graphs) to which the block can be increased.
  1665. Since MS-DOS allocates all available memory to a .com program, you
  1666. would use this call most often to reduce the size of a program's initial
  1667. memory-allocation block.
  1668. 47
  1669. _ _ | | _ _
  1670. _ _ | | _ _
  1671. _ ______________
  1672. If there is an error, the carry flag (CF) is set and the error code returns in
  1673. AX:
  1674. Code
  1675. Meaning
  1676. _ ________________________________________________________________
  1677. 7 Memory-control blocks destroyed (a user program changed memory
  1678. that didn't belong to it)
  1679. 8 Not enough free memory to satisfy the request
  1680. 9 Wrong address in ES (the memory block it points to cannot be
  1681. modified with Set Block)
  1682. The following macro shrinks the initial memory-allocation block of a .com
  1683. program. It takes as a parameter the offset of the first byte following the
  1684. last instruction of a program (LAST_INST in the sample programs), uses
  1685. it to calculate the number of paragraphs in the program, and then adds 17
  1686. to the result: one to round up and 16 to set aside 256 bytes for a stack. It
  1687. then sets up SP and BP to point to this stack.
  1688. Macro Definition:
  1689. set_block macro last_byte
  1690. mov bx,offset last_byte
  1691. mov cl,4
  1692. shr bx,cl
  1693. add bx,17
  1694. mov ah,4AH
  1695. int 21H
  1696. mov ax,bx
  1697. shl ax,cl
  1698. dec ax
  1699. mov sp,ax
  1700. mov bp,sp
  1701. endm
  1702. Example:
  1703. The following program invokes a second copy of command.com and exe-
  1704. cutes a dir (directory) command.
  1705. pgm_file db "command.com",0
  1706. cmd_line db 9,"/c dir /w",0DH
  1707. parm_blk db 14 dup (?)
  1708. reg_save db 10 dup (?)
  1709. ;
  1710. begin: set_block last_inst ;THIS FUNCTION
  1711. exec pgm_file,cmd_line,parm_blk,0 ;See Function
  1712. ;4BH
  1713. 48
  1714. _ _ | | _ _
  1715. _ _ | | _ _
  1716. _ ______________
  1717. Load and Execute Program (Function 4BH,
  1718. Code 00H)
  1719. Call:
  1720. AH = 4BH
  1721. AL = 00H
  1722. DS:DX
  1723. Pointer to pathname
  1724. ES:BX
  1725. Pointer to parameter block
  1726. Return:
  1727. Carry set:
  1728. AX
  1729. 1 = Invalid function
  1730. 2 = File not found
  1731. 3 = Path not found
  1732. 4 = Too many open files
  1733. 5 = Access denied
  1734. 8 = Insufficient memory
  1735. 10 = Bad environment
  1736. 11 = Bad format
  1737. Carry not set:
  1738. No error
  1739. Comments:
  1740. Function 4BH, Code 00H, loads and executes a program. DX must contain
  1741. the offset (from the segment address in DS) of an ASCIZ string that specifies
  1742. the drive and pathname of an executable program file. BX must contain
  1743. the offset (from the segment address in ES) of a parameter block. AL must
  1744. contain 0.
  1745. There must be enough free memory for MS-DOS to load the program file.
  1746. MS-DOS allocates all available memory to a program when it loads it, so
  1747. you must free some memory with Function 4AH (Set Block) before using
  1748. this function request to load and execute another program. Unless you or
  1749. MS-DOS needs the memory for some other purpose, you should shrink the
  1750. memory to the minimum amount required by the current process before
  1751. issuing this request.
  1752. MS-DOS creates a Program Segment Prefix for the program being loaded
  1753. and sets the terminate and CONTROL-C addresses to the instruction that
  1754. immediately follows the call to Function 4BH in the invoking program.
  1755. 49
  1756. _ _ | | _ _
  1757. _ _ | | _ _
  1758. _ ______________
  1759. The parameter block consists of four addresses:
  1760. Table 0.2
  1761. Contents of the Parameter Block
  1762. _ _________________________________________________________________________
  1763. Offset Length
  1764. (Hex) (Bytes) Description
  1765. _ _________________________________________________________________________
  1766. 00 2 (word)
  1767. Segment address of the environment to be passed; 00H
  1768. means copy the parent program's environment.
  1769. 02 4 (dword)
  1770. Segment: Offset of command line to be placed at offset 80H
  1771. of the new Program Segment Prefix. Must be a correctly-
  1772. formed command line no longer than 128 bytes.
  1773. 06 4 (dword)
  1774. Segment: Offset of FCB to be placed at offset 5CH of the
  1775. new Program Segment Prefix (the Program Segment Prefix
  1776. is described in Chapter 4, "MS-DOS Control Blocks and
  1777. Work Areas")
  1778. 0A 4 (dword)
  1779. Segment: Offset of FCB to be placed at offset 6CH of the
  1780. new Program Segment Prefix
  1781. _ _________________________________________________________________________
  1782. All open files of a program are available to the newly loaded program, giv-
  1783. ing the parent program control over the definition of standard input, out-
  1784. put, auxiliary, and printer devices. For example, a program could write a
  1785. series of records to a file, open the file as standard input, open a second file
  1786. as standard output, and then use Function 4BH, Code 00H (Load and Exe-
  1787. cute Program) to load and execute a program that takes its input from
  1788. standard input, sorts records, and writes to standard output.
  1789. The loaded program also receives an environment, a series of ASCIZ strings
  1790. of the form parameter=value (for example, verify= on). The environment
  1791. must begin on a paragraph boundary, be less than 32K bytes long, and
  1792. end with a byte of 00H (that is, the final entry consists of an ASCIZ string
  1793. followed by two bytes of 00H). Following the last byte of zeros is a set of
  1794. initial arguments passed to a program containing a word count followed
  1795. by an ASCIZ string. If the call finds the file in the current directory, the
  1796. ASCIZ string contains the drive and pathname of the executable program as
  1797. passed to Function 4BH. If the call finds the file in the path, it concaten-
  1798. ates the filename with the path information. (A program may use this area
  1799. to determine whence it was loaded.) If the word-environment address is 0,
  1800. the loaded program either inherits a copy of the parent program's environ-
  1801. ment or receives a new environment built for it by the parent.
  1802. Place the segment address of the environment at offset 2CH of the new
  1803. Program Segment Prefix. To build an environment for the loaded pro-
  1804. gram, put it on a paragraph boundary and place the segment address of
  1805. the environment in the first word of the parameter block. To pass a copy
  1806. of the parent's environment to the loaded program, put 00H in the first
  1807. 50
  1808. _ _ | | _ _
  1809. _ _ | | _ _
  1810. _ ______________
  1811. word of the parameter block.
  1812. If there is an error, the carry flag (CF) is set and the error code returns in
  1813. AX:
  1814. Code
  1815. Meaning
  1816. _ ________________________________________________________________
  1817. 1 AL not 0 or 3
  1818. 2 Program file not found
  1819. 3 Path invalid or not found
  1820. 4 Too many open files (no handle available)
  1821. 5 Directory full, a directory with the same name exists, or a file with
  1822. the same name exists
  1823. 8 Not enough memory to load the program
  1824. 10 Environment appears longer than 32K
  1825. 11 Program file is an .exe file that contains internally inconsistent
  1826. information
  1827. Executing Another Copy of Command.com
  1828. Since command.com builds pathnames, searches command paths for pro-
  1829. gram files, and relocates .exe files, the simplest way to load and execute
  1830. another program is to load and execute an additional copy of
  1831. command.com, passing it a command line that includes the /c switch,
  1832. which invokes the .com or .exe file.
  1833. This action requires 17K bytes of available memory, so a program that
  1834. does it should be sure to shrink its initial memory-allocation block with
  1835. Function 4AH (Set Block). The format of a command line that contains
  1836. the /c switch:
  1837. length,/c command,0DH
  1838. Length is the length of the command line, counting the length byte but not
  1839. the ending carriage-return (0DH).
  1840. Command is any valid MS-DOS command.
  1841. 0DH is a carriage-return character.
  1842. If a program executes another program directly\(emnaming it as the program
  1843. file to Function 4BH instead of command.com\(emit must perform all the
  1844. processing normally done by command.com.
  1845. 51
  1846. _ _ | | _ _
  1847. _ _ | | _ _
  1848. _ ______________
  1849. Macro Definition:
  1850. exec macro path,command,parms
  1851. mov dx,offset path
  1852. mov bx,offset parms
  1853. mov word ptr parms[02H],offset command
  1854. mov word ptr parms[04H],cs
  1855. mov word ptr parms[06H],5CH
  1856. mov word ptr parms[08H],es
  1857. mov word ptr parms[0AH],6CH
  1858. mov word ptr parms[0CH],es
  1859. mov al,0
  1860. mov ah,4BH
  1861. int 21H
  1862. endm
  1863. Example:
  1864. The following program invokes a second copy of command.com and exe-
  1865. cutes a dir (directory) command by using the /w (wide) switch:
  1866. pgm_file db "command.com",0
  1867. cmd_line db 9,"/c dir /w",0DH
  1868. parm_blk db 14 dup (?)
  1869. reg_save db 10 dup (?)
  1870. ;
  1871. begin:
  1872. set_block last_inst ;See Function 4AH
  1873. exec pgm_file,cmd_line,parm_blk,0 ;THIS FUNCTION
  1874. 52
  1875. _ _ | | _ _
  1876. _ _ | | _ _
  1877. _ ______________
  1878. Load Overlay (Function 4BH, Code 03H)
  1879. Call:
  1880. AH = 4BH
  1881. AL = 03H
  1882. DS:DX
  1883. Pointer to pathname
  1884. ES:BX
  1885. Pointer to parameter block
  1886. Return:
  1887. Carry set:
  1888. AX
  1889. 1 = Invalid function
  1890. 2 = File not found
  1891. 3 = Path not found
  1892. 4 = Too many open files
  1893. 5 = Access denied
  1894. 8 = Insufficient memory
  1895. 10 = Bad environment
  1896. Carry not set:
  1897. No error
  1898. Comments:
  1899. Function 4BH, Code 03H, loads a program segment (overlay). DX must
  1900. contain the offset (from the segment address in DS) of an ASCIZ string that
  1901. specifies the drive and pathname of the program file. BX must contain the
  1902. offset (from the segment address in ES) of a parameter block. AL must
  1903. contain 3.
  1904. MS-DOS assumes that since the invoking program is loading into its own
  1905. address space, it requires no free memory. This call does not create a Pro-
  1906. gram Segment Prefix.
  1907. 53
  1908. _ _ | | _ _
  1909. _ _ | | _ _
  1910. _ ______________
  1911. The parameter block is four bytes long:
  1912. Table 0.3
  1913. Contents of the Parameter Block
  1914. _ _________________________________________________________________________
  1915. Offset Length
  1916. (Hex) (Bytes) Description
  1917. _ _________________________________________________________________________
  1918. 00 2 (word) Segment address where program is to be loaded
  1919. 02 2 (word)
  1920. Relocation factor; usually the same as the first word of the
  1921. parameter block (for a description of an .exe file and of
  1922. relocation, see Chapter 6, ".Exe File Structure and
  1923. Loading.")
  1924. _ _________________________________________________________________________
  1925. If there is an error, the carry flag (CF) is set and the error code returns in
  1926. AX:
  1927. Code
  1928. Meaning
  1929. _ ________________________________________________________________
  1930. 1 AL not 00H or 03H
  1931. 2 Program file not found
  1932. 3 Path invalid or not found
  1933. 4 Too many open files (no handle available)
  1934. 5 Directory is full, a directory with the same name exists, or a file
  1935. with the same name exists
  1936. 8 Not enough memory to load the program
  1937. 10 Environment appears longer than 32K
  1938. Macro Definition:
  1939. exec_ovl macro path,parms,seg_addr
  1940. mov dx,offset path
  1941. mov bx,offset parms
  1942. mov parms,seg_addr
  1943. mov parms[02H],seg_addr
  1944. mov al,3
  1945. mov ah,4BH
  1946. int 21H
  1947. endm
  1948. 54
  1949. _ _ | | _ _
  1950. _ _ | | _ _
  1951. _ ______________
  1952. Example:
  1953. The following program opens a file named textfile.asc, redirects standard
  1954. input to that file, loads more.com as an overlay, and calls an overlay
  1955. named bit.com, which reads textfile.asc as standard input. The overlay
  1956. must establish its own addressability and end with a FAR return.
  1957. stdin equ 0
  1958. ;
  1959. file db "TEXTFILE.ASC",0
  1960. cmd_file db "\bit.com",0
  1961. parm_blk dw 4 dup (?)
  1962. overlay label dword
  1963. dw 0
  1964. handle dw ?
  1965. new_mem dw ?
  1966. ;
  1967. begin: set_block last_inst ;see Function 4AH
  1968. jc setblock_error ;routine not shown
  1969. allocate_memory 2000 ;see Function 48H
  1970. jc allocate_error ;routine not shown
  1971. mov new_mem,ax ;save seg of memory
  1972. open_handle file,0 ;see Function 3DH
  1973. jc open_error ;routine not shown
  1974. mov handle,ax ;save handle
  1975. xdup2 handle,stdin ;see Function 45H
  1976. jc dup2_error ;routine not shown
  1977. close_handle handle ;see Function 3EH
  1978. jc close_error ;routine not shown
  1979. mov ax,new_mem ;addr of new memory
  1980. exec_ovl cmd_file,parm_blk,ax ;THIS FUNCTION
  1981. jc exec_error ;routine not shown
  1982. call overlay ;call the overlay
  1983. free_memory new_mem ;see Function 49H
  1984. jc free_error ;routine not shown
  1985. ;
  1986. 55
  1987. _ _ | | _ _
  1988. _ _ | | _ _
  1989. _ ______________
  1990. End Process (Function 4CH)
  1991. Call:
  1992. AH = 4CH
  1993. AL
  1994. Return code
  1995. Return:
  1996. None
  1997. Comments:
  1998. Function 4CH terminates a process and returns to MS-DOS. AL contains a
  1999. return code that can be retrieved by the parent process with Function 4DH
  2000. (Get Return Code of Child Process) or the if command, using errorlevel.
  2001. MS-DOS closes all open handles, ends the current process, and returns
  2002. control to the invoking process.
  2003. This function request doesn't require CS to contain the segment address of
  2004. the Program Segment Prefix. You should use it to end a program (rather
  2005. than Interrupt 20H or a jump to location 0) unless your program must be
  2006. compatible with MS-DOS versions before 2.0.
  2007. _ ________________________________________________________________
  2008. Note
  2009. If you use file sharing, you must remove all locks issued by this process
  2010. or the DOS will be in an uncertain state.
  2011. _ ________________________________________________________________
  2012. 56
  2013. _ _ | | _ _
  2014. _ _ | | _ _
  2015. _ ______________
  2016. Macro Definition:
  2017. end_process macro return_code
  2018. mov al,return_code
  2019. mov ah,4CH
  2020. int 21H
  2021. endm
  2022. Example:
  2023. The following program displays a message and returns to MS-DOS with a
  2024. return code of 8. It uses only the opening portion of the sample program
  2025. skeleton shown at the beginning of this chapter.
  2026. message db "Displayed by FUNC_4CH example",0DH,0AH,"$"
  2027. ;
  2028. begin: display message ;See Function 09H
  2029. end_process 8 ;THIS FUNCTION
  2030. code ends
  2031. end code
  2032. 57
  2033. _ _ | | _ _
  2034. _ _ | | _ _
  2035. _ ______________
  2036. Get Return Code of Child Process (Function
  2037. 4DH)
  2038. Call:
  2039. AH = 4DH
  2040. Return:
  2041. AX
  2042. Return code
  2043. Comments:
  2044. Function 4DH retrieves the return code specified when a child process ter-
  2045. minates via either Function 31H (Keep Process) or Function 4CH (End
  2046. Process). The code returns in AL. AH returns a code that specifies why the
  2047. program ended:
  2048. Code
  2049. Meaning
  2050. _ ________________________________________________________________
  2051. 0 Normal termination
  2052. 1 Terminated by CONTROL-C
  2053. 2 Critical device error
  2054. 3 Function 31H (Keep Process)
  2055. This call can retrieve the exit code only once.
  2056. Macro Definition:
  2057. ret_code macro
  2058. mov ah,4DH
  2059. int 21H
  2060. endm
  2061. 58
  2062. _ _ | | _ _
  2063. _ _ | | _ _
  2064. _ ______________
  2065. Example:
  2066. No example is included for this function request, because the meaning of a
  2067. return code varies.
  2068. 59
  2069. _ _ | | _ _
  2070. _ _ | | _ _
  2071. _ ______________
  2072. Find First File (Function 4EH)
  2073. Call:
  2074. AH = 4EH
  2075. DS:DX
  2076. Pointer to pathname
  2077. CX
  2078. Attributes to match
  2079. Return:
  2080. Carry set:
  2081. AX
  2082. 2 = File not found
  2083. 3 = Path not found
  2084. 18 = No more files
  2085. Carry not set:
  2086. No error
  2087. Comments:
  2088. Function 4EH searches the current or specified directory for the first entry
  2089. that matches the specified pathname. DX must contain the offset (from the
  2090. segment address in DS) of an ASCIZ string that specifies the pathname,
  2091. which can contain wildcard characters. CX must contain the attribute to
  2092. be used in searching for the file, as described in Section 1.5.5, "File Attri-
  2093. butes," earlier in this chapter.
  2094. If the attribute field is hidden file, system file, or subdirectory entry (02H,
  2095. 04H, or 10H), or any combination of these values, all normal file entries
  2096. are also searched. To search all directory entries except the volume label,
  2097. set the attribute byte to 16H (hidden file, system file, and directory entry).
  2098. 60
  2099. _ _ | | _ _
  2100. _ _ | | _ _
  2101. _ ______________
  2102. If this function finds a directory entry that matches the name and attri-
  2103. bute, it fills the current DTA as follows:
  2104. Table 0.4
  2105. _ _________________________________________________________________________
  2106. Offset Length Description
  2107. _ _________________________________________________________________________
  2108. 00H 21 Reserved for subsequent Function 4FH (Find Next File)
  2109. 15H 1 Attribute found
  2110. 16H 2 Time file was last written
  2111. 18H 2 Date file was last written
  2112. 1AH 2 Low word of file size
  2113. 1CH 2 High word of file size
  2114. 1EH 13
  2115. Name and extension of the file, followed by 00H. All blanks
  2116. are removed; if there is an extension, it is preceded by a
  2117. period. (Volume labels include a period after the eighth
  2118. character.)
  2119. _ _________________________________________________________________________
  2120. If there is an error, the carry flag (CF) is set and the error code returns in
  2121. AX:
  2122. Code
  2123. Meaning
  2124. _ ________________________________________________________________
  2125. 2 Specified file invalid or doesn't exist
  2126. 3 Specified path invalid or doesn't exist
  2127. 18 No matching directory entry found
  2128. Macro Definition:
  2129. find_first_file macro path,attrib
  2130. mov dx,offset path
  2131. mov cx,attrib
  2132. mov ah,4EH
  2133. int 21H
  2134. endm
  2135. 61
  2136. _ _ | | _ _
  2137. _ _ | | _ _
  2138. _ ______________
  2139. Example:
  2140. The following program displays a message that specifies whether a file
  2141. named report.asm exists in the current directory on the disk in drive B.
  2142. yes db "File exists.",0DH,0AH,"$"
  2143. no db "File does not exist.",0DH,0AH,"$"
  2144. path db "b:report.asm",0
  2145. buffer db 43 dup (?)
  2146. ;
  2147. begin: set_dta buffer ;See Function 1AH
  2148. find_first_file path,0 ;THIS FUNCTION
  2149. jc error_findfirst ;Routine not shown
  2150. cmp al,12H ;File found?
  2151. je not_there ;No
  2152. display yes ;See Function 09H
  2153. jmp return ;All done
  2154. not_there: display no ;See Function 09H
  2155. 62
  2156. _ _ | | _ _
  2157. _ _ | | _ _
  2158. _ ______________
  2159. Find Next File (Function 4FH)
  2160. Call:
  2161. AH = 4FH
  2162. Return:
  2163. Carry set:
  2164. AX
  2165. 18 = No more files
  2166. Carry not set:
  2167. No error
  2168. Comments:
  2169. Function 4FH searches for the next directory entry that matches the name
  2170. and attributes specified in a previous Function 4EH (Find First File). The
  2171. current DTA must contain the information filled in by Function 4EH (Find
  2172. First File).
  2173. If the function finds a matching entry, it fills the current DTA just as it
  2174. did for Find First File (see Function 4EH (Find First File)).
  2175. If there is an error, the carry flag (CF) is set and the error code returns in
  2176. AX:
  2177. Code
  2178. Meaning
  2179. _ ________________________________________________________________
  2180. 2 Specified path invalid or doesn't exist
  2181. 18 No matching directory entry found
  2182. 63
  2183. _ _ | | _ _
  2184. _ _ | | _ _
  2185. _ ______________
  2186. Macro Definition:
  2187. find_next_file macro
  2188. mov ah,4FH
  2189. int 21H
  2190. endm
  2191. Example:
  2192. The following program displays the number of files contained in the
  2193. current directory on the disk in drive B.
  2194. message db "No files",0DH,0AH,"$"
  2195. files dw ?
  2196. path db "b:*.*",0
  2197. buffer db 43 dup (?)
  2198. ;
  2199. begin: set_dta buffer ;See Function 1AH
  2200. find_first_file path,0 ;See Function 4EH
  2201. jc error_findfirst ;Routine not shown
  2202. cmp al,12H ;Directory empty?
  2203. je all_done ;Yes, go home
  2204. inc files ;No, bump file counter
  2205. search_dir: find_next_file ;THIS FUNCTION
  2206. jc error_findnext ;Routine not shown
  2207. cmp al,12H ;Any more entries?
  2208. je done ;No, go home
  2209. inc files ;Yes, bump file counter
  2210. jmp search_dir ;And check again
  2211. done: convert files,10,message ;See end of chapter
  2212. all_done: display message ;See Function 09H
  2213. 64
  2214. _ _ | | _ _