home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / ASM / BCDASM.ZIP / BCDASM / SRC / CONSOLIO.ASM < prev   
Encoding:
Assembly Source File  |  1997-06-03  |  6.1 KB  |  280 lines

  1.     title    CONSOLIO -- Copyright 1997, Morten Elling
  2.     subttl    Simple console Input/Output (DOS/Win32)
  3.  
  4.     include    model.inc
  5.     include    modelt.inc
  6.     include consolio.ash
  7.     include consolio.inc    ; Get DOS/Win32 definitions
  8.  
  9.     @CODESEG
  10.     locals
  11.  
  12.     ife @isWin32    ; ----- DOS section ---------------------------
  13.  
  14. ;//////////////////////////////////////////////////////////////////////
  15. ;//    Name    IsDevRedir                      (DOS)
  16. ;//    Desc    Test redirection of standard input or standard output.
  17. ;//
  18. ;//
  19. ;//    Entry    Passed arg
  20. ;//    Exit    Acc > 0: Is redirected.
  21. ;//        Acc = 0: Not redirected.
  22. ;//        Acc =-1: Error on DOS call.
  23.  
  24. IsDevRedir proc
  25. arg    hDev    :@uint        ; Device handle (STDIN or STDOUT)
  26. @uses    rbx,rdx
  27. ;.
  28.     mov   ax, [hDev]
  29.     cmp   ax, STDOUT
  30.     ja sh @@err
  31.     xchg  bx, ax
  32.     mov   ax, 4400h     ; Get Device Data (into dx)
  33.     int   21h
  34.     jc sh @@err
  35.     lea   ax, [bx+81h]
  36.     and   dl, al        ; Isolate bits 7,0 (STDOUT: 7,1)
  37.     cmp   dl, al
  38.     mov   ax, 0
  39.     jz sh @@ret        ; Has original device attributes
  40.     inc   ax        ; Has changed
  41.     jmp sh @@ret
  42. @@err:    or    ax, -1
  43. @@ret:    RET
  44. IsDevRedir endp
  45.  
  46.  
  47. ;//////////////////////////////////////////////////////////////////////
  48. ;//    Name    GetKey                           (DOS)
  49. ;//    Desc    (Wait for and) Read character from the standard input
  50. ;//        device (usually the keyboard).
  51. ;//
  52. ;//
  53. ;//    Entry    N/A
  54. ;//    Exit    Acc = keyboard code (al zero if extended key).
  55. ;//
  56. ;//    Note    Checks for Ctrl-C and Ctrl-Break.
  57.  
  58. GetKey    proc
  59. ;.
  60.     mov   ah, 08h        ; Read Keyboard Without Echo
  61.     int   21h
  62.     sub   ah, ah        ; Assume normal key
  63.     test  al, al
  64.     jnz sh @@ret
  65.     mov   ah, 08h        ; Was extended; read again
  66.     int   21h
  67.     mov   ah, al
  68.     sub   al, al
  69. @@ret:    RET
  70. GetKey    endp
  71.  
  72.  
  73. ;//////////////////////////////////////////////////////////////////////
  74. ;//    Name    WriteZStr                      (DOS)
  75. ;//    Desc    Write zero-terminated Ascii string to standard output.
  76. ;//
  77. ;//
  78. ;//    Entry    Passed arg
  79. ;//    Exit    Acc = No. of bytes written.
  80.  
  81. WriteZStr proc
  82. arg    pStr    :dataptr    ; Addr of AsciiZ string
  83. @uses    ds,rbx,rcx,rdx
  84. ;.
  85.     @LDS  dx, [pStr]
  86.     mov   bx, dx
  87.     sub   al, al
  88.     dec   bx
  89. @@waz:    inc   bx
  90.     cmp   al, [bx]        ; End of string?
  91.     jnz   @@waz
  92.     mov   cx, bx
  93.     sub   cx, dx        ; String length, less 0
  94.     jz sh @@ret
  95.     mov   bx, STDOUT
  96.     mov   ah, 40h        ; Write File or Device
  97.     int   21h
  98.     mov   cx, ax        ; ax = output length
  99. @@ret:    mov   ax, cx
  100.     RET
  101. WriteZStr endp
  102.  
  103.  
  104. ;//////////////////////////////////////////////////////////////////////
  105. ;//    Name    WriteNL                       (DOS)
  106. ;//    Desc    Write new-line (one CR/LF pair) to standard output.
  107. ;//
  108. ;//
  109. ;//    Entry    N/A
  110. ;//    Exit    Acc = No. of bytes written.
  111.  
  112. WriteNL    proc
  113. @uses    rbx,rcx,rdx
  114. ;.
  115.     if @isStackFar
  116.     push  ds
  117.     @LDSEGM ds, ss
  118.     endif
  119.     mov   ax, 0dh + 0ah shl 8
  120.     push  ax
  121.     mov   dx, sp
  122.     mov   cx, 2
  123.     mov   bx, STDOUT
  124.     mov   ah, 40h        ; Print the stack
  125.     int   21h        ; ax = output length
  126.     pop   cx        ; Clean up stack
  127.     if @isStackFar
  128.     pop   ds
  129.     endif
  130.     RET
  131. WriteNL    endp
  132.  
  133.  
  134.     else    ; ----- Win32 section ---------------------------------
  135.  
  136.  
  137. ;//////////////////////////////////////////////////////////////////////
  138. ;//    Name    IsDevRedir                    (Win32)
  139. ;//    Desc    Test redirection of standard input or standard output.
  140. ;//
  141. ;//
  142. ;//    Entry    Passed arg
  143. ;//    Exit    Acc > 0: Is redirected.
  144. ;//        Acc = 0: Not redirected.
  145. ;//        Acc =-1: Error on Win32 API call.
  146. ;//
  147. ;//    Note    Returns 0 if stdout redirected to LPTn (COMn?)
  148. ;//        or if a DOS box is running under Win95 v4.00.950a.
  149. ;//
  150. ;//    ToDo    Make this work as the DOS version!
  151.  
  152. IsDevRedir proc
  153. arg    hDev    :@uint        ; STD_INPUT_HANDLE or STD_OUTPUT_HANDLE
  154. ;.
  155. ; ----- Return handle to current standard input/output
  156. ;    (may have been redirected)
  157.         mov   eax, [hDev]
  158.     cmp   eax, STD_INPUT_HANDLE
  159.     je sh @@idr1
  160.     cmp   eax, STD_OUTPUT_HANDLE
  161.     jne sh @@err
  162. @@idr1: call  GetStdHandle, eax
  163.     test  eax, eax
  164.     jz sh @@err
  165.  
  166. ; ----- See what file type
  167.     call  GetFileType, eax
  168.     cmp   eax, FILE_TYPE_CHAR
  169.     setnz al        ; Set flag if not console
  170.     and   eax, 1
  171.     jmp sh @@ret
  172.  
  173. @@err:  or    eax, -1
  174. @@ret:    RET
  175. IsDevRedir endp
  176.  
  177.  
  178. ;//////////////////////////////////////////////////////////////////////
  179. ;//    Name    GetKey                         (Win32)
  180. ;//    Desc    (Wait for and) Read character from the standard input
  181. ;//        device (usually the keyboard).
  182. ;//
  183. ;//
  184. ;//    Entry    N/A
  185. ;//    Exit    Acc = keyboard code.
  186. ;//
  187. ;//    Note    Doesn't read extended keys (function or arrow keys).
  188. ;//        Returns Ctrl-C as 03h (but Ctrl-Break is handled by
  189. ;//        Windows).
  190.  
  191. GetKey    proc
  192. local    @@mode    :dword, \
  193.     @@rbuf    :dword, \
  194.     @@rlen    :dword
  195. @uses    rsi,rbx,rdx
  196. ;.
  197.     call  GetStdHandle, STD_INPUT_HANDLE
  198.     mov   ebx, eax
  199.  
  200. ; ----- Set device in 'raw' mode
  201.     lea   esi, [@@mode]
  202.     call  GetConsoleMode, ebx, esi
  203.     mov   eax, [esi]
  204.     and   eax, NOT (ENABLE_LINE_INPUT or ENABLE_ECHO_INPUT \
  205.         or ENABLE_PROCESSED_INPUT)
  206.     call  SetConsoleMode, ebx, eax
  207.  
  208. ; ----- Read std. input device
  209.     lea   edx, [@@rbuf]
  210.     lea   eax, [@@rlen]
  211.     call  ReadFile, ebx, edx, 1, eax, NULL
  212.  
  213. ; ----- Restore device mode
  214.     mov   eax, [esi]
  215.     call  SetConsoleMode, ebx, eax
  216.  
  217. ; ----- Return key
  218.     mov   eax, [@@rbuf]    ; ReadFile buffer contents
  219.     and   eax, 0ffh
  220.     RET
  221. GetKey    endp
  222.  
  223.  
  224. ;//////////////////////////////////////////////////////////////////////
  225. ;//    Name    WriteZStr                    (Win32)
  226. ;//    Desc    Write zero-terminated Ansi string to standard output.
  227. ;//
  228. ;//
  229. ;//    Entry    Passed arg
  230. ;//    Exit    Acc = No. of bytes written.
  231.  
  232. WriteZStr proc
  233. arg    pzStr    :dataptr    ; Addr of AnsiZ string
  234. @uses    rbx,rcx,rdx
  235. ;.
  236.     call  GetStdHandle, STD_OUTPUT_HANDLE
  237.     mov   ebx, eax
  238.  
  239. ; ----- Get string length
  240.     mov   edx, [pzStr]
  241.     lea   ecx, [edx-1]
  242.     sub   eax, eax
  243. @@w1:    inc   ecx
  244.     cmp   al, [ecx]
  245.     jnz   @@w1
  246.     sub   ecx, edx
  247.     jz sh @@ret
  248.  
  249. ; ----- Do the output
  250.     push  eax        ; Allocate local var
  251.     mov   eax, esp
  252.     call  WriteFile, ebx, \
  253.         edx, ecx, eax, NULL
  254.     pop   ecx        ; ecx = output length
  255. @@ret:    xchg  eax, ecx
  256.     RET
  257. WriteZStr endp
  258.  
  259.  
  260. ;//////////////////////////////////////////////////////////////////////
  261. ;//    Name    WriteNL                     (Win32)
  262. ;//    Desc    Write new-line (one CR/LF pair) to standard output.
  263. ;//
  264. ;//
  265. ;//    Entry    N/A
  266. ;//    Exit    As for WriteZStr
  267. ;//     Calls   WriteZStr
  268.  
  269. WriteNL proc
  270. @uses    rcx
  271. ;.
  272.     push  00000a0dh     ; Ansi CR/LF string
  273.     call  WriteZStr, esp
  274.     pop   rcx        ; Remove string
  275.     RET
  276. WriteNL endp
  277.  
  278.     endif
  279.  
  280.     END