- Nov 23, 2019
- 1
- 0
- 6
Hello to everyone and thank you for reading my question.
This is my code so far:
Ok, so i'm reading a string from the keyboard and i'm displaying it on the next row. I don't know if this method is suited for outputing my string into a .txt file instead, and if it is, i would really appreciate if someone could give me a start.
My first ideea would be to delete my print_string procedure and replace it with one that prints into a .txt file, but i don't know if it is that simple.
P.S: This code is not entirely mine.
This is my code so far:
Code:
; this macro prints a char in al and advances
; the current cursor position:
putc macro char
push ax
mov al, char
mov ah, 0eh
int 10h
pop ax
putc endm
org 100h
jmp start ; skip over the declarations and data
buffer db "empty buffer --- empty buffer"
size = $ - offset buffer ; declare constant
start:
; print a welcome message:
; get string to ds:di
lea di, buffer ; buffer offset.
mov dx, size ; buffer size.
call get_string
putc 0Dh
putc 10 ; next line.
; print string in ds:si using procedure:
mov si, di
call print_string
; wait for any key...
mov ax, 0
int 16h
ret
; get a null terminated string from keyboard,
; write it to buffer at ds:di, maximum buffer size is set in dx.
; 'enter' stops the input.
get_string proc near
push ax
push cx
push di
push dx
mov cx, 0 ; char counter.
cmp dx, 1 ; buffer too small?
jbe empty_buffer ;
dec dx ; reserve space for last zero.
;============================
; eternal loop to get
; and processes key presses:
wait_for_key:
mov ah, 0 ; get pressed key.
int 16h
cmp al, 0Dh ; 'return' pressed?
jz exit
cmp al, 8 ; 'backspace' pressed?
jne add_to_buffer
jcxz wait_for_key ; nothing to remove!
dec cx
dec di
putc 8 ; backspace.
putc ' ' ; clear position.
putc 8 ; backspace again.
jmp wait_for_key
add_to_buffer:
cmp cx, dx ; buffer is full?
jae wait_for_key ; if so wait for 'backspace' or 'return'...
mov [di], al
inc di
inc cx
; print the key:
mov ah, 0eh
int 10h
jmp wait_for_key
;============================
exit:
; terminate by null:
mov [di], 0
empty_buffer:
pop dx
pop di
pop cx
pop ax
ret
get_string endp
; print null terminated string at current cursor position,
; raddress of string in ds:si
print_string proc near
push ax ; store registers...
push si ;
next_char:
mov al, [si]
cmp al, 0
jz printed
inc si
mov ah, 0eh ; teletype function.
int 10h
jmp next_char
printed:
pop si ; re-store registers...
pop ax ;
ret
print_string endp
Ok, so i'm reading a string from the keyboard and i'm displaying it on the next row. I don't know if this method is suited for outputing my string into a .txt file instead, and if it is, i would really appreciate if someone could give me a start.
My first ideea would be to delete my print_string procedure and replace it with one that prints into a .txt file, but i don't know if it is that simple.
P.S: This code is not entirely mine.