x8086 print string to txt file. (emu8086)

nousernamefound

Junior Member
Nov 23, 2019
1
0
6
Hello to everyone and thank you for reading my question.

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.
 

Cogman

Lifer
Sep 19, 2000
10,277
125
106
The short answer on what to do is "It depends".

What does it depend on? Mostly, the OS that you are using. Your print method is triggering an interrupt (10h). Interrupts are setup by the operating system. What they do is entirely dependent on the OS that you are running under.

In the modern era, the best practice is to not rely on the interrupts directly, but instead call an OS library which in turn triggers the right interrupts for the kernel level operation that you want to achieve.

As this is likely a school project, I'm going to guess that half the assignment is reading up (or discovering) your specific OS interrupts and triggering them directly. With that in mind, nobody on this forum will be able to give you a good answer to what you need to do to make this work. (not without reading all your homework docs... which, we aren't able to do :) ).

Were I to do this for real, I'd probably link to libc and use the C api IO libraries to do my work (Though, probably wouldn't be doing this in ASM in the first place). That would give you the most platform independent way to write stuff out.

A lower level solution would be to find your specific OS or Kernel API for IO and call that.

The lowest level is triggering the right interrupt to make stuff happen. I'd do that as a last resort.
 

cytg111

Lifer
Mar 17, 2008
23,049
12,719
136
Super fast cheat : Write it in C, fopen blabla, a few lines of code, generate the exe, open it in olly or x64dbg and steal the code.