home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l350 / 3.ddi / EXAMPLES / ASM / DOSDEMO.ASM next >
Encoding:
Assembly Source File  |  1988-05-29  |  10.7 KB  |  536 lines

  1. ;************************************************************************/
  2. ;*    Copyright (C) 1986-1988 Phar Lap Software, Inc.            */
  3. ;*    Unpublished - rights reserved under the Copyright Laws of the    */
  4. ;*    United States.  Use, duplication, or disclosure by the         */
  5. ;*    Government is subject to restrictions as set forth in         */
  6. ;*    subparagraph (c)(1)(ii) of the Rights in Technical Data and     */
  7. ;*    Computer Software clause at 252.227-7013.            */
  8. ;*    Phar Lap Software, Inc., 60 Aberdeen Ave., Cambridge, MA 02138    */
  9. ;************************************************************************/
  10. ;
  11. ; This program illustrates how MS-DOS system calls are made from
  12. ; 386 protected mode.
  13. ;
  14.  
  15. ;
  16. ; Constants
  17. ;
  18. nl    equ    00ah
  19.  
  20. _data    segment    para public use32 'data'
  21.  
  22.  
  23. ;
  24. ;    Messages
  25. ;
  26.  
  27. hellomsg db    'DOS demo program for 386 protected mode',0dh,0ah,0dh,0ah,'$'
  28.  
  29. strikemsg db    'Strike any key --> ',0
  30.  
  31. keymsg    db    nl,'Code of the key which you hit - ',0
  32.  
  33. nlmsg    db    nl,0
  34.  
  35. linputmsg db    'Type in a line of text --> ',0
  36.  
  37. pbmsg     db    nl,'DOS read               --> ',0
  38.  
  39. cdiskmsg db    'Current disk -- ',0
  40.  
  41. crtmsg    db    'Now creating the file "TEST.DAT"',nl,0
  42.  
  43. wrtmsg    db    'Now writing data to the file "TEST.DAT"',nl,0
  44.  
  45. closemsg db    'Now closing the file "TEST.DAT"',nl,0
  46.  
  47. openmsg    db    'Now reopening the file "TEST.DAT"',nl,0
  48.  
  49. readmsg    db    'Data from the file "TEST.DAT" -- ',0
  50.  
  51. seekmsg    db    'Data starting from byte position 5 of "TEST.DAT" -- ',0
  52.  
  53. ffmsg    db    'Name of the first file of the current directory -- ',0
  54.  
  55. sfmsg    db    'Name of the second file of the current directory -- ',0
  56.  
  57. renmsg    db    'Now renaming the file "TEST.DAT" to "NEWTEST.DAT"',nl,0
  58.  
  59. delmsg    db    'Now deleting the file "NEWTEST.DAT"',nl,0
  60.  
  61. cdirmsg db    'Current directory -- ',0
  62.  
  63. setdtamsg db    'Setting disk transfer address to -- ',0
  64.  
  65. dtamsg    db    'Disk transfer address is -- ',0
  66.  
  67. ferrmsg    db    nl,'ABORT -- File system error #',0
  68.  
  69. ;
  70. ;    The input buffer
  71. ;
  72.  
  73. inbuff    db    80,0,81 dup (?)
  74.  
  75.  
  76. ;    
  77. ;    File variables
  78. ;
  79.  
  80. flname    db    'TEST.DAT',0        ; Name of the test file
  81. nflname    db    'NEWTEST.DAT',0        ; New file name
  82. dsrcstr    db    '*.*',0            ; Directory search string
  83. flhand    dw    ?            ; The file handle
  84.  
  85. fldata    db    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'    ; The file data
  86. flend    label    byte                ;
  87.  
  88. fdsize    equ    flend-fldata        ; The size of the data
  89.  
  90. _data    ends
  91.  
  92. _stack    segment    byte stack use32 'stack'
  93.  
  94.     db    8192 dup (?)
  95.  
  96. _stack    ends
  97.  
  98.     assume    cs:_text,ds:_data
  99.  
  100. _text    segment    para public use32 'code'
  101.  
  102.     public    _start_            ;
  103. _start_    proc    near            ;
  104.  
  105. ;*********************************************************
  106. ; Test print string (function 09)
  107. ;*********************************************************
  108.     lea    edx,hellomsg        ; Output the hello message.
  109.     mov    ah,09h
  110.     int    21h
  111.  
  112. ;*********************************************************
  113. ; Test keyboard and console I/O
  114. ;*********************************************************
  115. ;
  116. ; Test read keyboard (function 08) and display character (function 02, in
  117. ; subroutine printmsg)
  118. ;
  119.     lea    ebx,strikemsg        ; Ask the user to strike a key.
  120.     call    printmsg        ; 
  121.     mov    ah,8h            ;
  122.     int    21h            ;
  123.  
  124.     push    ax            ; Output the hex code of the key
  125.     lea    ebx,keymsg        ;    which was struck.
  126.     call    printmsg        ;
  127.     pop    ax            ;
  128.     call    hbout            ;
  129.     lea    ebx,nlmsg        ;
  130.     call    printmsg        ;
  131.  
  132. ;
  133. ; Test buffered keyboard input (function 0A)
  134. ;
  135.     lea    ebx,linputmsg        ; Read in a line of input.
  136.     call    printmsg        ;
  137.     lea    edx,inbuff        ;
  138.     mov    ah,0ah            ;
  139.     int    21h            ;
  140.  
  141.     lea    ebx,pbmsg        ; Echo the line back to the screen.
  142.     call    printmsg        ;
  143.     xor    ebx,ebx            ;
  144.     mov    bl,inbuff+1        ;
  145.     add    ebx,offset inbuff+2    ;
  146.     mov    byte ptr [ebx],00ah    ;
  147.     mov    byte ptr 1[ebx],0    ;
  148.     lea    ebx,inbuff+2        ;
  149.     call    printmsg        ;
  150.  
  151. ;*********************************************************
  152. ; Test disk I/O that doesn't use FCBs
  153. ;*********************************************************
  154. ;
  155. ; Test get current disk (function 19)
  156. ;
  157.     lea    ebx,cdiskmsg        ; Output the name of the current disk.
  158.     call    printmsg        ;
  159.     mov    ah,19h            ;
  160.     int    21h            ;
  161.     add    al,'A'            ;
  162.     mov    dl,al            ;
  163.     mov    ah,2h            ;
  164.     int    21H            ;
  165.     lea    ebx,nlmsg        ;
  166.     call    printmsg        ;
  167.  
  168. ;
  169. ; Test create file (function 3C)
  170. ;
  171.     lea    ebx,crtmsg        ; Create a test file.
  172.     call    printmsg        ;
  173.     lea    edx,flname        ;
  174.     mov    cx,0            ;
  175.     mov    ah,03ch            ;
  176.     int    21h            ;
  177.     call    flercheck        ;
  178.     mov    flhand,ax        ;
  179.  
  180. ;
  181. ; Test write to file/device (function 40)
  182. ;
  183.     lea    ebx,wrtmsg        ; Write some data in the file.
  184.     call    printmsg        ;
  185.     mov    bx,flhand        ;
  186.     mov    ecx,fdsize        ;
  187.     lea    edx,fldata        ;
  188.     mov    ah,040h            ;
  189.     int    21h            ;
  190.     call    flercheck        ;
  191.  
  192. ;
  193. ; Test close file (function 3E)
  194. ;
  195.     lea    ebx,closemsg        ; Close the file.
  196.     call    printmsg        ;
  197.     mov    bx,flhand        ;
  198.     mov    ah,03eh            ;
  199.     int    21h            ;
  200.     call    flercheck        ;
  201.  
  202. ;
  203. ; Test open file (function 3D)
  204. ;
  205.     lea    ebx,openmsg        ; Re-open the file.
  206.     call    printmsg        ;
  207.     lea    edx,flname        ;
  208.     mov    al,2            ;
  209.     mov    ah,03dh            ;
  210.     int    21h            ;
  211.     call    flercheck        ;
  212.     mov    flhand,ax        ;
  213.  
  214. ;
  215. ; Test read from file (function 3F)
  216. ;
  217.     lea    ebx,readmsg        ; Read back the data which we
  218.     call    printmsg        ;    wrote to the file.
  219.     mov    bx,flhand        ;
  220.     xor    ecx,ecx            ;
  221.     mov    cl,inbuff        ;
  222.     lea    edx,inbuff+2        ;
  223.     mov    ah,03fh            ;
  224.     int    21h            ;
  225.     call    flercheck        ;
  226.  
  227.     mov    ebx,eax            ; Print out the data which we read
  228.     add    ebx,offset inbuff+2    ;
  229.     mov    byte ptr [ebx],0    ;    
  230.     lea    ebx,inbuff+2        ;
  231.     call    printmsg        ;
  232.     lea    ebx,nlmsg        ;
  233.     call    printmsg        ;
  234.  
  235. ;
  236. ; Test move file pointer (function 42)
  237. ;
  238.     lea    ebx,seekmsg        ; Seek to byte 5 of the file.
  239.     call    printmsg        ;
  240.     mov    al,0            ;
  241.     mov    bx,flhand        ;
  242.     mov    cx,0            ;
  243.     mov    dx,5            ;
  244.     mov    ah,42h            ;
  245.     int    21h            ;
  246.     call    flercheck        ;
  247.  
  248.     mov    bx,flhand        ; Read from the file again.
  249.     xor    ecx,ecx            ;
  250.     mov    cl,inbuff        ;
  251.     lea    edx,inbuff+2        ;
  252.     mov    ah,03fh            ;
  253.     int    21h            ;
  254.     call    flercheck        ;
  255.  
  256.     mov    ebx,eax            ; Print out the data which we read.
  257.     add    ebx,offset inbuff+2    ;
  258.     mov    byte ptr [ebx],0    ;    
  259.     lea    ebx,inbuff+2        ;
  260.     call    printmsg        ;
  261.     lea    ebx,nlmsg        ;
  262.     call    printmsg        ;
  263.  
  264. ;
  265. ; Test rename file (function 56)
  266. ;
  267.     lea    ebx,closemsg        ; Close the file.
  268.     call    printmsg        ;
  269.     mov    bx,flhand        ;
  270.     mov    ah,03eh            ;
  271.     int    21h            ;
  272.     call    flercheck        ;
  273.  
  274.     lea    ebx,renmsg        ; Rename the file.
  275.     call    printmsg        ;
  276.     lea    edx,flname        ;
  277.     lea    edi,nflname        ;
  278.     mov    ah,56h            ;
  279.     int    21h            ;
  280.     call    flercheck        ;
  281.  
  282. ;
  283. ; Test delete file (function 41)
  284. ;
  285.     lea    ebx,delmsg        ; Delete the file.
  286.     call    printmsg        ;
  287.     lea    edx,nflname        ;
  288.     mov    ah,41h            ;
  289.     int    21h            ;
  290.     call    flercheck        ;
  291.  
  292. ;
  293. ; Test get current directory (function 47)
  294. ;
  295.     lea    ebx,cdirmsg        ; Print out the name of the current
  296.     call    printmsg        ;    directory.
  297.     mov    dl,0            ;
  298.     lea    esi,inbuff+2        ;
  299.     mov    ah,47h            ;
  300.     int    21h            ;
  301.     call    flercheck
  302.     lea    ebx,inbuff+2        ;
  303.     call    printmsg        ;
  304.     lea    ebx,nlmsg        ;
  305.     call    printmsg        ;
  306.  
  307. ;
  308. ; Test get disk transfer address (function 2F)
  309. ;
  310.     mov    ah,2Fh            ; get DTA
  311.     int    21h
  312.  
  313.     push    ebx            ; save returned ES:EBX
  314.     push    es            ;
  315.     lea    ebx,dtamsg        ; Output the transfer address
  316.     call    printmsg        ;
  317.     pop    eax            ;
  318.     call    hwout            ;
  319.     mov    dl,':'            ;
  320.     mov    ah,02h            ;
  321.     int    21h            ;
  322.     pop    eax            ;
  323.     call    hlout            ;
  324.     lea    ebx,nlmsg        ;
  325.     call    printmsg        ;
  326.  
  327. ;
  328. ; Test set disk transfer address (function 1A)
  329. ;
  330.     lea    ebx,setdtamsg        ; Output the transfer address
  331.     call    printmsg        ;    we will set the DTA to
  332.     mov    ax,ds            ;
  333.     call    hwout            ;
  334.     mov    dl,':'            ;
  335.     mov    ah,02h            ;
  336.     int    21h            ;
  337.     lea    eax,inbuff+2        ;
  338.     call    hlout            ;
  339.     lea    ebx,nlmsg        ;
  340.     call    printmsg        ;
  341.     lea    edx,inbuff+2        ; Set the DTA.
  342.     mov    ah,1ah            ;
  343.     int    21h            ;
  344.  
  345.     mov    ah,2Fh            ; get DTA
  346.     int    21h            ;
  347.     push    ebx            ; save returned ES:BX
  348.     push    es            ;
  349.     lea    ebx,dtamsg        ; Output the transfer address
  350.     call    printmsg        ;
  351.     pop    eax            ;
  352.     call    hwout            ;
  353.     mov    dl,':'            ;
  354.     mov    ah,02h            ;
  355.     int    21h            ;
  356.     pop    eax            ;
  357.     call    hlout            ;
  358.     lea    ebx,nlmsg        ;
  359.     call    printmsg        ;
  360.  
  361. ;
  362. ; Test find matching file (4E)
  363. ;
  364.     lea    ebx,ffmsg        ; Print out the name of the first
  365.     call    printmsg        ;    file in the current directory.
  366.     lea    edx,dsrcstr        ;
  367.     mov    ecx,0            ;
  368.     mov    ah,4eh            ;
  369.     int    21h            ;
  370.     call    flercheck        ;
  371.     lea    ebx,inbuff+2+30        ;
  372.     call    printmsg        ;
  373.     lea    ebx,nlmsg        ;
  374.     call    printmsg        ;
  375.  
  376. ;
  377. ; Test find next matching file (function 4F)
  378. ;
  379.     lea    ebx,sfmsg        ; Print out the name of the second 
  380.     call    printmsg        ;    file in the directory.
  381.     mov    ah,04fh            ;
  382.     int    21h            ;
  383.     call    flercheck        ;
  384.     lea    ebx,inbuff+2+30        ;
  385.     call    printmsg        ;
  386.     lea    ebx,nlmsg        ;
  387.     call    printmsg        ;
  388.  
  389. ;*********************************************************
  390. ; Test terminate process (function 4C)
  391. ;*********************************************************
  392.     mov    ax,04c00h        ; Exit back to DOS.
  393.     int    21h            ;
  394.  
  395. _start_    endp                ;
  396.  
  397.  
  398. ;    
  399. ;    printmsg - Print a message to the screen
  400. ;
  401. ;    ebx - Points to the message to be printed out.  The message
  402. ;          must be null terminated.
  403. ;
  404.  
  405. printmsg proc    near
  406.  
  407.     push    edx            ; Save EDX.
  408.  
  409. #1:    mov    dl,[ebx]        ; Load the next character into DL and
  410.     or    dl,dl            ;    branch if null.
  411.     je    #3            ;
  412.  
  413.     mov    ah,02h            ; Output the character.
  414.     int    21h            ;
  415.  
  416.     cmp    byte ptr [ebx],0ah    ; If the character is a LF, then
  417.     jne    #2            ;    also output a CR.
  418.     mov    ah,02h            ;
  419.     mov    dl,0dh            ;
  420.     int    21h            ;
  421.  
  422. #2:    add    ebx,1            ; Increment the message pointer and
  423.     jmp    #1            ;    loop.
  424.  
  425. #3:    pop    edx            ; Restore EDX and return.
  426.     ret                ;
  427.  
  428. printmsg endp                ;
  429.  
  430.  
  431. ;
  432. ;     hlout - Output the hex long word in EAX to the screen
  433. ;
  434.  
  435. hlout    proc    near            ;
  436.  
  437.     push    eax            ; Output the high word of the long. 
  438.     ror    eax,16            ;
  439.     call    hwout            ;
  440.  
  441.     pop    eax            ; Output the low word of the long.
  442.     call    hwout            ;
  443.  
  444.     ret                ; Return.
  445.  
  446. hlout    endp                ;
  447.  
  448.  
  449. ;
  450. ;     hwout - Output the hex word in AX to the screen
  451. ;
  452.  
  453. hwout    proc    near            ;
  454.         
  455.     push    ax            ; Output the high byte of the word.
  456.     ror    ax,8            ;
  457.     call    hbout            ;
  458.  
  459.     pop    ax            ; Output the low byte of the word.
  460.     call    hbout            ;
  461.  
  462.     ret                ; Return.
  463.  
  464. hwout    endp                ;
  465.  
  466.  
  467. ;
  468. ;     hbout - Output the hex byte in AL to the screen
  469. ;
  470.  
  471. hbout    proc    near            ;
  472.         
  473.     push    ax            ; Output the high digit of the byte.
  474.     ror    ax,4            ;
  475.     call    hdout            ;
  476.  
  477.     pop    ax            ; Output the low digit of the byte.
  478.     call    hdout            ;
  479.  
  480.     ret                ; Return.
  481.  
  482. hbout    endp
  483.  
  484.  
  485. ;
  486. ;     hdout - Output the hex digit in AL to the screen
  487. ;
  488.  
  489. hdout    proc    near
  490.  
  491.     and    ax,0fh            ; Zap any extra bits and translate
  492.     cmp    ax,10            ;    to ASCII.
  493.     jge    #1            ;
  494.     add    al,'0'-'A'+10        ;
  495. #1:    add    al,'A'-10        ;
  496.  
  497.     push    dx            ; Call MS-DOS to output the digit.
  498.     mov    dl,al            ;
  499.     mov    ah,2h            ;
  500.     int    21h            ;
  501.     pop    dx            ;
  502.  
  503.     ret                ; Return.
  504.  
  505. hdout    endp
  506.  
  507.  
  508. ;    
  509. ;    flercheck - File error check
  510. ;
  511.  
  512. flercheck proc    near
  513.  
  514.     jc    #1            ; Make a quick return if no error.
  515.     ret                ;
  516.  
  517. #1:    push    ax            ; Save the error code.
  518.  
  519.     lea    ebx,ferrmsg        ; Output an error message.
  520.     call    printmsg        ;
  521.  
  522.     pop    ax            ; Output the DOS error code.
  523.     call    hwout            ;
  524.  
  525.     lea    ebx,nlmsg        ; Output a new line.
  526.     call    printmsg        ;
  527.  
  528.     mov    ax,04c01h        ; Exit back to MS-DOS.
  529.     int    21h            ;
  530.  
  531. flercheck endp                ;
  532.  
  533. _text    ends
  534.  
  535.     end _start_
  536.