Prog_Data SEGMENT
Prompt DB 16,"Input string -->"
Str DB 80 DUP(32)
VideoSeg EQU 0B800H
Prog_Data ENDS
;
;
Prog_Code SEGMENT
ASSUME CS:Prog_Code, DS:Prog_Data, SS:Prog_Stack
Main_Prog PROC
MOV AX, Prog_Data
MOV DS, AX ; load the offset of the Data segment
;
MOV DH, 10 ; cursor positioning (Y=10)
MOV DL, 15 ; cursor positioning (X=15)
MOV AH, 30 ; character attributes
;
CALL ClrScr ; clear screen
;
MOV SI, offset Prompt ; load offset of Prompt in SI
CALL WriteLn ; output prompt on the screen
;
MOV SI, offset Str
CALL ReadLn ; read string from the keyboard
;
MOV DH, 20
MOV DL, 15 ; new cursor ccordinates
MOV SI, offset Str ; load offset of input string to SI
CALL WriteLn ; output string
MOV AX, 4C00H ; return to DOS
INT 21H
Main_Prog ENDP
;
;
BinOut PROC ; outputs BX on the screen in binary form
PUSH BX ; save the registers
PUSH CX
;
MOV CX, 16 ; word length
Nextbit: SHL BX, 1
JC one
MOV AL, '0' ; bit=0
CALL PutChar ; output it on the screen
JMP weiter
One: MOV AL, '1' ; bit=1
CALL PutChar ; output it on the screen
Weiter: INC DL ; new cursor position
LOOP nextbit ; main loop over all bits
;
POP CX ; restore the registers
POP BX
RET
BinOut ENDP
;
ReadLn PROC ; reads a string from the keyboard --> [SI]
PUSH DI ; save registers
PUSH BX
PUSH CX
MOV DI, SI
MOV BX, 0 ; set string length =0
;
Nextchar: MOV CH, AH ; AH is used in GetChar
CALL GetChar ; get a char from the keyboard
MOV AH, CH ; restore AH (color settings)
;
CMP AL, 13 ; end of string ?
JE Done ; YES
;
Proceed: CALL PutChar ; echo char on the screen
INC DL ; new cursor position
INC SI ; address of the new character
MOV [SI], AL ; save the string character
INC BX ; increase the string length
JMP Nextchar
;
Done: MOV [DI],BL ; set the string length
POP CX ; restore the registers
POP BX
POP DI
RET
ReadLn ENDP
;
;
GetChar PROC ; returns the ASCII code in AL
label1: MOV AH, 0 ; BIOS read key procedure
INT 16H
CMP AL, 0 ; special key pressed ?
JE label1 ; yes, wait for another key
RET
GetChar ENDP
;
WriteLn PROC ; writes a line on the screen starting with the
; currect cursor position (X,Y)=(DL,DH)
; SI points to the string
PUSH AX
PUSH CX
PUSH SI
MOV CL, [SI] ; CL := Length(Str)
INC SI
;
outchar: CMP CL, 0
JE outdone
MOV AL, [SI] ; AL := next character
CALL PutChar ; output char to the screen
INC SI ; offset of the next character
INC DL ; new column
DEC CL
JMP outchar
outdone: POP SI
POP CX
POP AX
RET
WriteLn ENDP
;
;
PutChar proc ; writes a character to VRAM. AL=char, AH=attribute
; DH/DL = row/column (0-24, 0-79)
push DI ; save the registers
push ES
push AX
mov AX, VideoSeg ; B800H : video buffer segment
mov ES, AX
xor AX, AX ; clear AX
mov AL, DH ; load the row number in AX
shl AX, 1 ; multiply AX by 2^7=128
shl AX, 1
shl AX, 1
shl AX, 1
shl AX, 1
shl AX, 1
shl AX, 1
mov DI, AX ; store the result in DI
xor AX, AX ; clear AX
mov AL, DH ; load the row number in AX
shl AX, 1 ; multiply AX by 2^5=32
shl AX, 1
shl AX, 1
shl AX, 1
shl AX, 1
add DI, AX ; DI:=DH*(128+32)=DH*160
xor AX, AX ; clear AX
mov AL, DL ; load the column number in AX
shl AL, 1 ; multiply the column by 2
add DI, AX ; DI:=offset of the screen byte
pop AX ; restore AX
mov ES:[DI], AX ; store char/attribute
pop ES ; restore the registers
pop DI
ret ; return to the calling program
PutChar endp ; end of the procedure
;
;
ClrScr PROC ; fill the screen with the BG color
PUSH CX
PUSH DI
PUSH ES
MOV DI, VideoSeg
MOV ES, DI
MOV AL, ' '
MOV DI, 0
MOV CX, 2000
CLD
REP STOSW
POP ES
POP DI
POP CX
RET
ClrScr ENDP
;
Prog_Code ENDS
;
Prog_Stack SEGMENT STACK
DB 64 DUP(0)
Prog_Stack ENDS
;
;
END
|