home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
259_01
/
conio.asm
< prev
next >
Wrap
Assembly Source File
|
1988-02-25
|
18KB
|
350 lines
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
;* *
;* FILE: CONIO.ASM *
;* *
;* This file contains library routines for putch(), getch(), getche(), *
;* ungetch(), and kbhit(). Although these routines are included in the *
;* C libraries that come with the Microsoft (R) C compiler, they are *
;* redirectable, which renders them useless. (kbhit() is not redirectable, *
;* but was added to increase functionality; specifically, to indicate what *
;* key was pressed, while still implementing Ctrl-C and Ctrl-S checking. *
;* *
;* Since these five routines are used by cputs(), cgets(), cprintf(), and *
;* cscanf(), these higher-level functions are also useless, unless *
;* correctly working versions of putch(), getch(), getche(), ungetch(), *
;* and kbhit() are installed in the C libraries. *
;* *
;* (If you WANT your I/O to be redirectable, you can use putchar(), *
;* getchar(), ungetchar(), puts(), gets(), printf(), and scanf() ). *
;* *
;* When assembling, use the -dRNEAR option if assembling for small or *
;* compact models, and the -dRFAR option if assembling for medium, large, *
;* or huge models. If you are not using masm ver 4.0 or later, you will *
;* have to define either a RNEAR or RFAR label at the top of this program. *
;* *
;* To assemble, use a command like this: *
;* *
;* masm -mx -dRFAR conio; *
;* *
;* To install these routines into the library, use a command like this: *
;* *
;* lib llibc -putch -getch -getche -ungetch -kbhit +conio; *
;* *
;* *
;* Routines written by Jeff D. Pipkins at Quality Micro Systems, Inc. *
;* This file is hereby declared to be public domain. Since it is public *
;* domain, QMS (R) is not liable for ANY damages caused by the use or *
;* abuse of this material. Original release: 03/02/1987 *
;* *
;* NOTE: These routines were written to work with the Microsoft (R) *
;* C compiler (ver 3.0 or later), but they can easily be adapted *
;* to work with other C compilers on any IBM (R) PC compatible. *
;* *
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
;* *
;* Revision 04/16/1987 (JDP): *
;* Added code for ungetch() with a one-character buffer. *
;* See ungetch() and getch() routines for details. *
;* *
;* Revision 06/09/1987 (JDP): *
;* Modified getch() to translate lf to cr (it already translated *
;* cr to lf) so that a discriminating routine can tell the difference *
;* if necessary. *
;* Added kbhit() in response to a quest by The C User's Group for *
;* a standard routine. *
;* Added more documentation in the external .DOC file so that *
;* C programmers who have no assembly-language experience can read *
;* the specs without looking at the assembly code. *
;* *
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
IF1
IFNDEF RNEAR
IFNDEF RFAR
%OUT FATAL ERROR: You MUST specify either -dRNEAR or -dRFAR on the command line!
%OUT (Note: Remember to use the -MX option as well!)
.ERR
ERROR EQU TRUE
ENDIF
ENDIF
IFDEF RNEAR
IFDEF RFAR
%OUT FATAL ERROR: You CANNOT specify both -dRNEAR and -dRFAR on the command line!
%OUT (Note: Remember to use the -MX option as well!)
.ERR
ERROR EQU TRUE
ENDIF
ENDIF
IFNDEF ERROR
IFDEF RNEAR
%OUT Generating object file for use with small or compact memory models...
ELSE
%OUT Generating object file for use with medium, large, or huge memory models...
ENDIF
ENDIF
ENDIF
_TEXT Segment Byte Public 'CODE'
Assume CS:_TEXT
Parm Equ [BP]
ungot_char DW 0 ; Storage for one keystroke of lookahead
EOF Equ -1
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
;* *
;* void putch(c) *
;* int c; *
;* *
;* Displays the character passed on the screen, regardless of redirection. *
;* NOTE: Any line feeds displayed ('\n') will be preceeded by a cr ('\r'). *
;* *
;* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Public _putch
IFDEF RFAR
_putch Proc Far
ELSE
_putch Proc Near
ENDIF
;* Parameters
IFDEF RFAR
putch Struc
DW ? ; Single-word save area for BP
DD ? ; Double-word return address
char DW ? ; Character to put on the console
putch EndS
ELSE
putch Struc
DW ? ; Single-word save area for BP
DW ? ; Single-word return address
char DW ? ; Character to put on the console
putch EndS
ENDIF
;* Output character to console using video BIOS
Push BP
Mov BP, SP
Mov CX, SI ; Some BIOS clones destroy SI & DI
Mov DX, DI ; so save them first.
Mov AH, 0EH ; TTY output function
Mov BL, 7 ; Attribute
Mov AL, Byte Ptr Parm[char]
Cmp AL, 0AH ; Line feed?
Je CRLF ; If so, translate to CRLF sequence
Int 10H ; Video BIOS
Mov SI, CX ; Restore SI
Mov DI, DX ; and DI
Pop BP ; BIOS destroys BP if scrolling occurs.
Ret
CRLF:
Mov AH, 0BH ; Check keyboard status
Int 21H ; Allow Ctrl-C or Ctrl-S
Mov AX, 0E0DH
Int 10H
Mov AX, 0E0AH
Int 10H
Mov SI, CX
Mov DI, DX
Pop BP
Ret
_putch En